• Takapapatapaka@lemmy.world
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 month ago

    For anyone curious but not wanting to read the article :

    The python fastest ways of the base article are mainly using a for loop and using regex (first one is faster on small string, regex becomes faster around 25 characters).

    For loop :

    def loop_in(s):
        for c in s:
            if c in "aeiouAEIOU":
                return True 
        return False 
    

    Regex :

    import re
    def regex(s):
        return bool(re.search(r'[aeiouAEIOU]', s))
    

    There are updates to the article, suggesting faster ways. First one uses the find() function, second one swaps the strings of the For Loop method :

    def loop_in_perm(s):
            for c in "aeiouAEIOU":
                if c in s:
                    return True
            return False
    
  • TaviRider@reddthat.com
    link
    fedilink
    arrow-up
    2
    ·
    1 month ago

    The recursive solution could have used tail recursion to operate on strings of any size without using O(n) stack space. By just incrementing the string pointer it could do it with just one string buffer too.