Issue with my code

marduk marduk at python.net
Tue Feb 5 23:05:16 CET 2013



On Tue, Feb 5, 2013, at 04:37 PM, darnold wrote:
> On Feb 5, 2:19 pm, maiden129 <sengokubasarafe... at 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.



More information about the Python-list mailing list