November 23, 2024, 12:19:56 AM

1,531,352 Posts in 46,734 Topics by 1,523 Members
› View the most recent posts on the forum.


PYTHON HELP

Started by Daddy, March 06, 2009, 05:05:53 PM

previous topic - next topic

0 Members and 1 Guest are viewing this topic.

Go Down

Daddy

Code Select
def isPandigital(n):
digList = list(str(n))
for i in digList:
if str(i) in digList:
continue
else:
return False
return max(digList) == len(str(n))
print isPandigital(3142)


Keeps returning False. HOWEVER
Code Select
def isPandigital(n):
digList = list(str(n))
for i in digList:
if str(i) in digList:
continue
else:
return False
return max(digList)
print isPandigital(3142)

Will return 4

and
Code Select
def isPandigital(n):
digList = list(str(n))
for i in digList:
if str(i) in digList:
continue
else:
return False
return len(str(n))
print isPandigital(3142)

Also returns 4, which both are correct.

So I don't get why it returns false, because if you have the == isn't it the same as saying:

return 4 == 4, which would be true?
ffffffffffff

Feynman

Did you set it to Wumbo?

mario583

Quote from: Bassir on March 06, 2009, 06:18:37 PM
Did you set it to Wumbo?

I Wumbo, you wumbo, he, she, me, wumbo, wumbo, wumboing, I'll have three wumbo, wumbora, wumbology, the study of wumbo? It's first grade, JMV! bassir;

guff

March 07, 2009, 08:31:54 AM #3 Last Edit: March 07, 2009, 03:14:33 PM by guff
Quote from: Raekewn on March 06, 2009, 05:05:53 PM
Code Select
def isPandigital(n):
digList = list(str(n))
for i in digList:
if str(i) in digList:
continue
else:
return False
return max(digList) == len(str(n))
print isPandigital(3142)
what the lol how is that testing if it's pandigital
all that for loop is doing is checking to see if each element of digList is an element of digList, which is obviously true
also, there's not really a reason to make digList a list because strings are iterable too
and the reason that it returns false is that you're comparing a string ("4") to an int (4) which will always evaluate to false

but basically if you actually want to test for pandigitalness then you've got to generate a list of the numbers from 1 to n (range(1, n + 1)), then check to see if each digit of your input is in that list and that the lengths are the same baddood;

Go Up