35일차 문제

양진혁·2021년 12월 5일
0

문제풀이

find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55

def find_uniq(arr):
  s = set(arr)
  for i in s:
    if arr.count(i) == 1:
      return i

한가지 다른 숫자를 찾는 문제이다. set을 통해서 중복을 제거한 후 반복문을 통해서 i의 갯수를 찾아보고 한개인 경우 리턴한다.

두번째 문제
Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

def count_bits(n):
  return bin(n).count('1')

숫자를 이진수로 바꾼 후 1의 갯수를 찾는 것으로 bin을 사용해서 이진수로 변환시킨 후 count를 통해서 1의 갯수를 찾는다.

0개의 댓글