Python Nozioni di baseFacile

Conversione da decimale a ottale

Guida dettagliata e implementazione Python per il problema "Conversione da decimale a ottale".

Dichiarazione del problema

Facile

Scrivi una funzione decimal_to_octal(n) che accetta un intero non negativo n e restituisce una stringa che rappresenta il suo equivalente ottale (base 8). Non includere zeri iniziali (ad eccezione dell'input 0, che dovrebbe restituire "0").

Per convertire il numero decimale in ottale, dividi ripetutamente il numero per 8 e raccogli i resti in ordine inverso.

Vincoli
  • 0 <= n <= 10^6

Esempi

Example 1
Input
decimal_to_octal(100)
Output
'144'
Explanation

100 ÷ 8 = 12 remainder 4, 12 ÷ 8 = 1 remainder 4, 1 ÷ 8 = 0 remainder 1. Reading remainders bottom-up: 144.

Example 2
Input
decimal_to_octal(15)
Output
'17'
Explanation

15 ÷ 8 = 1 remainder 7, 1 ÷ 8 = 0 remainder 1. Result: 17.

Example 3
Input
decimal_to_octal(8)
Output
'10'
Explanation

8 ÷ 8 = 1 remainder 0, 1 ÷ 8 = 0 remainder 1. Result: 10.

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.