Search code examples
pythontaskfindall

findall Python change syntax of re.findall Word a10n


Im stuck in my re findall syntax

new_s = re.findall(r"[A-Za-z@#0-9]+|\W+", s)

But I am not familiar with this re syntax, right now I make 50% of "Word a10n (abbreviation)" kata.

is it possible to change re.findall sitntax to complete this kata?


import re
def abbreviate(s):
    
    new_s = re.findall(r"[A-Za-z@#0-9]+|\W+", s)
    n = []
    for i in new_s:    
        if len(i)>3 and i.isalpha():
            l = len(i)-2
            n.append(f"{i[0]}{l}{i[-1]}")
        else:
            n.append(i)
    
    return "".join(n)

source https://www.codewars.com/kata/5375f921003bf62192000746/train/python


Solution

  • You got some points missing, but nice usage of findall, I like it and great job :)


    1. You separated each "part" by piping | in the regex pattern, but you should pipe the numbers as well!

    2. You need to separate the "special separators/characters" as well! i.e. the @#_ characters.

    So your regex pattern should be:

    r"[A-Za-z]+|\W+|[0-9]|[@#_]"