Palindroma

From Sistemi Operativi
Revision as of 11:17, 29 October 2013 by Renzo (talk | contribs)
Jump to navigation Jump to search

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?