Jump to Python (6)

GreenBean·2021년 3월 30일
0

Jump to Python

목록 보기
6/7
post-thumbnail

Jump to Python - 6 [마지막]

점프 투 파이썬의 예제를 푼 뒤, 그것에 대해 정리한 포스팅

새 창을 통해 점프 투 파이썬 으로 이동


08장 | 종합문제

✔ Q1. 문자열 바꾸기

# 1.
a = "a:b:c:d"
b = a.replace(":", "#")
print(b)

# 1.result
a#b#c#d

✔ Q2. 딕셔너리 값 추출하기

# 1.
a = {"A":90, "B":80}
print(a.get("C", 70))

# 1.result
70

✔ Q4. 리스트 총합 구하기

# 1.
A = [20, 55, 67, 82, 45, 33, 90, 87, 100, 25]

def sum(num):
    total = 0
    for i in num:
        if i >= 50:
            total += i
        else:
            pass
    print(total)

sum(A)

# 1.result
481
# 2.
A = [20, 55, 67, 82, 45, 33, 90, 87, 100, 25]

result = 0
for num in A:
    if num >= 50:
        result += num
print(result)

# 2.result
481

✔ Q5. 피보나치 함수

# 1.
def fib(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-2) + fib(n-1)

for i in range(10):
    print(fib(i), end=" ")
    
# 1.result
0 1 1 2 3 5 8 13 21 34 

✔ Q6. 숫자의 총합 구하기

# 1.
a = input("Enter the number : ")
nums = a.split(",")

sum = 0
for num in nums:
    sum += int(num)
print(sum)

# 1.result
Enter the number : 3, 6, 7, 14
30

✔ Q7. 한 줄 구구단

# 1.
a = input("Enter the number you want to create a multiplication table : ")

for num in range(1, 10):
    if 2 <= int(a) < 10:
        print(int(a)*num, end=" ")
    else:
        print("Please enter a number between 2 and 9.")
        break

# 1.result
Enter the number you want to create a multiplication table : 7
7 14 21 28 35 42 49 56 63 

✔ Q8. 역순 저장

# 1.
a = open('abc.txt', 'r')
content = a.readlines()
a.close()

content.reverse()

a = open('abc.txt', 'w')
for sentence in content:
    sentence = sentence.strip()
    a.write(sentence)
    a.write("\n")
a.close()

# 1.result
(in 'abc.txt')
EEE
DDD
CCC
BBB
AAA

✔ Q9. 평균 값 구하기

# 1.
a = open('sample.txt')
content = a.readlines()
a.close()

sum = 0
for score in content:
    num = int(score)
    sum += num
avg = sum / len(content)

a = open("result.txt", "w")
a.write(str(avg))
a.close()

# 1.result
(in 'result.txt')
79.0

✔ Q10. 사칙연산 계산기

# 1.
class Calculator(object):
    def __init__(self, a):
        self.a = a
    def sum(self):
        total = 0
        for num in self.a:
            total += int(num)
        print(total)
    def avg(self):
        total = 0
        for num in self.a:
            total += int(num)
        average = total / len(self.a)
        print(average)

call = Calculator([1, 2, 3, 4, 5])
call.sum()
call.avg()

# 1.result
15
3.0
# 2.
class Calculator(object):
    def __init__(self, *args):
        self.args = args
    def sum(self):
        total = 0
        for num in self.args:
            total += int(num)
        print(total)
    def avg(self):
        total = 0
        for num in self.args:
            total += int(num)
        average = total / len(self.args)
        print(average)

call = Calculator(1, 2, 3, 4, 5)
call.sum()
call.avg()

# 2.result
15
3.0

✔ Q13. DashInsert 함수

# 1.
a = "4546793"
nums = list(map(int, a))
result = []

for i, num in enumerate(nums):
    result.append(str(num))
    if i < len(nums)-1:
        odd = num % 2 == 1
        nex_odd = nums[i+1] % 2 == 1
        if odd and nex_odd:
            result.append("-")
        elif not odd and not nex_odd:
            result.append("*")

print("".join(result))

# 1.result
454*67-9-3
profile
🌱 Backend-Dev | hwaya2828@gmail.com

0개의 댓글