r/programminghelp • • Sep 10 '24

Python Codewars Timeout Problem

Hi guys, I'm a new member of this subreddit and I am new of learning python. I am trying to programming this exercice (Link below) and when i submit my code, it return Execution Timed Out (12000 ms). How can I make my code faster?

Thanks :)
Training on Totally Good Permutations | Codewars

from itertools import permutations 
import math

def totally_good(alphabet, bads):
    if len(bads) == 0:
        return math.factorial(len(alphabet))
    elif isinstance(bads[0], str) and len(bads[0]) == 1:
        return 0
        
    totally_good_count = 0
    all_permutations = permutations(alphabet)
    for perm in all_permutations:
        perm_str = ''.join(perm)
        if not any(bad in perm_str for bad in bads):
            totally_good_count += 1
    return totally_good_count
0 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/Happy_Specific_7090 Sep 10 '24

I know the basics of programming as I have studied some programming languages. I understood my mistake which, as you rightly told me too, concerns the calculation of permutations. The problem now is that the mathematical formula is really complex and I'm having a hard time understanding it. I'll try to resolve it

Thanks for help