import string
def reverseString(s):
#return(s[::-1]) #clever, starts at s[len-1] and ends at s[0] counting by -1
newString = ""
for i in range(len(s)-1, -1, -1):
newString += s[i]
return newString
def isInteger(s):
#this loop iterates over all the characters in s, from first to last
for character in s:
if character < '0' or character > '9':
return False
return True
# the "usual" way, iterating over the range of indices [0..n-1]
for i in range(len(s)):
if s[i] < '0' or s[i] > '9':
return False # return False as soon as I find a non-integer
return True # can only return True after I have looked at all characters
#let's check negative numbers as well!
def isInteger2(s):
if s[0] == "-":
start = 1
else:
start = 0
for i in range(start, len(s)):
if s[i] not in string.digits: # must import string to use this!
return False
return True
def isPalindrome(s):
# with a loop
for i in range(len(s)): # do I have to go all the way to len(s)??
if s[i] != s[-(i+1)]:
return False
return True
# making use of reverseString!
rev = reverseString(s)
if (rev == s):
return True
return False
print(reverseString("Mark"))
print(isInteger("1231243"))
print(isInteger("12312b3"))
print(isInteger("0"))
print(isInteger("-1"))
print(isInteger2("-1"))
print(isInteger2("1231243"))
print(isInteger2("12312b3"))
print(isInteger2("0"))
print("Mark" * 7)
print("Karen" < "Mark")
print("Mark" < "Mary")
print("Marks" < "Mark")
print(isPalindrome("racecar"))
print(isPalindrome("car"))
print(isPalindrome("ana"))