python recursion and dichotomy

Keywords: Python ascii encoding

A built-in function

1. Revsedflip, return iterator

# take s Inversion
s = 'It's not Shanghai. Tap water comes from the sea'
# Method 1
print(s[::-1])
# Method two
s1 = reversed(s)
i = ''
for el in s1:
  i +=el
  print(i)

Slice slice

lis = ['nishi','woshi','shuia','benjim']
s = slice(1,3)
print(lis[s])

  3.formate

# Format output
s = "My name is{name},I'd like to go{adress},I like it{hobby}.".format(name='zhangmeng',adress='Shanghai',hobby='dance')
print(s)


# Character string
print(format('test', '<20')) # Left alignment
print(format('test', '>20')) # Right alignment
print(format('test', '^20')) # Centered
# numerical value
print(format(3, 'b')) # Binary system
print(format(97, 'c')) # convert to unicode character
print(format(11, 'd')) # Decimal system
print(format(11, 'o')) # Octal number system
print(format(11, 'x')) # Hexadecimal(Lowercase letters)
print(format(11, 'X')) # Hexadecimal(Capital)
print(format(11, 'n')) # and d equally
print(format(11)) # and d equally
# Floating point number
print(format(123456789, 'e')) # Scientific counting. Keep 6 decimal places by default
print(format(123456789, '0.2e')) # Scientific counting. Keep 2 decimal places(A lowercase letter)
print(format(123456789, '0.2E')) # Scientific counting. Keep 2 decimal places(Capitalization)
print(format(1.23456789, 'f')) # Decimal point counting. Keep 6 decimal places
print(format(1.23456789, '0.2f')) # Decimal point counting. Keep 2 decimal places
print(format(1.23456789, '0.10f')) # Decimal point counting. Keep 10 decimal places
print(format(1.23456789e+10000, 'F')) # Decimal point counting.

4. type() return type ﹣ ord(); input character find the position of character encoding ﹣ chr(); input position find the corresponding character ﹣ ascii() judge whether the information given is ASCII

for i in range(65536):
    print(chr(i), end="")

Two. Recursion.

Function calls itself, recursive entry (parameter) and exit (return)

Syntax:

def func():
    print('I'm recursive.')
    func()

func() # Official display up to 1000.But it won't run to 1000. It's 998
# Traversal of tree structure

import os
def func(lujing, n):
    lis = os.listdir(lujing) # Open the folder and list all file names in the folder
    for el in lis: # el Is the name of the file
    # Restore file path
    path = os.path.join(lujing,el)
    if os.path.isdir(path):# Determine whether the path is a folder
        print('*' * n,el) # Show folder name
        func(path, n+1) # Once more
    else:
        print('/t'*n,el) # Display file

func('f:/a', 0)
import os
def func(lujing, n):
    lis = os.listdir(lujing ) # Open the folder and list all file names in the folder
    for el in lis: # el File name
# Path to restore file
    path = os.path.join(lujing, el)
    if os.path.isdir(path): # Determine whether the path is a folder
        print('*'* n, el) # Show folder name
        func(path, n+1) # Once more
    else:
        with open(path,mode='wb') as f: # Open file, write content
            f.write(b'123456')
              print('/t'*n, el) # Display file
func('F:/a',0)

Dichotomy

The search efficiency is very high. The main function of dichotomy search is to find elements
# binary search 
lst = [1,4,6,7,9,21,23,45,67,87,65,43,89]
n = int(input('Please enter a number:'))
lst = sorted(lst)
# print(lst)
left = 0
right = len(lst)-1
while left <= right:
    mid = (left + right)//2
    if n > lst[mid]:
        left = mid+1
    elif n < lst[mid]:
        right = mid -1
     else:
        print("existence")
        break
else:
print('Non-existent')
# Recursive cut list

def func(n,lst):
    left = 0
    right = len(lst)-1
    if lst != []:
        mid = (left+right)//2
         if n>lst[mid]:
            func(n,lst[mid+1:])
        elif n < lst[mid]:
            func(n,lst[:mid])
        else:
            print("Eureka")
            return
    else:
        print("Can't find")
        return

n = int(input('Please enter the number you want to find:'))
lst = [1,4,6,7,9,21,23,45,67,87,65,43,89]


func(n,lst)
# Recursion 2
def func(n,lst,left,right): # Recursion finds out what is mutable and what is immutable
    if left <= right:
        mid = (left+right)//2
            if n > lst[mid]:
                left = mid+1
                return func(n,lst,left,right)
            elif n < lst[mid]:
                right = mid - 1
                return func(n, lst, left, right)
            else:
                print('Eureka')
                return mid # difficulty
    else:
        print('Can't find')
        return -1

n = int(input('Please enter the number you want to find:'))
lst = [13,45,56,67,78,57,89,101]
ret = func(n,lst,0,len(lst)-1)
print(ret)
# Fastest way to find

lst = [13,45,56,57,67,78,89,101]

# Find the maximum number
new_lst = []
for i in range(99):
    new_lst.append(0)

for i in lst:
    new_lst[i] = 1

i = int(input('Please enter the data you are looking for:'))
if new_lst[i] == 0:
    print('Non-existent')
else:
    print('existence')

 

Posted by Benny007 on Fri, 06 Dec 2019 23:42:27 -0800