HACKING 120% {Hacking, programmazione, computer & molto altro}

[Esercizio] Parola Palindroma!, Ci provo pure io a postare un esercizio!

« Older   Newer »
  Share  
RootkitNeo
view post Posted on 7/9/2013, 21:51     +1   -1




Una parola è palindroma se la prima metà è uguale alla seconda. Non puoi sapere se è palindroma se non scorri le due metà. Questa è una soluzione buona, se cerchi qualcosa di sbrigativo e meno didattico puoi spezzare la stringa a metà, invertirla e confrontarla con l'altra metà.
Detto questo quelle che hai usato sono tutte astrazioni, in un modo o nell'altro all'interno verranno confrontati.
 
Top
view post Posted on 7/9/2013, 23:20     +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


Sono appena tornato a casa, questa sera ti faccio i calcoli di tempo, sto implentando pure il tuo in python 3 :)

Il mio è questo:
CODICE
print ("Palindromo") if abc[:int(len(abc)/2)]==abc[:int((len(abc)+1)/-2)] else print("Non palindromo")


Edited by Wet Water - 8/9/2013, 01:30
 
Web  Top
view post Posted on 8/9/2013, 01:03     +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


Comunque ho buttato giù il tuo in python. Ho usato un for invece che un while perché il for è più rapido in genere; non ho fatto attenzione a scrivere il codice nel minor spazio possibile:

CODICE
def par2(abc):
   j=len(abc)-1
   for x in range(0,int(len(abc)/2)):
       if abc[x]==abc[j]:
           j-=1
       else:
           print("Non palindromo")
           break
   print("Palindromo")
 
Web  Top
RootkitNeo
view post Posted on 8/9/2013, 02:24     +1   -1




Tutti i cicli vengono comunque ottimizzati, e viene ottimizzato l'accesso alla memoria... anche questo gioca un ruolo importante.

Si può scrivere anche così:
CODICE
bool palindroma(string str) {
 string str1 = string(str.rbegin(),str.rend());
 return (str1.compare(str) == 0);
}


è più rapido, ma il confronto deve comunque avvenire carattere per carattere, non si sfugge.

Guarda il mio in asm nell'altro esercizio che è stato postato, concettualmente a basso livello il confronto avviene carattere per carattere (questo accade ovviamente sui singoli bit).
 
Top
view post Posted on 8/9/2013, 10:27     +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


Allora questo è più lento perché confronta tutta la stringa e non solo metà :)
 
Web  Top
RootkitNeo
view post Posted on 8/9/2013, 10:39     +1   -1




Si, in quest'ultimo caso si. Ma nel precedente no, in quanto sino a che i<j il ciclo continua, poi si interrompe.
Ad ogni modo una parola è palindroma se la prima metà è uguale alla seconda.
 
Top
view post Posted on 8/9/2013, 11:29     +1   -1

Utente

Group:
Utenti UP
Posts:
43
Reputation:
0
Location:
Kernel.

Status:


Vero Root non avevo pensato alla ricorsione per risolverlo!
Not bad!
 
Top
RootkitNeo
view post Posted on 8/9/2013, 12:46     +1   -1




Con la ricorsione molto probabilmente le prestazioni ne risentono. Serve a non complicarsi la vita con alcuni algoritmi, ma è meglio l'algoritmo iterativo.
 
Top
view post Posted on 8/9/2013, 16:41     +1   -1

Utente

Group:
Utenti UP
Posts:
43
Reputation:
0
Location:
Kernel.

Status:


Ho provato a fare un algoritmo che risolve la torre di Hanoi (con la ricorsione) se mettevo 500 dischi finiva lo Stack e non me lo compilava xD
 
Top
23 replies since 7/9/2013, 00:27   231 views
  Share