: 문제를 보고는 쉽게 생각했다가 생각보다 시간이 오래걸려서 시간내에 제출을 못할 뻔했다. 역시 손으로 직접해봐야 아는 것 같다. 잘 안다고 생각했더라도 코드로 작성해내는 것은 좀 더 단련되어야 하는 부분이라고 느꼈다.
print("1번 답안")
tmp = []
print(tmp)
선언만 하면 되는....단순 문제!
def data_type(a):
if isinstance(a,list):
typeStr = []
typeStr.append(type(a).__name__)
for i in a:
typeStr.append(data_type(i))
else:
typeStr = type(a).__name__
return typeStr
print("2번 답안")
print(data_type([2,3,[2,'hello']]))
type(a).__name__ : 기존 type(a)함수는 <class 'int'> 와 같은 형식으로 값을 반환하는데, 해당 기능을 통해 데이터 타입의 이름만 받아오도록 했다.
2번 답안
['list', 'int', 'int', ['list', 'int', 'str']]
하지만...리스트의 경우2번 답안처럼 나오게 되버렸다. 그외의 경우에는 괜찮았음!
import math
def calc_tips():
totalPrice = 51
tipsPercent = 10 #디폴트 10%
tips = totalPrice * tipsPercent * 0.01
tips = math.ceil(tips) #올림 처리
print(tips)
print("3번 답안")
calc_tips()
math 모듈의 ceil 함수를 통해 소수점은 올림처리했다.
def search_target(sentence, target):
target = target
targetIndex = 0
cnt = 0
targetList = []
while sentence.find(target,targetIndex) != -1 :
targetIndex = sentence.find(target,targetIndex + 1)
targetList.append(targetIndex)
cnt += 1
targetIndex += 1
print(cnt, ',', targetList)
print("4번 답안")
sentence = "Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex."
target = "than"
search_target(sentence, target)
def div_ab(a,b):
share = 0
remainder = a
while remainder >= b:
remainder = remainder - b
share += 1
print(f'{a}나누기{b}의 결과 : 몫{share}, 나머지{remainder}')
print("5번 답안")
a = 3
b = 2
div_ab(a, b)
코드를 입력하세요