Skip to content

Commit df696b7

Browse files
committed
upload Lesson 15
1 parent 01c19a8 commit df696b7

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

15-CaterpillarMethod_cckao.pdf

453 KB
Binary file not shown.

AbsDistinct.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
my_dictionary = {}
8+
9+
for item in A:
10+
temp = abs(item)
11+
if temp not in my_dictionary:
12+
my_dictionary[temp] = True
13+
14+
# print(my_dictionary)
15+
#print(len(my_dictionary))
16+
return len(my_dictionary)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# you can write to stdout for debugging purposes, e.g.
2+
# print("this is a debug message")
3+
4+
def solution(M, A):
5+
# write your code in Python 3.6
6+
7+
# try to remember which value was counted
8+
current_dictionary = {}
9+
10+
len_A = len(A)
11+
count_slice = 0
12+
begin = 0
13+
end = 0
14+
15+
# cbombine the two loops (using one while loop)
16+
while (begin < len_A) and (end < len_A):
17+
# distinct value (end +=1)
18+
if A[end] not in current_dictionary:
19+
current_dictionary[ A[end] ] = 1
20+
# super important: not just plus 1 (the key to be faster)
21+
count_slice += (end - begin +1)
22+
if count_slice > 1_000_000_000:
23+
return 1_000_000_000
24+
end +=1
25+
# smae value
26+
else:
27+
# the same value happens (begin +=1)
28+
# important: remove the value of begin
29+
current_dictionary.pop(A[begin])
30+
begin+=1
31+
32+
return count_slice
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# you can write to stdout for debugging purposes, e.g.
2+
# print("this is a debug message")
3+
4+
def solution(M, A):
5+
# write your code in Python 3.6
6+
7+
# try to remember which value was counted
8+
current_dictionary = {}
9+
10+
len_A = len(A)
11+
count_slice = 0
12+
begin = 0
13+
end = 0
14+
15+
for begin in range(len_A):
16+
end = begin
17+
# print(begin)
18+
while end < len_A:
19+
# print(end)
20+
if A[end] not in current_dictionary:
21+
current_dictionary[ A[end] ] = 1
22+
count_slice += 1
23+
# print('count plus 1')
24+
if count_slice > 1_000_000_000:
25+
return 1_000_000_000
26+
end +=1
27+
else:
28+
# the same value happens (begin +=1)
29+
current_dictionary = {}
30+
break
31+
32+
# important:
33+
# begin will plus 1, and so we reset 'current_dictionary'
34+
current_dictionary = {}
35+
36+
return count_slice

CountTriangles.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):
5+
# write your code in Python 3.6
6+
7+
my_list = A
8+
my_list.sort()
9+
# print(my_list)
10+
11+
if len(A) < 3:
12+
return 0
13+
14+
count_triangular = 0
15+
16+
for index_one in range( len(A) -2):
17+
index_two = index_one + 1
18+
index_three = index_one + 2
19+
while (index_two< len(A)-1):
20+
if ( index_three<len(A) ) and (my_list[index_one] + my_list[index_two] > my_list[index_three]) :
21+
index_three +=1
22+
else:
23+
count_triangular += (index_three - index_two - 1)
24+
index_two += 1
25+
26+
return count_triangular

0 commit comments

Comments
 (0)