Skip to content

Commit

Permalink
[알고리즘]이진변환반복하기-개선된풀이
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyeongchoi committed May 18, 2023
1 parent 3c4bec3 commit 93dd250
Showing 1 changed file with 7 additions and 32 deletions.
39 changes: 7 additions & 32 deletions argorithm/이진변환반복하기.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
def removeZero(val):
removedZeroCnt = 0
for index, i in enumerate(val):
if i == '0':
removedZeroCnt += 1
return removedZeroCnt, ''.join(val).replace('0', '')

def transform(val):
namosi = []
while val != 1:
rest = val % 2
val = val // 2
namosi.append(str(rest))
namosi.append('1')
namosi.reverse()
return namosi

def solution(s):
answer = []
zeroCnt = 0
transCnt = 0
listedVal = list(s)
while listedVal:
while True:
if s == '1':
break
# 0 제거
removedZeroCnt, val = removeZero(listedVal)
print(removedZeroCnt, val)
zeroCnt += removedZeroCnt
# 0 제거 후 길이 확인
removedStringLen = len(val)
print(removedStringLen)
removedZero = s.count('1')
zeroCnt += len(s) - removedZero
# 이진 변환
listedVal = transform(removedStringLen)
s = bin(removedZero)[2:]
transCnt += 1
if len(listedVal) == 1 and listedVal[0] == "1":
break
answer.append(transCnt)
answer.append(zeroCnt)
return answer
return [transCnt, zeroCnt]

if __name__ == '__main__':
result = solution("110010101001")
Expand Down

0 comments on commit 93dd250

Please sign in to comment.