Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98907 > unrolled thread

palindrome

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2015-11-16 23:29 -0500
Last post2015-11-17 10:31 +0100
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  palindrome Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-16 23:29 -0500
    Re: palindrome Abhiram R <abhi.darkness@gmail.com> - 2015-11-17 10:09 +0530
      Re: palindrome Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-16 23:48 -0500
        Re: palindrome Abhiram R <abhi.darkness@gmail.com> - 2015-11-17 10:25 +0530
        Re: palindrome Peter Otten <__peter__@web.de> - 2015-11-17 10:48 +0100
    Re: palindrome Peter Otten <__peter__@web.de> - 2015-11-17 10:31 +0100

#98907 — palindrome

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2015-11-16 23:29 -0500
Subjectpalindrome
Message-ID<s4bl4bd739gfq92c5hipb16grv8a5fh5pb@4ax.com>
http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html

Here is my answers.  What would make it better?

import random
str1=""
letcount=4
count=0
abc='abcdefghijklmnopqrstuvwxyz'
while True:
    for i in range(letcount):
        a=random.choice(abc)
        str1+=a
    print str1
    count+=1
    if str1==str1[::-1]:        
        break
    else:
        str1=""
print "Tries= ",count
print str1

[toc] | [next] | [standalone]


#98908

FromAbhiram R <abhi.darkness@gmail.com>
Date2015-11-17 10:09 +0530
Message-ID<mailman.376.1447735216.16136.python-list@python.org>
In reply to#98907
On Tue, Nov 17, 2015 at 9:59 AM, Seymore4Head <Seymore4Head@hotmail.invalid>
wrote:

> http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html
>
> Here is my answers.  What would make it better?
>
> import random
> str1=""
> letcount=4
> count=0
> abc='abcdefghijklmnopqrstuvwxyz'
> while True:
>     for i in range(letcount):
>         a=random.choice(abc)
>         str1+=a
>     print str1
>     count+=1
>     if str1==str1[::-1]:
>         break
>     else:
>         str1=""
> print "Tries= ",count
> print str1
> --
>
> ​

The question asks to get an input from the user and print if it's a
palindrome or not.
It should be just

strA=raw_input()
if strA==strA[::-1]:
    print "Palindrome"
else:
    print "Not"

Right? Am I missing something? Why are you generating random strings and
trying to check for palindromes?​


