Python Nozioni di baseFacile

Contare il numero di giorni in un dato mese dell'anno

Guida dettagliata e implementazione Python per il problema "Conteggio del numero di giorni in un dato mese dell'anno".

Dichiarazione del problema

Facile

Scrivi una funzione days_in_month(month, year) che accetta un numero intero month (1-12) e un numero intero year e restituisce il numero di giorni in quel mese.

Ricorda:

- Mesi di 31 giorni: gennaio(1), marzo(3), maggio(5), luglio(7), agosto(8), ottobre(10), dicembre(12)

- Mesi di 30 giorni: aprile(4), giugno(6), settembre(9), novembre(11)

- Febbraio(2): 28 giorni normalmente, 29 giorni negli anni bisestili

- Anno bisestile: divisibile per 4, ma non per 100, a meno che non sia divisibile anche per 400

Vincoli
  • 1 <= month <= 12
  • 1 <= year <= 9999

Esempi

Example 1
Input
days_in_month(2, 2024)
Output
29
Explanation

2024 is divisible by 4 and not by 100, so it's a leap year. February has 29 days.

Example 2
Input
days_in_month(1, 2023)
Output
31
Explanation

January always has 31 days regardless of the year.

Example 3
Input
days_in_month(2, 1900)
Output
28
Explanation

1900 is divisible by 100 but not by 400, so it's NOT a leap year. February has 28 days.

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.