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


Groups > comp.lang.python > #38236

Re: Issue with my code

From marduk <marduk@python.net>
Subject Re: Issue with my code
Date 2013-02-05 17:05 -0500
References (1 earlier) <mailman.1379.1360090617.2939.python-list@python.org> <db762eb7-0901-4feb-bdf8-ac14aca4a711@googlegroups.com> <mailman.1384.1360093449.2939.python-list@python.org> <mailman.1386.1360095557.2939.python-list@python.org> <282fd10c-2199-45b3-94aa-f57a38722442@h9g2000vbk.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1392.1360101919.2939.python-list@python.org> (permalink)

Show all headers | View raw



On Tue, Feb 5, 2013, at 04:37 PM, darnold wrote:
> On Feb 5, 2:19 pm, maiden129 <sengokubasarafe...@gmail.com> wrote:
> > How to reverse the two loops?
> >
> 
> s=input("Enter a string, eg(4856w23874): ")
> 
> checkS=['0','1','2','3','4','5','6','7','8','9']
> 
> for digit in checkS:
>     t = s.count(digit)
>     if t == 0:
>         pass
>     elif t == 1:
>         print(digit,"occurs 1 time.")
>     else:
>         print(digit, "occurs", t,"times.")
> 
> 
> >>>
> Enter a string, eg(4856w23874): 23493049weee2039412367
> 0 occurs 2 times.
> 1 occurs 1 time.
> 2 occurs 3 times.
> 3 occurs 4 times.
> 4 occurs 3 times.
> 6 occurs 1 time.
> 7 occurs 1 time.
> 9 occurs 3 times.
> >>>

Although that implementation also scans the string 10 times (s.count()),
which may not be as efficient (although it is happening in C, so perhaps
not).

A better solution involves only scanning the string once.

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


Thread

Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 10:38 -0800
  Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 10:43 -0800
  Re: Issue with my code marduk <marduk@python.net> - 2013-02-05 13:56 -0500
    Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 11:20 -0800
      Re: Issue with my code Dave Angel <davea@davea.name> - 2013-02-05 14:43 -0500
        Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 12:19 -0800
        Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 12:19 -0800
          Re: Issue with my code darnold <darnold992000@yahoo.com> - 2013-02-05 13:37 -0800
            Re: Issue with my code marduk <marduk@python.net> - 2013-02-05 17:05 -0500
              Re: Issue with my code darnold <darnold992000@yahoo.com> - 2013-02-05 14:25 -0800
    Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 11:20 -0800
  Re: Issue with my code MRAB <python@mrabarnett.plus.com> - 2013-02-05 19:06 +0000
  Re: Issue with my code Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-05 17:20 -0500
  Re: Issue with my code Terry Reedy <tjreedy@udel.edu> - 2013-02-05 19:06 -0500
  Re: Issue with my code rusi <rustompmody@gmail.com> - 2013-02-05 22:29 -0800
    Re: Issue with my code rusi <rustompmody@gmail.com> - 2013-02-05 22:32 -0800

csiph-web