• Seb

    Sounds great! Looking forward to it.

    posted in Pythonista read more
  • Seb

    @mikael
    @7upser

    Thank you both for your help. I am going to do some lessons on using return in functions as I’ve realised I don’t fully understand it.

    7upser, I didn’t realise you could iterate without it being in a list so have removed that, and also simplified it so there isn’t two functions directly after each other changing the same variable. Is that something thats just ‘best practise?’ (Although just a fledgling hobby at the moment I don’t want to pick up bad habits)

    Mikael that is a frustratingly short solution! I have a lot to learn. Thanks for your help.

    Here is the code now it works :) I just need to convert to letters which i’ll look at shortly.

    keys = '12233333333337777777777'
    sameletter = True           
    
    def splitter(keys):
        global sameletter
        split_letters = []
        buffer = []
        previousdigit = ''
        for digit in keys:
            if digit in {previousdigit, "''"}:
                check_buffer_len(buffer, previousdigit)
            else: 
                sameletter = False
                    
            if sameletter == True:
                buffer.append(digit)
                previousdigit = digit
            else:
                split_letters.append(buffer)
                buffer = []
                buffer.append(digit)
                previousdigit = digit
                
        split_letters.append(buffer)
                
        print(split_letters)        
                
        
    def check_buffer_len(buffer, previousdigit):    
        global sameletter
        if len(buffer) < 3:
            sameletter = True
            return
        if len(buffer) < 4 and previousdigit in {'7','9'}:
            sameletter = True
        else:
            sameletter = False

    posted in Pythonista read more
  • Seb

    @stephen said:

    but i get

    NameError: name 'split_letters' is not defined

    Thanks, have edited it now. Do you still get ‘split_letters’ is not defined? The code runs for me but I just get a blank output ‘[ ]’

    posted in Pythonista read more
  • Seb

    @mikael said:

    @Seb, please surround your code with three back ticks (```) to make it readable.

    Meanwhile, of course, there already is a function for the grouping. The following gives you a list of tuples, where the first item is the key pressed, and the second is how many times it was pressed.

    import itertools
    
    digits = [
        (key, len(list(grouper)))
        for key, grouper
        in itertools.groupby('4433555555666')
    ]
    
    print(digits)
    

    Thanks! now edited.

    groupby is interesting, thanks for that suggestion. My original reasoning for splitting the digits up though, is if the string has ‘333333’ it would end up being ‘333’, ‘333’ and translate to ‘f’,’f’ eventually. However if it was ‘777777’ you can press 7 four times not three, so the grouping would end up ‘7777’, ‘77’.

    I am trying to find an elegant solution to splitting the numbers either when they change or when they’ve reached the repeat limit (and cycle to the next letter)

    If i run the groupby solution I would end up with (‘3’, 6), (‘7’, 6) which could be useful but would then still need splitting again, if you see what I mean?

    posted in Pythonista read more
  • Seb

    Hi, I am pretty new to coding in general and have been following lots of tutorials to try and learn python. I am attempting a question from one of the tutorials, translating digits to letters.

    def keypad_string(keys):
        '''
        Given a string consisting of 0-9,
        find the string that is created using
        a standard phone keypad
        
        | 1         | 2 (abc)| 3 (def)  |
        | 4 (ghi)   | 5 (jkl)   | 6 (mno)|
        | 7 (pqrs)| 8 (tuv) | 9 (wxyz)|
        |   *   |   0 ()    |   #   |
        
        You can ignore 1, and 0 corresponds to space
            >>> keypad_string('12345')
        'adgj'
        >>> keypad_string('4433555555666')
        'hello'
        >>> keypad_string('2022')
        'a b'
        >>> keypad_string('')
        ''
        >>> keypad_string('111')
        ''
    

    The way I thought of initially was to split all the letters into their corresponding groups before translating them. So a string ‘1223333’ would end up being ‘1’, ‘22’, ‘333’, ‘3’. I am aiming to check the digit in the string, see if it is the same as the previous digit, if not move the digit into the final string, and add the new digit into the buffer to correctly split the numbers into their groups.

    Here is what I have so far, but I can’t seem to get the True/ False variables to change, so everything ends up in the buffer + final string!

    keys = '1223333'
    keylist = list(keys)
    global sameletter
    sameletter = True           
    
    def splitter(keylist):
        split_letters = []
        buffer = []
        previousdigit = ''
        for digit in keylist:   
            check_previous(digit, previousdigit)    
            check_buffer_len(buffer, previousdigit)
            if sameletter == True:
                buffer.append(digit)
                previousdigit = digit
            else:
                split_letters.append(buffer)
                buffer.clear
                buffer.append(digit)
                previousdigit = digit
                
        print(split_letters)        
                
    def check_previous(digit, previousdigit):
        if digit != previousdigit:
            sameletter = False
    
    def check_buffer_len(buffer, previousdigit):
        if len(buffer) == 3:
            if previousdigit in {'7','9'}:
                sameletter = True
            else:
                sameletter = False
    
                
    splitter(keys)
    

    I think the problem, looking through it with the debugger, is that the sameletter variable never actually changes to false? Would anybody be able to help

    Thanks!

    posted in Pythonista read more
  • Seb

    Just wanted to say as a complete beginner this is really helpful! I look forward to the next episode :)

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!