Python/traversals/
help(print)
Help on built-in function print in module builtins:
print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
print(“Enter the value of the root node: “, end=””)
Enter the value of the root node:check = input().strip().lower()
RUFDcheck
‘rufd’
isinstance(4,object)
True
def in_order(node):
if not isinstance(node,object):#false,不执行return,直接执行下一句
return
print(“node.data”)
in_order(4)
node.data
def in_order(node):
if isinstance(node,object):#ture,执行return后跳出
return
print(“node.data”)
in_order(4)
data=6
class TreeNode:
def init(self, data):
self.data = data
self.right = None
self.left = None
tree_node = TreeNode(data)
tree_node
<main.TreeNode object at 0x006CF9D0>tree_node()
Traceback (most recent call last):
File “<pyshell#231>”, line 1, in
tree_node()
TypeError: ‘TreeNode’ object is not callabletree_node.data
6tree_node.right
tree_node = TreeNode(3)
tree_node.data
3
check = input().strip().lower()
9left_data = int(check)
left_data
9isinstance(left_data,int)
Truea= input().strip().lower()
7isinstance(a,int)
False
Python/sorts/Odd-Even_transposition_parallel.py
for i in range(10, 0, -1):
print(i)
10
9
8
7
6
5
4
3
2
1
arr = []
for i in range(10, 0, -1):
arr.append(i)
print(*arr)
10 9 8 7 6 5 4 3 2 1arr
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]print(arr)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
##Process是啥函数from multiprocessing import Process, Pipe, Lock
Process(target = abs, args = (-1,-3,4))
<Process(Process-5, initial)>processArray=[]
processArray.append(Process(target = abs, args = (-1,-3,4)))
processArray
[<Process(Process-6, initial)>]
#lock used to ensure that two processes do not access a pipe at the same time
processLock = Lock()
processLock.acquire()
TrueprocessLock.release()
Python/sorts/Odd-Even_transposition_single-threaded.py
list
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]arr=list
for i in range(0, 10):
for i in range(i % 2, 10 - 1, 2):
if arr[i + 1] < arr[i]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
print(*arr)
9 10 7 8 5 6 3 4 1 2
9 7 10 5 8 3 6 1 4 2
7 9 5 10 3 8 1 6 2 4
7 5 9 3 10 1 8 2 6 4
5 7 3 9 1 10 2 8 4 6
5 3 7 1 9 2 10 4 8 6
3 5 1 7 2 9 4 10 6 8
3 1 5 2 7 4 9 6 10 8
1 3 2 5 4 7 6 9 8 10
1 2 3 4 5 6 7 8 9 10
Python/sorts/bogo_sort.py
user_input =input(‘Enter numbers separated by a comma:\n’).strip()
Enter numbers separated by a comma:
-3,9,-5,8unsorted = [int(item) for item in user_input.split(’,’)]
unsorted
[-3, 9, -5, 8]
while not True:#假的不执行
print(“yes not true”)
while not False:#真的就执行
print(“yes not true”)
break
yes not true
Python/sorts/bubble_sort.py
##break不可以单独用在if中,必须配合循环中使用来终止循环
swapped = False
if not swapped:
print(“break”)
break
if not swapped:
break
SyntaxError: ‘break’ outside loop
for i in range(3):
swapped = False
if not swapped: break
Python/sorts/bucket_sort.py
unsorted
[-1.0, 4.0, -8.0, 7.0]my_list=unsorted
min_value, max_value = (min(my_list), max(my_list))
min_value, max_value
(-8.0, 7.0)
bucket_count
4.0[[] for _ in range(int(bucket_count))]
[[], [], [], []]
buckets
[[-8.0, 9.0], [-1.0, 2.0], [4.0, 5.0], [7.0, 7.0]]
sorted([buckets[i][j] for i in range(2)
for j in range(2)])
[-8.0, -1.0, 2.0, 9.0]print([buckets[i][j] for i in range(2)
for j in range(2)])
[-8.0, 9.0, -1.0, 2.0]
print([buckets[i][j] for i in range(4)
for j in range(2)])
[-8.0, 9.0, -1.0, 2.0, 4.0, 5.0, 7.0, 7.0]