Python 기본 사항쉬움

2진수를 8진수로 변환

'2진수를 8진수로 변환' 문제에 대한 자세한 가이드 및 Python 구현.

문제 설명

쉬움

2진수를 나타내는 문자열을 취하고 이에 해당하는 8진수(base-8)를 나타내는 문자열을 반환하는 함수 binary_to_octal(binary_str)을 작성하세요. 출력에 선행 0을 포함하지 마십시오(입력 '0' 제외).

힌트: 이진수를 오른쪽에서 왼쪽으로 3개의 그룹으로 그룹화한 다음 각 그룹을 8진수로 변환합니다.

제약
  • 1 <= len(binary_str) <= 20
  • binary_str contains only '0' and '1'

Example 1
Input
binary_to_octal('1010')
Output
'12'
Explanation

Group from right: 001 010. 001 = 1, 010 = 2. So octal is 12. (Binary 1010 = Decimal 10 = Octal 12.)

Example 2
Input
binary_to_octal('111111')
Output
'77'
Explanation

Group: 111 111. 111 = 7, 111 = 7. Octal is 77. (Binary 111111 = Decimal 63 = Octal 77.)

Example 3
Input
binary_to_octal('11001010')
Output
'312'
Explanation

Group from right: 011 001 010. 011=3, 001=1, 010=2. Octal is 312. (Binary 11001010 = Decimal 202 = Octal 312.)

Need a Hint?
세트나 힙과 같은 Numbers 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.