Python Nozioni di baseMedio

Potenza di un numero utilizzando la ricorsione

Guida dettagliata e implementazione Python per il problema "Potenza di un numero utilizzando la ricorsione".

Dichiarazione del problema

Medio

Scrivi una funzione power(base, exp) che calcoli base elevato alla potenza exp utilizzando la ricorsione. La funzione dovrebbe accettare due numeri interi base e exp come input e restituire il risultato come numero intero. Supponiamo che exp sia sempre non negativo.

Vincoli
  • 0 <= exp <= 20
  • -10 <= base <= 10

Esempi

Example 1
Input
base = 2, exp = 10
Output
1024
Explanation

2^10 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 1024

Example 2
Input
base = 3, exp = 4
Output
81
Explanation

3^4 = 3 * 3 * 3 * 3 = 81

Example 3
Input
base = 5, exp = 0
Output
1
Explanation

Any number raised to the power 0 is 1

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche della ricorsione 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.