From 40f5581956c08686322129ee535c97891ab3f8b6 Mon Sep 17 00:00:00 2001 From: Leandro Espino Date: Tue, 28 Nov 2023 21:35:34 -0600 Subject: [PATCH] Reto #36 - python --- .../python/EspinoLeandroo.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Retos/Reto #36 - PERMUTACIONES [Media]/python/EspinoLeandroo.py diff --git a/Retos/Reto #36 - PERMUTACIONES [Media]/python/EspinoLeandroo.py b/Retos/Reto #36 - PERMUTACIONES [Media]/python/EspinoLeandroo.py new file mode 100644 index 0000000000..778816f57f --- /dev/null +++ b/Retos/Reto #36 - PERMUTACIONES [Media]/python/EspinoLeandroo.py @@ -0,0 +1,19 @@ +def generate_permutations(word): + def generate_permutations_helper(word, index, permutations): + if index == len(word) - 1: + permutations.append(''.join(word)) + else: + for i in range(index, len(word)): + word[index], word[i] = word[i], word[index] + generate_permutations_helper(word, index + 1, permutations) + word[index], word[i] = word[i], word[index] # Deshacer el intercambio para volver al estado original + + permutations = [] + generate_permutations_helper(list(word), 0, permutations) + return permutations + +if __name__ == "__main__": + word = "Leandro" + permutations = generate_permutations(word) + for permutation in permutations: + print(permutation)