Thanks
Abhiram R (IRC - abhiii5459_ ; Twitter - https://twitter.com/abhiii5459)

[toc] | [prev] | [next] | [standalone]


#98909

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2015-11-16 23:48 -0500
Message-ID<78cl4bljie79vqr80d1g084jm8ma3k0r9k@4ax.com>
In reply to#98908
On Tue, 17 Nov 2015 10:09:27 +0530, Abhiram R
<abhi.darkness@gmail.com> wrote:

>On Tue, Nov 17, 2015 at 9:59 AM, Seymore4Head <Seymore4Head@hotmail.invalid>
>wrote:
>
>> http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html
>>
>> Here is my answers.  What would make it better?
>>
>> import random
>> str1=""
>> letcount=4
>> count=0
>> abc='abcdefghijklmnopqrstuvwxyz'
>> while True:
>>     for i in range(letcount):
>>         a=random.choice(abc)
>>         str1+=a
>>     print str1
>>     count+=1
>>     if str1==str1[::-1]:
>>         break
>>     else:
>>         str1=""
>> print "Tries= ",count
>> print str1
>> --
>>
>> ?
>
>The question asks to get an input from the user and print if it's a
>palindrome or not.
>It should be just
>
>strA=raw_input()
>if strA==strA[::-1]:
>    print "Palindrome"
>else:
>    print "Not"
>
>Right? Am I missing something? Why are you generating random strings and
>trying to check for palindromes??
>
The instructions do ask for input.  I am lazy.  I thought it would be
cool to have random input instead of just typing a phrase.

[toc] | [prev] | [next] | [standalone]


#98910

FromAbhiram R <abhi.darkness@gmail.com>
Date2015-11-17 10:25 +0530
Message-ID<mailman.377.1447736189.16136.python-list@python.org>
In reply to#98909
On Tue, Nov 17, 2015 at 10:18 AM, Seymore4Head <Seymore4Head@hotmail.invalid
> wrote:

> On Tue, 17 Nov 2015 10:09:27 +0530, Abhiram R
> <abhi.darkness@gmail.com> wrote:
>
> >On Tue, Nov 17, 2015 at 9:59 AM, Seymore4Head
> <Seymore4Head@hotmail.invalid>
> >wrote:
> >
> >> http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html
> >>
> >> Here is my answers.  What would make it better?
> >>
> >> import random
> >> str1=""
> >> letcount=4
> >> count=0
> >> abc='abcdefghijklmnopqrstuvwxyz'
> >> while True:
> >>     for i in range(letcount):
> >>         a=random.choice(abc)
> >>         str1+=a
> >>     print str1
> >>     count+=1
> >>     if str1==str1[::-1]:
> >>         break
> >>     else:
> >>         str1=""
> >> print "Tries= ",count
> >> print str1
> >> --
> >>
> >> ?
> >
> >The question asks to get an input from the user and print if it's a
> >palindrome or not.
> >It should be just
> >
> >strA=raw_input()
> >if strA==strA[::-1]:
> >    print "Palindrome"
> >else:
> >    print "Not"
> >
> >Right? Am I missing something? Why are you generating random strings and
> >trying to check for palindromes??
> >
> The instructions do ask for input.  I am lazy.  I thought it would be
> cool to have random input instead of just typing a phrase.
> --
>
>
​Haha. Nice. Although with your length of string and the range you're
picking from,the chances of you getting a palindrome are (1/24!)  :D ​




-- 
-Abhiram R

[toc] | [prev] | [next] | [standalone]


#98913

FromPeter Otten <__peter__@web.de>
Date2015-11-17 10:48 +0100
Message-ID<mailman.380.1447753729.16136.python-list@python.org>
In reply to#98909
Abhiram R wrote:

> ​Haha. Nice. Although with your length of string and the range you're
> picking from,the chances of you getting a palindrome are (1/24!)  :D ​

Are you sure?

>>> candidates = list(itertools.product(string.ascii_lowercase, repeat=4))
>>> len(candidates)/len([c for c in candidates if c == c[::-1]])
676.0

That looks like one in 26**(length//2)

[toc] | [prev] | [next] | [standalone]


#98912

FromPeter Otten <__peter__@web.de>
Date2015-11-17 10:31 +0100
Message-ID<mailman.379.1447752716.16136.python-list@python.org>
In reply to#98907
Seymore4Head wrote:

> http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html
> 
> Here is my answers.  What would make it better?

1. Break the code into functions: one to generate a random string (the 
desired length could be a parameter) and one to check if the string is a 
palindrome. With that the loop will become

tries = 0
while True:
    tries += 1
    candidate = random_string(length=4)
    print(candidate)
    if is_palindrome(candidate):
        break
print(tries, "tries")

2. If you plan to reuse these functions put the above code in a function 
(let's call it main), too, that you invoke with

if __name__ == "__main__":
    main()

to avoid that the code is executed when you import the module instead of 
running it as a script.

3. For better readability add spaces around operators. There is a tool 
called pep8 that will point out where you are breaking the standard Python 
coding conventions.

4. Minor rewrites:
4.1 Can you rewrite the while loop as a for loop?

for tries in ...:
   ...

Hint 1: you can put a while loop into a generator
Hint 2: there's a ready-made solution in itertools.

4.2 Can you build the random string using a generator expression and 
"".join(...)?

> import random
> str1=""
> letcount=4
> count=0
> abc='abcdefghijklmnopqrstuvwxyz'
> while True:
>     for i in range(letcount):
>         a=random.choice(abc)
>         str1+=a
>     print str1
>     count+=1
>     if str1==str1[::-1]:
>         break
>     else:
>         str1=""
> print "Tries= ",count
> print str1

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web