프로그래머스 연습문제 -이진 변환 반복하기 (level2)

j_wisdom_h·2022년 11월 17일
0

CodingTest

목록 보기
10/58
post-thumbnail

프로그래머스 연습문제 -이진 변환 반복하기(level2)


문제설명


제한사항 & 입출력 예


My Solution

def solution(s):
  cnt = 0
  c =0  # 0의 개수
  while s !="1":
    c += s.count("0")
    s = [ i for i in s if i=="1"]
    s = format(bin(len(s))[2:])
    cnt +=1

  return [cnt,c]

공부한 것

1. if, else, for문 한 문장에 쓰기

list1 = [1,2,3]

# if, else 둘 다 있을때
[i*j if i> 1 else 0 for j in list1 for i in list1]
# [0, 2, 3, 0, 4, 6, 0, 6, 9]

2) 만약 else 가 없다면, if 문은 맨 마지막 위치
[i*j for j in list1  for i in list1 if i> 1]
# [2, 3, 4, 6, 6, 9]

2. 2진수 함수 bin

bin(3)
#0b11

bin(-10)
#-0b1010

3. 리스트 중복값 제거하기(set)

a = [1,2,3,3,3,3,4,5,6,7,3,5,6,7]
a = set(a)
print(a)

4. 2개 이상의 원소 모두 삭제하기

arr = [5, 7, 1, 1, 2, 6, 1, 6, 7]

# 삭제할 원소 집합 생성
rm_set = {1, 6}
# 리스트 컴프리헨션 활용: 삭제할 원소 집합 데이터와 일일이 비교
arr_new = [i for i in arr if i not in rm_set]
print(arr_new) # [5, 7, 2, 7]

5. 리스트 원소 제거하기

remove()

lst = ['a', 'b', 'c', 'd']
lst.remove('a') # 'a' 삭제
print(lst) #['b','c','d']
*리스트에 같은 값이 있을 경우에는 앞에 있는 값이 사라진다.

pop()

lst = ['a', 'b', 'c', 'd']
print(lst.pop()) # 'd'

print(lst) #['a','b','c']

lst.pop(1)
print(lst) #['a','c']

del

lst = ['a', 'b', 'c', 'd']
del lst[2] 
print(lst) #['a','b','d']

clear()

lst = ['a', 'b', 'c', 'd']
lst.clear()
print(lst) #[]

6. 원소개수

len()

s="hello world"
print(len(s)) #11

count()

i = [1,1,1,1,3,4,5]
print(i.count(1)) # 4
profile
뚜잇뚜잇 FE개발자

0개의 댓글