Skip to content

Commit ad23480

Browse files
committed
finish ch3
1 parent 8d33752 commit ad23480

13 files changed

+261
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import string
2+
3+
def hash_func(astring):
4+
s = 0
5+
for one in astring:
6+
if one in string.whitespace:
7+
continue
8+
s = s + ord(one)
9+
return s
10+
11+
def find_anagram_hash_function(word1, word2):
12+
return hash_func(word1) == hash_func(word2)
13+
14+
def test_find_anagram_hash_function():
15+
word1 = 'buffy'
16+
word2 = 'bffyu'
17+
word3 = 'bffya'
18+
assert(find_anagram_hash_function(word1, word2) == True)
19+
assert(find_anagram_hash_function(word1, word3) == False)
20+
print("테스트 통과!")
21+
22+
if __name__ == '__main__':
23+
test_find_anagram_hash_function()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections import Counter, defaultdict
2+
3+
def find_dice_probabilities(S, n_faces=6):
4+
if S > 2 * n_faces or S < 2:
5+
return None
6+
7+
cdict = Counter()
8+
ddict = defaultdict(list)
9+
10+
for dice1 in range(1, n_faces+1):
11+
for dice2 in range(1, n_faces+1):
12+
t = [dice1, dice2]
13+
cdict[dice1+dice2] += 1
14+
ddict[dice1+dice2].append(t)
15+
16+
return [cdict[S], ddict[S]]
17+
18+
def test_find_dice_probabilities():
19+
n_faces = 6
20+
S = 5
21+
results = find_dice_probabilities(S, n_faces)
22+
print(results)
23+
assert(results[0] == len(results[1]))
24+
print("테스트 통과!")
25+
26+
if __name__ == '__main__':
27+
test_find_dice_probabilities()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import string
2+
3+
def delete_unique_word(str1):
4+
table_c = { key: 0 for key in string.ascii_lowercase }
5+
for i in str1:
6+
table_c[i] += 1
7+
for key, value in table_c.items():
8+
if value > 1:
9+
str1 = str1.replace(key, "")
10+
return str1
11+
12+
def test_delete_unique_word():
13+
str1 = "google"
14+
assert(delete_unique_word(str1) == 'le')
15+
print('테스트 통과!')
16+
17+
if __name__ == '__main__':
18+
test_delete_unique_word()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def remove_dup(l1):
2+
""" 리스트의 중복된 항목을 제거한 후 반환한다. """
3+
return list(set(l1))
4+
5+
def intersection(l1, l2):
6+
""" 교집합 결과를 반환한다. """
7+
return list(set(l1) & set(l2))
8+
9+
def union(l1, l2):
10+
""" 합집합 결과를 반환한다. """
11+
return list(set(l1) | set(l2))
12+
13+
def test_sets_operations_with_lists():
14+
l1 = [1, 2, 3, 4, 5, 5, 9, 11, 11, 15]
15+
l2 = [4, 5, 6, 7, 8]
16+
l3 = []
17+
assert(remove_dup(l1) == [1, 2, 3, 4, 5, 9, 11, 15])
18+
assert(intersection(l1, l2) == [4,5])
19+
assert(union(l1, l2) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15])
20+
assert(remove_dup(l3) == [])
21+
assert(intersection(l3, l2) == l3)
22+
assert(sorted(union(l3, l2)) == sorted(l2))
23+
print("테스트 통과!")
24+
25+
if __name__ == '__main__':
26+
test_sets_operations_with_lists()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def set_operations_with_dict():
2+
pairs = [('a', 1), ('b',2), ('c',3)]
3+
d1 = dict(pairs)
4+
print(d1)
5+
6+
d2 = {'a':1, 'c':2, 'd':3, 'e':4}
7+
print(d2)
8+
9+
union = d1.keys() & d2.keys()
10+
print(union)
11+
12+
union_items = d1.items() & d2.items()
13+
print(union_items)
14+
15+
subtraction1 = d1.keys() - d2.keys()
16+
print(subtraction1)
17+
18+
subtraction2 = d2.keys() - d1.keys()
19+
print(subtraction2)
20+
21+
subtraction_items = d1.items() - d2.items()
22+
print(subtraction_items)
23+
24+
""" 딕셔너리의 특정 키를 제외한다. """
25+
d3 = {key: d2[key] for key in d2.keys() - {'c', 'd'}}
26+
print(d3)
27+
28+
if __name__ == '__main__':
29+
set_operations_with_dict()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import timeit
2+
import random
3+
4+
for i in range(10000,1000001,20000):
5+
t = timeit.Timer("random.randrange(%d) in x" % i,
6+
"from __main__ import random, x")
7+
x = list(range(i)) # 리스트
8+
lst_time = t.timeit(number=1000)
9+
x = {j:None for j in range(i)} # 딕셔너리
10+
d_time = t.timeit(number=1000)
11+
print("%d,%10.3f,%10.3f" % (i, lst_time, d_time))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import defaultdict
2+
3+
def defaultdict_example():
4+
pairs = {('a', 1), ('b',2), ('c',3)}
5+
6+
d1 = {}
7+
for key, value in pairs:
8+
if key not in d1:
9+
d1[key] = []
10+
d1[key].append(value)
11+
print(d1)
12+
13+
d2 = defaultdict(list)
14+
for key, value in pairs:
15+
d2[key].append(value)
16+
print(d2)
17+
18+
if __name__ == '__main__':
19+
defaultdict_example()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import OrderedDict
2+
3+
def orderedDict_example():
4+
pairs = [('c', 1), ('b',2), ('a',3)]
5+
d1 = {}
6+
for key, value in pairs:
7+
if key not in d1:
8+
d1[key] = []
9+
d1[key].append(value)
10+
for key in d1:
11+
print(key, d1[key])
12+
13+
d2 = OrderedDict(pairs)
14+
for key in d2:
15+
print(key, d2[key])
16+
17+
if __name__ == '__main__':
18+
orderedDict_example()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from collections import Counter
2+
3+
def counter_example():
4+
''' 항목의 발생 횟수를 매핑하는 딕셔너리를 생성한다. '''
5+
seq1 = [1, 2, 3, 5, 1, 2, 5, 5, 2, 5, 1, 4]
6+
seq_counts = Counter(seq1)
7+
print(seq_counts)
8+
9+
''' 항목의 발생 횟수를 수동으로 갱신하거나, update() 메서드를 사용할 수 있다. '''
10+
seq2 = [1, 2, 3]
11+
seq_counts.update(seq2)
12+
print(seq_counts)
13+
14+
seq3 = [1, 4, 3]
15+
for key in seq3:
16+
seq_counts[key] += 1
17+
print(seq_counts)
18+
19+
''' a+b, a-b와 같은 셋 연산을 사용할 수 있다. '''
20+
seq_counts_2 = Counter(seq3)
21+
print(seq_counts_2)
22+
print(seq_counts + seq_counts_2)
23+
print(seq_counts - seq_counts_2)
24+
25+
if __name__ == '__main__':
26+
counter_example()

0 commit comments

Comments
 (0)