Jump to content

hey guys can anyone write a code for palindrome words using loops in pyhton. 

thanks.

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/
Share on other sites

Link to post
Share on other sites

How about you try it and we'll help you if you don't get it right? If you've already tried it, then post your code and we'll help show you how to correct it.

ive tried recursive method but i need it in loops .

def is_palindrome2(word):    leng = len(word)    if leng < 2:        return True    if word[0] != word[-1]:        return False    return is_palindrome2(word[1:-1])

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530297
Share on other sites

Link to post
Share on other sites

ive tried recursive method but i need it in loops .

Seems odd that you'd understand how to write a recursive algorithm but not an iterative one. Usually recursion is harder.

 

If you need to use loops then do this:

  1. Using a loop, reverse the original string
  2. Using a loop compare each character of the original string against the reversed string
Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530346
Share on other sites

Link to post
Share on other sites

 

Seems odd that you'd understand how to write a recursive algorithm but not an iterative one. Usually recursion is harder.

 

If you need to use loops then do this:

  1. Using a loop, reverse the original string
  2. Using a loop compare each character of the original string against the reversed string

 

i can do the loops question with in 20 mins ,recursive is not that hard i have no time for loops im working on another question of counting prime and i have 3 more questions .

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530377
Share on other sites

Link to post
Share on other sites

 

Seems odd that you'd understand how to write a recursive algorithm but not an iterative one. Usually recursion is harder.

 

If you need to use loops then do this:

  1. Using a loop, reverse the original string
  2. Using a loop compare each character of the original string against the reversed string

 

dude this is my question from assignment

 
Define a function named count_primes that returns the number of primes in a given list of positive
integers. For example, if the list is [2,91,11,271,289,143,24], the function returns 3 because there are
three primes in the list. (For this question, it is suggested that you define a supporting function to check whether
a given positive integer is a prime, then use this supporting function to define count_primes.)
 
i did the function of checking prime in my week 7 of the class and i tried to use it again but there is a promblem heres the code  the problem is its showing 5 as the answer insted of 3 in this list [2,91,11,271,289,143,24]
def isprime(n):    if n == 1:        count += 1    if n == 2:        print ("2 is a prime number.")        return    for x in range (2, n):        if n%x == 0:            return (n, "is not prime.")            break    else:        print (n, "is a prime number.")# above is my original week 7 code of primes            def count_primes(lst):    length = len(lst)    count = 0    for i in lst:        if i==1 or i==2:            count += 1        for x in range(2,i):            if i%x == 0:                count += 1                break            return count                            

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530394
Share on other sites

Link to post
Share on other sites

i can do the loops question with in 20 mins ,recursive is not that hard i have no time for loops im working on another question of counting prime and i have 3 more questions .

 

Sorry then, I'm not going to do your homework for you. I told you what you need to do, you just have to code it.

 

Here's a tutorial on for loops if you aren't familiar with them. It shows you how to iterate a string in the first example. http://www.tutorialspoint.com/python/python_for_loop.htm

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530400
Share on other sites

Link to post
Share on other sites

Sorry then, I'm not going to do your homework for you. I told you what you need to do, you just have to code it.

 

Here's a tutorial on for loops if you aren't familiar with them. It shows you how to iterate a string in the first example. http://www.tutorialspoint.com/python/python_for_loop.htm

well i will do it myself thanks for the help above and theres another question i posted above

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530403
Share on other sites

Link to post
Share on other sites

def count_primes(lst):    length = len(lst)    count = 0    for i in lst:        if i==1 or i==2:            count += 1        for x in range(2,i):            if i%x == 0:                count += 1                break            return count

 

First 1 isn't a prime number so you should remove it from if i==1 or i==2:

 

Also, your inner loop here isn't correct. You are incrementing count if you find that x divides evenly into i. That means it's not prime.

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530444
Share on other sites

Link to post
Share on other sites

Sorry then, I'm not going to do your homework for you. I told you what you need to do, you just have to code it.

 

Here's a tutorial on for loops if you aren't familiar with them. It shows you how to iterate a string in the first example. http://www.tutorialspoint.com/python/python_for_loop.htm

hey i got another method for count_primes question but it isnt working well look at this code

def isprime(n):    if n == 1:        count += 1    if n == 2:        count += 1    for x in range (2, n):        if n%x == 0:            count += 1            break    else:        print "enter a positive number"# above is my original week 7 code of primes            def count_primes(lst):    count = 0    for i in lst:        isprime(i)

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530448
Share on other sites

Link to post
Share on other sites

See my previous post where I mentioned what's wrong with your old count_prime function. Your isprime function still has those issues.

 

It also has more issues. count_primes isn't counting anything and isprime is doing the counting when the name suggests it should just return a true/false value and not worry about counting.

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530473
Share on other sites

Link to post
Share on other sites

First 1 isn't a prime number so you should remove it from if i==1 or i==2:

 

Also, your inner loop here isn't correct. You are incrementing count if you find that x divides evenly into i. That means it's not prime.

for x in range (2, n):        if n%x == 0:            count += 1            breakin this part its not evenly dividing its just looking for a number which can divide by itself and in the rage b/w (2 to itself)

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530503
Share on other sites

Link to post
Share on other sites

