-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch11_exercise.py
More file actions
197 lines (146 loc) · 4.86 KB
/
ch11_exercise.py
File metadata and controls
197 lines (146 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from typing import TextIO, Dict
from io import StringIO
def obser_bird(observation_file: TextIO) -> set[str]:
birds_observed = set()
for line in observation_file:
bird = line.strip()
birds_observed.add(bird)
return birds_observed
with open('test5.txt') as ob_file:
birds_observed = obser_bird(ob_file)
print(birds_observed)
birds_observed.add('hello')
for spec in birds_observed:
print(spec)
bird_to_bservations = {'canada goose':3, 'northern fulmar':1}
print(bird_to_bservations)
bird_to_bservations['snow goose'] = 33
bird_to_bservations['eagle'] = 999
print(bird_to_bservations)
for i in bird_to_bservations:
print(bird_to_bservations[i], i)
for key, val in bird_to_bservations.items():
print('같이',key, val)
fish_observe = {'연어':3,'참가자미': 7, '기름가자미':1, '넙치':3}
observation_to_fish = {}
for fish, key in fish_observe.items():
if key in observation_to_fish:
observation_to_fish[key].append(fish) #이미 해당 key에 값이 있을경우
else:
observation_to_fish[key] = [fish] #key에 값 없을 경우
print(observation_to_fish)
for index in observation_to_fish:
print(index,':',end = '')
for fish in observation_to_fish[index]:
print(fish,end = ' ')
print()
print('-----------------------------------------------------')
def find_dups(input_list :list) -> set:
element = set()
dups = set()
for index in input_list:
intial = len(element)
element.add(index)
later = len(element) #set은 중복 안되니까 이미 있는 값 들어오면 윗줄에서 add안됨 -> intial과 later값 같다
if intial == later:
dups.add(index)
return dups
num = [3,5,3,1,2,4,1,9]
output = find_dups(num)
print(output)
def find_name(input_name: list) -> set:
element = set()
dups = set()
for index in input_name:
check = len(element)
element.add(index)
check2 = len(element)
if check == check2:
dups.add(index)
return dups
name = ['kim','chu','lee','kim','jeong']
output2 = find_name(name)
print(output2)
def pairs(set1: set, set2: set) -> (set, set):
range_loop = len(set1)
pair = set()
for index in range(range_loop):
store_male = set1.pop()
store_female = set2.pop()
pair.add((store_male, store_female))
return pair
males = {'a','v','s','d'}
females = {'1','2','3','4'}
sum = pairs(males,females)
print(sum)
def find_author(filename : str) -> list:
names = set()
with open(filename,'r') as data:
for line in data:
if line.lower().startswith('author'):
name = line[6:].strip()
names.add(name) #list에는 add를 못쓴다 append는 가능
return names
name_output = find_author('pdb.txt')
print(name_output)
def count_values(input: dict) -> int:
set_to_dic = set(input.values()) #그냥 딕셔너리를 set으로 바꿔버림
return len(set_to_dic)
only_int = count_values({'red':1, 'green':1, 'blue':2})
print(only_int)
def return_less(input: dict) -> str: #애는 또 c처럼 푸네 // get은 key넣으면 값 나옴 -> 반대는 안됨
small = 1
for index in input:
temp = input[index] #index가 키 // dic[incex]가 값
if small > temp:
small = temp
return small
test = {'n':0.44, 'p':0.21, 'm':0.03, 'ne':1.3}
input_min = return_less(test)
print(input_min)
def count_duplication(input: dict) -> int: #list로 만들어서 반복문 count로 비교도 가능
output = 0
set_store = set()
for index in input:
intial = len(set_store)
set_store.add(input[index])
check = len(set_store)
if intial == check:
output=output+1
return output
dups_output = count_duplication({'1':'hello', '2':'good', '3':'hello', '4':'soso','5':'soso'})
print(dups_output)
#-----------------연습문제 8번 할 차례---------------------
def is_balanced(color: dict) -> bool:
sum = 0.0
for index in color:
sum = sum + color[index]
if sum == 1.0:
return_val = True
else:
return_val = False
return return_val
input_bool = is_balanced({'R':0.3, 'G':0.5, "b":0.1})
print(input_bool)
square_vector1 = {0:1,2:5, 6:3}
square_vector2 = {0:3, 2:1, 7:4}
def sparse_add(s_v1: dict, s_v2: dict) -> dict:
sum_dic = s_v1.copy()
for i in s_v2:
if i in sum_dic:
sum_dic[i] = sum_dic[i] + s_v2[i]
else:
sum_dic[i] = s_v2[i]
return sum_dic
output_add = sparse_add(square_vector1,square_vector2)
print(output_add)
def sparse_dot(s_v1: dict, s_v2: dict) -> dict:
dot_sum = s_v1.copy()
total = 0
for i in s_v2:
if i in dot_sum:
dot_sum[i] = dot_sum[i] * s_v2[i]
total = total + dot_sum[i]
return total
output_dot = sparse_dot(square_vector1, square_vector2)
print(output_dot)