So I'm trying to test if something is a palindrome. Here is my code:
This function returns a string of the first half of a larger string. ("TEST" returns "TE", "HELLO" returns "HE")
def takeStart(s):
start = ""
# The following determines the final index of the first half
if len(s)%2==0:
a = (len(s)/2)-1
else:
a = ((len(s)-1)/2)-1
for i in range(a):
start+=s[i]
return start
This function returns a string of the second half of a larger string. ("TEST" returns "ST", "HELLO" returns "LO")
def takeEnd(s):
end = ""
# The following determines the beginning index of the second half
if len(s)%2==0:
a = (len(s)/2)
else:
a = ((len(s)-1)/2)
for i in range(a,len(s)):
end+=s[i]
return end
This function flips a string. ("TEST" returns "TSET", "HELLO" returns "OLLEH")
def flip(s):
flipped = ""
for i in range(1,len(s)):
flipped+=s[len(s)-i]
flipped+=s[0]
return flipped
This code takes every product of two 3-digit numbers, and checks if it's a palindrome
for i in range(100,1000):
for q in range(100,1000):
a = i*q
if takeStart(str(a)) == flip(takeEnd(str(a))):
print(str(a))
When this code is run, it outputs:
Traceback (most recent call last):
File "[redacted]", line 39, in <module>
if takeStart(str(a)) == flip(takeEnd(str(a))):
File "[redacted]", line 14, in takeStart
for i in range(a):
TypeError: 'float' object cannot be interpreted as an integer
Alright, I thought I just convert a to an integer and all should be swell.
Doing that appears to remove all errors, but there is NO output whatsoever. (There are new lines every once and a while which makes me think it's working but not outputing any data)
Any ideas on why this is happening?
UPDATE: my code now:
def takeStart(s):
start = ""
if len(s)%2==0:
a = (len(s)//2)
else:
a = (len(s)-1)//2
return start[0:a]
def takeEnd(s):
end = ""
if len(s)%2==0:
a = (len(s)//2)
else:
a = ((len(s)-1)//2)
return end[int(a):len(s)]
def flip(s):
return s[::-1]
for i in range(100,1000):
for q in range(100,1000):
a = i*q
if takeStart(str(a)) == flip(takeEnd(str(a))):
print(str(a))
This is just outputting every single number. I tested each method and they're returning empty strings. (I'm assuming), which is why every number is passing the palindrome check and printing.
First, using range(int(a))
and range(int(a), len(s))
will solve your error. As Jon Clements points out, you can solve that more easily by just using //
instead of /
to get integers in the first place. But either way, it's not causing any problems.
Your problem is that range
s, and just about everything related in Python, are half-open. So, your takeStart
function is returning all the values up to, but not including, the half-way point—that is, it gives you H
for HELLO
, T
for TEST
, BIGG
for BIGGERTEST
.
Just get rid of the -1
on your a = …
lines, and that will solve that problem.
And then it prints out a whole bunch of output lines, all palindromes, which I assume is what you were intending to do.
However, you're still not going to get any odd-length palindromes. For example, with 'MADAM', even when you get the functions right, takeStart(s)
is MA
, takeEnd(s)
is DAM
, flip(takeEnd(s))
is MAD
, and that's not the same as MAD
. Even though your functions are working right, they're not solving the problem. So there's a bug in your design as well as your implementation. If you think about it for a while, you should figure out how to make this work.
And, once you do, you should realize that takeStart
and takeEnd
can be simplified a lot. (Hint: In which cases do you really need to treat odd and even lengths differently?)
While we're at it, this:
foo = ""
for i in range(x, y):
foo += s[i]
return foo
… is just a verbose, slow, and easy-to-get-wrong way of writing this:
return foo[x:y]
And, likewise, your whole flipped
function is just:
return s[::-1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With