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 2fdc3a8 commit 3c4bec3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion argorithm/.idea/argorithm.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion argorithm/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions argorithm/이진변환반복하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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:
# 0 제거
removedZeroCnt, val = removeZero(listedVal)
print(removedZeroCnt, val)
zeroCnt += removedZeroCnt
# 0 제거 후 길이 확인
removedStringLen = len(val)
print(removedStringLen)
# 이진 변환
listedVal = transform(removedStringLen)
transCnt += 1
if len(listedVal) == 1 and listedVal[0] == "1":
break
answer.append(transCnt)
answer.append(zeroCnt)
return answer

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

0 comments on commit 3c4bec3

Please sign in to comment.