RuntimeError
1. RuntimeError: dictionary changed size during iteration
๋ฐ๋ชฉ๋ฌธ์์์ dic๋ set์ ์ฌ์ด์ฆ๊ฐ ๋ฐ๋๋ฉด ๋ฐ์ํ๋ ์๋ฌ์ด๋ค.
๋ค๋ง, break ํ์ฌ for๋ฌธ์์ ๋๊ฐ ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์๋๋ค.
def solution(number, k):
n = len(number)- k
answer = ''
limit = -1
dic = {}
for i, j in enumerate(number):
dic[i] = int(j)
dic = dict(sorted(dic.items(), key=lambda x:x[1], reverse=True))
while n != 0:
for key, value in dic.items():
if key < limit:
del dic[key] # ์๋ฌ ๋ฐ์
continue
if (len(number)-key >= n):
answer += str(value)
limit = key
n -= 1
del dic[key] # ์๋ฌ ๋ฐ์ํ์ง ์์
break
return answer
Leave a comment