Evenly divisible means no remainder. What n%x == 0 is checking that n/x has no remainder. 6/2 = 3 and has no remainder so 6%2 == 0 is true. You're counting it as a prime but in reality your if statement is testing that the number isn't prime.

thats true but it is like dividing by 2 and then 3 and then by 4 and so on.....

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530607
Share on other sites

Link to post
Share on other sites

go to you idle and type 

for i in range(2,10):

     print i

 

 

it prints the numbers between 2 to 10 so in my function all im doing is im dividing from 2 to 10 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530625
Share on other sites

Link to post
Share on other sites

What I'm saying is that if n%x == 0 is true that means the number isn't prime. Also, range(2,n) only goes up to n - 1

 

Here's how it works out for a number

# Say n = 9# Your loop "for "for x in range(2,n):" means use all the values from 2 to 8# So you get the following results with n%x == 09%2 == 0 # false9%3 == 0 # true9%4 == 0 # false9%5 == 0 # false9%6 == 0 # false9%7 == 0 # false9%8 == 0 # false# because 9%3 == 0 returned true, it means that it's not a prime number but you are counting it as a prime
Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530645
Share on other sites

Link to post
Share on other sites

 

What I'm saying is that if n%x == 0 is true that means the number isn't prime. Also, range(2,n) only goes up to n - 1

 

Here's how it works out for a number

# Say n = 9# Your loop "for "for x in range(2,n):" means use all the values from 2 to 8# So you get the following results with n%x == 09%2 == 0 # false9%3 == 0 # true9%4 == 0 # false9%5 == 0 # false9%6 == 0 # false9%7 == 0 # false9%8 == 0 # false# because 9%3 == 0 returned true, it means that it's not a prime number but you are counting it as a prime

holy shit i made a huge mistake and its too late! :(

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530664
Share on other sites

Link to post
Share on other sites

anyways thanks mate youuve been a lot of help!

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530674
Share on other sites

Link to post
Share on other sites

No problem, but why is it too late?

submission was @ 9:00 pst its 9:20

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530727
Share on other sites

Link to post
Share on other sites

No problem, but why is it too late?

i think i screwd up question one , i had to use only one return statement and i used two :( 

def is_palindrome1(word):    ori = []    rev = []    for i in word:        ori.append(i)    rev = ori[::-1]    print reverse    if ori == rev:        return True    else :        return False

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530857
Share on other sites

Link to post
Share on other sites

i think i screwd up question one , i had to use only one return statement and i used two

 

Hopefully that loop is enough to get the marks for that requirement. Shouldn't lose many marks for the return mistake though.

# Just neededreturn ori == rev# instead ofif ori == rev:    return Trueelse :    return False

However print reverse causes an error because reverse isn't defined. I assume you meant print rev. The function doesn't actually run with that error in there. But again, might not cause you to lose many marks.

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3530876
Share on other sites

Link to post
Share on other sites

Just wanted to mention a couple things about the code to show you a few things.

# this list conversion works but it is unnecessary because a string can already be iterated and sliced    ori = []    rev = []    for i in word:        ori.append(i)    rev = ori[::-1]# could be replaced with just    rev = word[::-1]# in fact, the whole method could be this def is_palindrome1(word):    return word == word[::-1]# however because you needed to use a loop that probably wouldn't be accepted. So I would have manually reversed the string with a loop.def is_palindrome1(word):    rev = ""    for c in word:        rev = c + rev  # add char to beginning of the new string    return word == rev

Not that I know what your teacher is looking for in an answer though.

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3531001
Share on other sites

Link to post
Share on other sites

Just wanted to mention a couple things about the code to show you a few things.

# this list conversion works but it is unnecessary because a string can already be iterated and sliced    ori = []    rev = []    for i in word:        ori.append(i)    rev = ori[::-1]# could be replaced with just    rev = word[::-1]# in fact, the whole method could be this def is_palindrome1(word):    return word == word[::-1]# however because you needed to use a loop that probably wouldn't be accepted. So I would have manually reversed the string with a loop.def is_palindrome1(word):    rev = ""    for c in word:        rev = c + rev  # add char to beginning of the new string    return word == rev

Not that I know what your teacher is looking for in an answer though.

oh nice your code is efficient but i have inform the user that its is either true or false

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3531104
Share on other sites

Link to post
Share on other sites

oh nice your code is efficient but i have inform the user that its is either true or false

Yup, that's what return word == rev is doing.

# thisreturn word == rev# is equivalent to writing thisif word == rev:    return Trueelse:    return False# because word == rev is a boolean expression and will be evaluated to either True or Falseif word == rev:     return True else:    return False# means that if word == rev is true, then return True# and if word == rev is false, then return False# so that's why that can be shortened toreturn word == rev# because it'll do just that, return true if word == rev, or false if it doesn't

An if statement uses a boolean expression to decide what block to execute. But boolean expressions can also be returned by themselves like I showed you. They can also be assigned to variables like so

valid = (word == rev) # the variable valid now contains either True or False (brackets just for easier readability)# and it can be used wherever a boolean can be usedif valid:    # do something

Hope that makes at least some sense to you. If not, don't worry about it too much right now. You'll learn it all eventually.

Link to comment
https://linustechtips.com/topic/258365-palindrome-python-code/#findComment-3531172
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×