Write a function that determines if an inputted number is a palindrome.
I'm going to write this in python, then hopefully you'll be able to adapt it to the language you need. I am going to write it with a loop. def palindrome(nums): ##starting a new class, simple stuff nums = [1,2,2,1] ##defining the list, put your numbers here instead of mine newlen = len(nums)/2 ##divides list length by two, it's for the loop for i in range(newlen): ##loop over each value in the first half of the list if nums[i]==nums[len(nums)-i] ##if the ith value is the same as the ith from last value return 'true' else return 'false'
Close, but you missed a couple colons, have an index error and only ever check the first and last item. Here it is slightly modified. The second line does an "integer divide" by 2, since I was using python 3.x I needed '//' instead of just '/'. This, is so we can compare first half and last half. If length is uneven the middle element does not matter. Needed the '-1' in the fourth line, otherwise the index would be 1 past the end of the list on the first iteration. Changed it to 'not equal' so that at any time it finds something not equal we just return False without continuing. If it successfully exits the for loop it returns True. Also change True and False to booleans instead of strings. def palindrome(nums): newlen = len(nums)//2 for i in range(newlen): if nums[i]!=nums[len(nums)-i-1]: return False return True And some tests. >>> palindrome([1,2,2,1]) True >>> palindrome([1,2,3,2,1]) True >>> palindrome([1,2,3,3,2,1]) True >>> palindrome([1,2,4,3,2,1]) False
One other note is that if you are using this a lot on really long palindromes you may want to set a variable say end = len(nums) - 1 before the for loop and use [end -i] in the if statement so it doesn't have to be recalculated possibly many times and affect performance. Thus, it would look like this. def palindrome(nums): newlen = len(nums)//2 end = len(nums) - 1 for i in range(newlen): if nums[i]!=nums[end-i]: return False return True
Thanks, I'm still fairly new to Python. I don't quite have everything yet. I figured that asker would be able to correct any errors like the the ones you pointed out. I was providing an outline that (s)he could tweak and fix.
Here's a recursive function: import sys def isPalindrome( theNumber ): if( len( theNumber ) < 2 ): return True elif( theNumber[ 0 ] == theNumber [ -1 ] ): return( isPalindrome( theNumber[ 1: -1 ] ) ) else: return False if( __name__ == "__main__" ): print isPalindrome( sys.argv[ 1 ] ) You can run it with: ./palindrome.py 1234564321
@rsmith6559 hey even though your code is good, but i suggest that you don`t overburden beginners with command line arguments and standard boilerplates.... let the code be simple ./palindrome.py num will work in the command prompt or terminal...
The function is a different way to handle the problem, that was the main point. The rest of it was just to provide something that works and could be tested.
Join our real-time social learning platform and learn together with your friends!