[수업출처] 숙명여자대학교 소프트웨어학부 유석종교수님 자료구조 수업 & [파이썬으로 배우는 자료구조 프로그래밍]
* 수강생이 혼자 푼 답을 기록한 것으로 정답이 아닐 수 있습니다 😁
1.
l = [21, 7, 40, 29, 11, 5, 90, 78, 64, 15, 88]
max = 0
min = 1000
for i in range(len(l)):
if l[i] > max:
max = l[i]
if l[i] < min:
min = l[i]
print((min, max))
(5, 90)
2.
def mul(n, m):
l = []
for i in range(1, n):
if i % m == 0:
l.append(i)
return l
print(mul(10, 3))
[3, 6, 9]
3.
def gcd(a, b):
while a % b != 0:
a, b = b, a % b
return b
def fraction(num):
down = 10**len(str(num))
up = num * down
common = gcd(up, down)
new_up = int(up // common)
new_down = int(down // common)
print(new_up, '/', new_down)
fraction(3.14)
# 157/50
4.
def palindrome(string):
while len(string) > 1:
if string[0] != string[-1]:
return False
else:
string = string[1:-1]
return True
palindrome('abnnba')
# True
palindrome('sdfg')
# False
5.
l = [1]
num = 1
for i in range(14):
num += 3
l.append(num)
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 35, 37, 40, 43]
6.
l = []
num = 1
for i in range(15):
num += i
l.append(num)
[1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79, 92, 106]
7.
scores = [[75, 90, 85], [60, 100, 75], [90, 70, 80]]
for score in scores:
for i in score:
print(i, end = ',')
print()
75,90,85,
60,100,75,
90,70,80,
8.
p = [(9, 4), (5, 3), (4, 2)]
q = [(7, 3), (2, 2)]
add_list = []
mul_list = []
def add(p, q):
while p and q:
coef1, exp1 = p[0]
coef2, exp2 = q[0]
if exp1 > exp2:
add_list.append(p.pop(0))
elif exp1 < exp2:
add_list.append(q.pop(0))
else:
add_list.append((coef1 + coef2, exp1))
p.pop(0)
q.pop(0)
for coef, exp in p:
add_list.append((coef, exp))
for coef, exp in q:
add_list.append((coef, exp))
return add_list
def mul(p, q):
while p and q:
coef1, exp1 = p[0]
for coef, exp in q:
mul_list.append((coef1*coef, exp1+exp))
p.pop(0)
return mul_list
add(p, q)
print('P + Q = ', add_list)
# P + Q = [(9, 4), (12, 3), (6, 2)]
mul(p, q)
print('P * Q = ', mul_list)
# P * Q = [(63, 7), (18, 6), (35, 6), (10, 5), (28, 5), (8, 4)]
- 곱셈 후 지수가 같은 항 합치는 방법 생각해야함!
'Software > Data Structures' 카테고리의 다른 글
[Python] 2. Python Data Type (0) | 2022.04.16 |
---|---|
[Python] 1. Introduction (0) | 2022.03.08 |
[자료구조] 탐색 (0) | 2021.04.20 |
[자료구조] 연결 리스트 (0) | 2021.04.20 |
[자료구조] 선형 자료구조 (0) | 2021.04.18 |