Created
May 27, 2019 16:26
-
-
Save sleeyax/6b281ed79148bca82b7c723fa5b8b6d7 to your computer and use it in GitHub Desktop.
Generate all possible combinations w/o itertools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @description: generate all possible characters & combinations of a given string without using libraries | |
# @syntax: | |
# generate(string_to_array("abcdef"), 4) | |
# generate(['a', 'b', 'c'], 2) | |
def generate(chars, pos, combinations = []): | |
if len(combinations) == 0: | |
combinations = chars | |
if pos == 1: | |
return combinations | |
result_combinations = [] | |
for combination in combinations: | |
print(combination) | |
for char in chars: | |
result_combinations.append(combination + char) | |
array_print(result_combinations) | |
return generate(chars, pos-1, result_combinations) | |
def array_print(input): | |
for item in input: | |
print(item) | |
return | |
def string_to_array(string): | |
chars = [] | |
for char in string: | |
chars.append(char) | |
return chars | |
chars = "abcdefghijklmnopqrstuvwxyz" | |
generate(string_to_array(chars), 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an old script I made a while ago. I'm dumping it here, because I'm removing the repo it used to have.