Python Nozioni di baseFacile

Conversione da ottale a binario

Guida dettagliata e implementazione Python per il problema della "conversione da ottale a binario".

Dichiarazione del problema

Facile

Scrivi una funzione octal_to_binary(octal_str) che accetta una stringa che rappresenta un numero ottale (base 8, cifre 0-7) e restituisce una stringa che rappresenta il suo equivalente binario (base 2). Non includere zeri iniziali nell'output (ad eccezione dell'input '0').

Suggerimento: convertire ciascuna cifra ottale nel suo equivalente binario a 3 bit e concatenare.

Vincoli
  • 1 <= len(octal_str) <= 10
  • octal_str contains only digits '0' through '7'

Esempi

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

1 = 001, 2 = 010. Concatenate: 001010. Remove leading zeros: 1010.

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

7 = 111, 7 = 111. Concatenate: 111111.

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

3 = 011, 1 = 001, 2 = 010. Concatenate: 011001010. Remove leading zero: 11001010.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Numbers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.