Programmazione competitivaFacile

Elenco telefonico

Guida dettagliata e implementazione Python per il problema della "Rubrica telefonica".

Dichiarazione del problema

Facile

Scrivi una funzione phone_directory(contacts, query) che accetta un elenco di stringhe univoche contacts e una stringa di query query. Dovrebbe restituire un elenco di elenchi di stringhe, dove l'iesimo elenco contiene i contatti ordinati che corrispondono al prefisso di query fino alla lunghezza i+1 (indicizzato a 1). Se nessun contatto corrisponde, restituisce un elenco vuoto per quel prefisso.

Vincoli
  • 1 <= len(contacts) <= 100
  • 1 <= len(query) <= 20
  • All strings consist of lowercase English letters.

Esempi

Example 1
Input
phone_directory(['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], 'gee')
Output
[['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky']]
Explanation

For 'g', 'ge', and 'gee', all 4 contacts match prefix and are returned in sorted order.

Example 2
Input
phone_directory(['mobile', 'mouse', 'moneypot', 'monitor', 'mousepad'], 'mouse')
Output
[['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad']]
Explanation

For prefix 'm' and 'mo', all contacts match. For 'mou', 'mous', and 'mouse', only 'mouse' and 'mousepad' match.

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