Skip to content

Commit dc36b61

Browse files
committed
upload Lesson 13
1 parent bba9e0b commit dc36b61

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

13-Fibonacci_cckao.pdf

439 KB
Binary file not shown.

FibFrog_performance_50.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# you can write to stdout for debugging purposes, e.g.
2+
# print("this is a debug message")
3+
4+
def solution(A):
5+
# write your code in Python 3.6
6+
7+
#test_A = [1,1,1,1,1,1,1]
8+
#result = my_solution(test_A)
9+
#print(result)
10+
#print('end')
11+
12+
result = my_solution(A)
13+
return result
14+
15+
def my_solution(A):
16+
len_river = len(A)
17+
18+
fabonacci = []
19+
fabonacci.append(0)
20+
fabonacci.append(1)
21+
index = 1
22+
while fabonacci[index] <= len_river:
23+
index += 1
24+
temp = fabonacci[index-1] + fabonacci[index-2]
25+
fabonacci.append(temp)
26+
27+
# print(fabonacci)
28+
# fabonacci = fabonacci[:-1] # remove the last element
29+
fabonacci = fabonacci[::-1] # reverse a list in Python
30+
# print(fabonacci)
31+
32+
my_queue = []
33+
my_dictionary = {-1:0} # position:-1, steps:0
34+
my_queue.append(my_dictionary)
35+
# print(my_queue)
36+
# print(len(my_queue))
37+
38+
index = 0
39+
while True:
40+
41+
# cannot take element from queue anymore
42+
if len(my_queue)==index:
43+
# print( len(my_queue) )
44+
# print( index )
45+
return -1
46+
47+
# get from queue
48+
temp_dictionary = my_queue[index]
49+
# print(temp_dictionary)
50+
51+
# get key and value
52+
for key in temp_dictionary:
53+
current_position = key
54+
current_step = temp_dictionary[key]
55+
# print(current_position)
56+
# print(current_step)
57+
58+
# from big to small
59+
for item in fabonacci:
60+
61+
next_position = current_position + item
62+
63+
# reach the other side
64+
if next_position == len_river:
65+
current_step += 1
66+
return current_step
67+
68+
# can not jump
69+
elif next_position > len_river:
70+
pass
71+
elif next_position < 0 :
72+
pass
73+
elif A[next_position] ==0:
74+
pass
75+
76+
# jump to next position
77+
else:
78+
current_step += 1
79+
temp_dictionary = {next_position: current_step}
80+
my_queue.append(temp_dictionary)
81+
82+
A[next_position] = 0 # important
83+
84+
index += 1 # take "next element" from queue
85+

Ladder_high_performance.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# you can write to stdout for debugging purposes, e.g.
2+
# print("this is a debug message")
3+
4+
def solution(A, B):
5+
# write your code in Python 3.6
6+
7+
L = len(A)
8+
max_is_sufficient = max(A)
9+
10+
# compute num of ways (by using Fibonacci)
11+
dictionary_num_ways = {}
12+
dictionary_num_ways[1] = 1
13+
dictionary_num_ways[2] = 2 # 1+1 or 2
14+
for x in range(3, max_is_sufficient+1, 1):
15+
dictionary_num_ways[x] = dictionary_num_ways[x-1] + dictionary_num_ways[x-2]
16+
# dictionary_num_ways[x-1] + '1'
17+
# or
18+
# dictionary_num_ways[x-2] + '2'
19+
dictionary_num_ways[x] = dictionary_num_ways[x] % (2 ** 30)
20+
# note: very important (to be quick in the extreme_large case)
21+
22+
# compute results ( num_ways % (2 ** B[index]) )
23+
result = [0] * L
24+
for index in range(L):
25+
num_ways = dictionary_num_ways[ A[index] ]
26+
each_result = num_ways % ( 2 ** B[index] )
27+
result[index] = each_result
28+
29+
return result

Ladder_low_performance.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# you can write to stdout for debugging purposes, e.g.
2+
# print("this is a debug message")
3+
4+
def solution(A, B):
5+
# write your code in Python 3.6
6+
7+
L = len(A)
8+
9+
# compute num of ways (by using Fibonacci)
10+
dictionary_num_ways = {}
11+
dictionary_num_ways[1] = 1
12+
dictionary_num_ways[2] = 2 # 1+1 or 2
13+
for x in range(3, L+1, 1):
14+
dictionary_num_ways[x] = dictionary_num_ways[x-1] + dictionary_num_ways[x-2]
15+
# dictionary_num_ways[x-1] + '1'
16+
# or
17+
# dictionary_num_ways[x-2] + '2'
18+
19+
# compute results ( num_ways % (2 ** B[index]) )
20+
result = [0] * L
21+
for index in range(L):
22+
num_ways = dictionary_num_ways[ A[index] ]
23+
each_result = num_ways % ( 2 ** B[index] )
24+
result[index] = each_result
25+
26+
return result

0 commit comments

Comments
 (0)