Python Nozioni di baseFacile

Conversione da esadecimale a decimale

Guida dettagliata e implementazione Python per il problema della "conversione da esadecimale a decimale".

Dichiarazione del problema

Facile

Scrivi una funzione hex_to_decimal(hex_str) che accetta una stringa che rappresenta un numero esadecimale (base 16, cifre 0-9 e A-F, senza distinzione tra maiuscole e minuscole) e restituisce il suo equivalente intero decimale (base 10).

In esadecimale, LA=10, SI=11, DO=12, RE=13, MI=14, FA=15. Ogni cifra rappresenta una potenza di 16.

Vincoli
  • 1 <= len(hex_str) <= 8
  • hex_str contains only valid hex characters (0-9, a-f, A-F)

Esempi

Example 1
Input
hex_to_decimal('1A')
Output
26
Explanation

1×16¹ + A(10)×16⁰ = 16 + 10 = 26.

Example 2
Input
hex_to_decimal('FF')
Output
255
Explanation

F(15)×16¹ + F(15)×16⁰ = 240 + 15 = 255.

Example 3
Input
hex_to_decimal('2B')
Output
43
Explanation

2×16¹ + B(11)×16⁰ = 32 + 11 = 43.

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.