Skip to content

Instantly share code, notes, and snippets.

@sleeyax
Created May 27, 2019 16:26
Show Gist options
  • Save sleeyax/6b281ed79148bca82b7c723fa5b8b6d7 to your computer and use it in GitHub Desktop.
Save sleeyax/6b281ed79148bca82b7c723fa5b8b6d7 to your computer and use it in GitHub Desktop.
Generate all possible combinations w/o itertools
# @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)
@sleeyax
Copy link
Author

sleeyax commented May 27, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment