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


Groups > comp.lang.python > #98912

Re: palindrome

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: palindrome
Date Tue, 17 Nov 2015 10:31:37 +0100
Organization None
Lines 63
Message-ID <mailman.379.1447752716.16136.python-list@python.org> (permalink)
References <s4bl4bd739gfq92c5hipb16grv8a5fh5pb@4ax.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de WsZvkbLjNkZIgrU5YU0j1ArH8mp+U+zQC+SNaTjGHtMg==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'tries': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'executed': 0.07; 'main()': 0.07; 'rewrite': 0.07; 'conventions.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'better?': 0.16; 'hint': 0.16; 'operators.': 0.16; 'pep8': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'script.': 0.18; '(the': 0.22; 'minor': 0.22; 'import': 0.24; 'module': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'coding': 0.27; 'function': 0.28; 'invoke': 0.29; 'spaces': 0.29; 'random': 0.29; 'print': 0.30; 'code': 0.30; 'candidate': 0.31; 'point': 0.33; 'running': 0.34; 'add': 0.34; 'could': 0.35; 'skip:i 20': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'tool': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'desired': 0.37; 'build': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'called': 0.40; 'avoid': 0.61; 'reuse': 0.66; 'url:2014': 0.66; 'here': 0.66; 'ready-made': 0.84; 'seymore4head': 0.84; 'url:03': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8631.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98912

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web