일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- codility
- caniuse
- 분할정복
- 알고리즘
- BinaryGap
- programmers
- 1793번
- 11727번
- 9251번
- EBS어학당
- 18406번
- python
- WebSecurityConfigurerAdapter
- SecurityFilterChain
- 신규아이디추천
- 프로그래머스
- 파이썬
- 1992번
- 입이 트이는 영어
- 11047번
- 백준
- 영어
- 1759번
- Spring Security
- 1057번
- Java
- 2630번
- github
- 권주현의 진짜 영국 영어
- 2163번
- Today
- Total
철갑이의 이모저모
[codility] BinaryGap with Python 본문
문제
https://app.codility.com/programmers/lessons/1-iterations/binary_gap/
BinaryGap coding task - Learn to Code - Codility
Find longest sequence of zeros in binary representation of an integer.
app.codility.com
풀이
정수 N을 이진법으로 표현했을 때 연속되는 0의 최대 시퀀스를 구하는 문제이다.
먼저, 정수 N을 이진법으로 표현하기 위해 format() 함수를 사용했다. format() 함수 사용법은 아래 링크에서 확인이 가능하다. 간단하게 설명하자면 format(value, format_spec) 형식으로 사용할 수 있고. format(N, 'b') 는 N을 b(Binary format)으로 변환한다는 의미이다.
https://docs.python.org/3/library/functions.html#format
Built-in Functions — Python 3.10.4 documentation
Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. abs(x) Return the absolute value of a number. The argument may be an integer, a floating poin
docs.python.org
https://docs.python.org/3/library/string.html#formatspec
string — Common string operations — Python 3.10.4 documentation
string — Common string operations Source code: Lib/string.py String constants The constants defined in this module are: string.ascii_letters The concatenation of the ascii_lowercase and ascii_uppercase constants described below. This value is not locale-
docs.python.org
답
def solution(N):
binary = format(N, 'b')
m_count = 0
count = 0
for i in binary:
if i == '1':
if m_count < count :
m_count = count
count = 0
else :
count += 1
return m_count

'알고리즘' 카테고리의 다른 글
[백준] 2309(일곱 난쟁이) with java (2) | 2022.08.15 |
---|---|
[codility] CyclicRotation with Python (0) | 2022.04.14 |
[Programmers] 체육복 with Python (0) | 2022.01.31 |
[Programmers] 숫자 문자열과 영단어 with Python (0) | 2022.01.29 |
[Programmers] 신규 아이디 추천 with Python (0) | 2022.01.29 |