Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Character count (python)

  1. #11
    Join Date
    Sep 2008
    Beans
    148

    Re: Character count (python)

    OK I sort of figured out how to do what I wanted, sort of as in I need to do other things with the string to actually make what I originally wanted but I'll do that by myself.
    PHP Code:
    #!/bin/python
    raw_input("blabla: ")
    "guumom"#Wtf
    print s
    aa
    =s.rfind(f)
    if 
    aa >= 2:
        
    s.replace(f"2" )#<<Would be 2 if "u" was entered, Meant to be how many times character specified appears, variable (aa) doesn't work??
        
    print a
    else:
        print 
    "naww" 

  2. #12
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: Character count (python)

    Er, ok, since you put up code, here is what I was alluding to in my first reply. A for loop, a placeholder and a counter...

    PHP Code:
    def letters(word=''):      
        
    count # Counter   
        
    prev '' # Placeholder
        
    for c in word# For loop
            
    if != prev:
                if 
    prev# Prevents printing the empty prev.
                    
    print prevcount
                count 
    1
                prev 
    c
            elif c 
    == prev:
                
    count count 1
        
    else: # needed to print out the final letter's count
            
    print ccount

    while 1:
        
    letters(raw_input(': ')) 
    Results in the following.
    Code:
    {grey@igbuntu:~} python foo.py
    : mississippi                 
    m 1                           
    i 1                           
    s 2                           
    i 1                           
    s 2                           
    i 1                           
    p 2                           
    i 1                           
    : ragoo                       
    r 1                           
    a 1                           
    g 1                           
    o 2                           
    : Gooooooooooooooaaaaaaaallllll!!
    G 1                              
    o 14                             
    a 8                              
    l 6                              
    ! 2
    Since that accurately counts all letters it is simply a matter of adjusting the exact display logic with a few ifs. Or, if you're getting really fancy, throwing it all into a dict of lists and extracting the information from that dict as needed.
    Last edited by Greyed; November 9th, 2008 at 06:16 AM. Reason: Some commenting.
    Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
    L.A.G. - Jobs Dissembles - 2010/4/29

  3. #13
    Join Date
    Sep 2008
    Beans
    148

    Re: Character count (python)

    Quote Originally Posted by Greyed View Post
    Er, ok, since you put up code, here is what I was alluding to in my first reply. A for loop, a placeholder and a counter...

    PHP Code:
    def letters(word=''):      
        
    count # Counter   
        
    prev '' # Placeholder
        
    for c in word# For loop
            
    if != prev:
                if 
    prev# Prevents printing the empty prev.
                    
    print prevcount
                count 
    1
                prev 
    c
            elif c 
    == prev:
                
    count count 1
        
    else: # needed to print out the final letter's count
            
    print ccount

    while 1:
        
    letters(raw_input(': ')) 
    Results in the following.
    Code:
    {grey@igbuntu:~} python foo.py
    : mississippi                 
    m 1                           
    i 1                           
    s 2                           
    i 1                           
    s 2                           
    i 1                           
    p 2                           
    i 1                           
    : ragoo                       
    r 1                           
    a 1                           
    g 1                           
    o 2                           
    : Gooooooooooooooaaaaaaaallllll!!
    G 1                              
    o 14                             
    a 8                              
    l 6                              
    ! 2
    Since that accurately counts all letters it is simply a matter of adjusting the exact display logic with a few ifs. Or, if you're getting really fancy, throwing it all into a dict of lists and extracting the information from that dict as needed.
    Actually yea that's sort of what I wanted I just realised my code is wrong because I didn't fully test it, thanks

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •