8000 소스 코드 등록 · Algorithm-box/080235@94d85e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94d85e7

Browse files
author
gilbutitbook
committed
소스 코드 등록
0 parents  commit 94d85e7

File tree

431 files changed

+20057
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

431 files changed

+20057
-0
lines changed

Chapter1/Better way1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
###
2+
### 아이템 1
3+
###
4+
import sys
5+
print(sys.version_info)
6+
print(sys.version)

Chapter1/Better way10.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
###
2+
### 아이템 10
3+
###
4+
5+
fresh_fruit = {
6+
'사과': 10,
7+
'바나나': 8,
8+
'레몬': 5,
9+
}
10+
11+
def make_lemonade(count):
12+
n = 1
13+
print(f'레몬 {count*n} 개로 레모네이드 {count//n} 개를 만듭니다.')
14+
fresh_fruit['레몬'] -= (count * n)
15+
print(f'레몬이 {fresh_fruit["레몬"]} 개 남았습니다.')
16+
17+
def out_of_stock():
18+
print(f'제료가 부족합니다. 재료를 보충해 주세요.')
19+
20+
count = fresh_fruit.get('레몬', 0)
21+
if count:
22+
make_lemonade(count)
23+
else:
24+
out_of_stock()
25+
26+
fresh_fruit['레몬'] = 5 # 테스트를 위해 갯수 리셋
27+
if count := fresh_fruit.get('lemon', 0):
28+
make_lemonade(count)
29+
else:
30+
out_of_stock()
31+
32+
def make_cider(count):
33+
n = 4
34+
35+
print(f'사과 {count} 개로 사과주스 {count//n} 개를 만듭니다.')
36+
fresh_fruit['사과'] -= (n *(count//n))
37+
print(f'사과가 {fresh_fruit["사과"]} 개 남았습니다.')
38+
39+
fresh_fruit['사과'] = 10 # 테스트를 위해 갯수 리셋
40+
41+
count = fresh_fruit.get('사과', 0)
42+
if count >= 4:
43+
make_cider(count)
44+
else:
45+
out_of_stock()
46+
47+
48+
fresh_fruit['사과'] = 10 # 테스트를 위해 갯수 리셋
49+
50+
if (count := fresh_fruit.get('사과', 0)) >= 4:
51+
make_cider(count)
52+
else:
53+
out_of_stock()
54+
55+
def slice_bananas(count):
56+
print(f'바나나 {count} 개를 슬라이스합니다.')
57+
fresh_fruit['바나나'] -= count
58+
return count
59+
60+
class OutOfBananas(Exception):
61+
pass
62+
63+
def make_smoothies(count):
64+
n=2
65+
if count > n:
66+
print(f'바나나 슬라이스 {count} 개로 스무디 {count//n} 개를 만듭니다.')
67+
print(f'바나나가 {fresh_fruit["바나나"]} 개 남았습니다.')
68+
else:
69+
raise OutOfBananas
70+
71+
pieces = 0
72+
count = fresh_fruit.get('바나나', 0)
73+
if count >= 2:
74+
pieces = slice_bananas(count)
75+
76+
try:
77+
smoothies = make_smoothies(pieces)
78+
except OutOfBananas:
79+
out_of_stock()
80+
81+
fresh_fruit['바나나'] = 8 # 테스트를 위해 갯수 리셋
82+
83+
count = fresh_fruit.get('바나나', 0)
84+
if count >= 2:
85+
pieces = slice_bananas(count)
86+
else:
87+
pieces = 0
88+
89+
fresh_fruit['바나나'] = 8 # 테스트를 위해 갯수 리셋
90+
91+
pieces = 0
92+
if (count := fresh_fruit.get('바나나', 0)) >= 2:
93+
pieces = slice_bananas(count)
94+
95+
try:
96+
smoothies = make_smoothies(pieces)
97+
except OutOfBananas:
98+
out_of_stock()
99+
100+
fresh_fruit['바나나'] = 8 # 테스트를 위해 갯수 리셋
101+
102+
if (count := fresh_fruit.get('바나나', 0)) >= 2:
103+
pieces = slice_bananas(count)
104+
else:
105+
pieces = 0
106+
107+
try:
108+
smoothies = make_smoothies(pieces)
109+
except OutOfBananas:
110+
out_of_stock()
111+
112+
count = fresh_fruit.get('banana', 0)
113+
114+
fresh_fruit['바나나'] = 8 # 테스트를 위해 갯수 리셋
115+
116+
if count >= 2:
117+
pieces = slice_bananas(count)
118+
to_enjoy = make_smoothies(pieces)
119+
else:
120+
count = fresh_fruit.get('사과', 0)
121+
if count >= 4:
122+
to_enjoy = make_cider(count)
123+
else:
124+
count = fresh_fruit.get('레몬', 0)
125+
if count:
126+
to_enjoy = make_lemonade(count)
127+
else:
128+
to_enjoy = '아무것도 없음'
129+
130+
if (count := fresh_fruit.get('바나나', 0)) >= 2:
131+
pieces = slice_bananas(count)
132+
to_enjoy = make_smoothies(pieces)
133+
elif (count := fresh_fruit.get('사과', 0)) >= 4:
134+
to_enjoy = make_cider(count)
135+
elif count := fresh_fruit.get('레몬', 0):
136+
to_enjoy = make_lemonade(count)
137+
else:
138+
to_enjoy = '아무것도 없음'
139+
140+
import random
141+
142+
def pick_fruit():
143+
if random.randint(1,10) > 2: # 80% 확률로 새 과일 보충
144+
return {
145+
'사과': random.randint(0,10),
146+
'바나나': random.randint(0,10),
147+
'레몬': random.randint(0,10),
148+
}
149+
else:
150+
return None
151+
152+
def make_juice(fruit, count):
153+
if fruit == '사과':
154+
return [('사과주스', count/4)]
155+
elif fruit == '바나나':
156+
return [('바나나스무디',count/2)]
157+
elif fruit == '레몬':
158+
return [('레모네이드',count/1)]
159+
else:
160+
return []
161+
162+
163+
bottles = []
164+
fresh_fruit = pick_fruit()
165+
while fresh_fruit:
166+
for fruit, count in fresh_fruit.items():
167+
batch = make_juice(fruit, count)
168+
bottles.extend(batch)
169+
fresh_fruit = pick_fruit()
170+
171+
print(bottles)
172+
173+
bottles = []
174+
while True: # 무한루프
175+
fresh_fruit = pick_fruit()
176+
if not fresh_fruit: # 중간에서 끝내기
177+
break
178+
179+
for fruit, count in fresh_fruit.items():
180+
batch = make_juice(fruit, count)
181+
bottles.extend(batch)
182+
183+
print(bottles)
184+
185+
bottles = []
186+
while fresh_fruit := pick_fruit():
187+
for fruit, count in fresh_fruit.items():
188+
batch = make_juice(fruit, count)
189+
bottles.extend(batch)
190+
191+
print(bottles)

Chapter1/Better way3.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
###
2+
### 아이템 3
3+
###
4+
a = b'h\x65llo'
5+
print(list(a))
6+
print(a)
7+
8+
a = 'a\u0300 propos'
9+
print(list(a))
10+
print(a)
11+
12+
def to_str(bytes_or_str):
13+
if isinstance(bytes_or_str, bytes):
14+
value = bytes_or_str.decode('utf-8')
15+
else:
16+
value = bytes_or_str
17+
return value # str 인스턴스
18+
19+
print(repr(to_str(b'foo')))
20+
print(repr(to_str('bar')))
21+
print(repr(to_str(b'\xed\x95\x9c'))) # UTF-8에서 한글은 3바이트임
22+
23+
def to_bytes(bytes_or_str):
24+
if isinstance(bytes_or_str, str):
25+
value = bytes_or_str.encode('utf-8')
26+
else:
27+
value = bytes_or_str
28+
return value # bytes 인스턴스
29+
30+
print(repr(to_bytes(b'foo')))
31+
print(repr(to_bytes('bar')))
32+
print(repr(to_bytes('한글')))
33+
34+
print(b'one' + b'two')
35+
print('one' + 'two')
36+
37+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
38+
# b'one' + 'two' # TypeError
39+
40+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
41+
# 'one' + b'two' # TypeError
42+
43+
44+
assert b'red' > b'blue'
45+
assert 'red' > 'blue'
46+
47+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
48+
# assert 'red' > b'blue' # TypeError
49+
50+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
51+
#assert b'blue' < 'red' # TypeError
52+
53+
print(b'foo' == 'foo') # 항상 false
54+
55+
print(b'red %s' % b'blue') # 타입이 같은 형식화문자열 사용
56+
print('red %s' % 'blue')
57+
58+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
59+
#print(b'red %s' % 'blue') # TypeError
60+
61+
print('red %s' % b'blue') # 생각과 다르게 작동
62+
63+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
64+
#with open('data.bin', 'w') as f: # TypeError
65+
# f.write(b'\xf1\xf2\xf3\xf4\xf5')
66+
67+
with open('data.bin', 'wb') as f:
68+
f.write(b'\xf1\xf2\xf3\xf4\xf5')
69+
70+
# 오류가 나는 부분. 오류를 보고 싶으면 커멘트를 해제할것
71+
#with open('data.bin', 'r') as f: # UnicodeDecodeError
72+
# data = f.read()
73+
74+
with open('data.bin', 'rb') as f:
75+
data = f.read()
76+
77+
with open('data.bin', 'r', encoding='cp1252') as f:
78+
data = f.read()
79+
80+
assert data == 'ñòóôõ'
81+
82+
# 시스템 인코딩 검사: python3 -c 'import locale; print(locale.getpreferredencoding())'

0 commit comments

Comments
 (0)
0