Difference between revisions of "Palindroma"
Jump to navigation
Jump to search
m |
m |
||
| Line 18: | Line 18: | ||
print("is {} palindrome? {}".format(s,"true" if palindrome(s) else "false")) | print("is {} palindrome? {}".format(s,"true" if palindrome(s) else "false")) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
| + | Domenique ha proposto questa funzione in C: | ||
| + | <syntaxhighlight lang="python"> | ||
| + | int palindroma(char *pnt){ | ||
| + | |||
| + | int dim=strlen(pnt)-1; | ||
| + | int l=dim/2; | ||
| + | int k; | ||
| + | |||
| + | for(k=0;k<=l;k++) | ||
| + | { | ||
| + | if(pnt[k]!=pnt[dim]) | ||
| + | return 0; | ||
| + | else | ||
| + | dim--; | ||
| + | } | ||
| + | return 1; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | altre proposte? | ||
Revision as of 11:17, 29 October 2013
Visto che qui non succede nulla, inizio io:
Ecco una implementazione in python3... chi mi propone altre soluzioni alternative?
#!/usr/bin/env python3
def palindrome(x):
if len(x) < 2: return True
else:
if x[0] == x[-1]:
return palindrome(x[1:-1])
else:
return False
if __name__ == "__main__":
s=input("type in a string: ")
print("is {} palindrome? {}".format(s,"true" if palindrome(s) else "false"))
Domenique ha proposto questa funzione in C:
int palindroma(char *pnt){
int dim=strlen(pnt)-1;
int l=dim/2;
int k;
for(k=0;k<=l;k++)
{
if(pnt[k]!=pnt[dim])
return 0;
else
dim--;
}
return 1;
}
altre proposte?