[COS Pro python 1급] p진법 to q진법

2023. 3. 14. 18:04자격증/COS Pro 1급 Python

D-4

 

def toten(n,p):
	ten=0
	for i in range(len(n)):
		ten+=int(n[-(i+1)])*(p**i)
	return ten

def toq(n,q):
	qnum=''
	while n>0:
		mok,namuji=divmod(n,q)
		qnum+=str(namuji)
		n=mok
	return qnum[::-1]

def solution(s1, s2, p, q):
	answer = ''
	ten1=toten(s1,p)
	ten2=toten(s2,p)
	sum=ten1+ten2

	answer=toq(sum,q)
	return answer

문자열 str뒤집기; str[::-1]

divmod(a, b)함수; a를 b로 나눈 몫과 나머지를 튜플형식으로 반환,