To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
You could think of other ways to handle those as well, such as expand the list of supported characters, all depending on the scope of your function.
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
You could think of other ways to handle those as well, such as expand the list of supported characters, all depending on the scope of your function.
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
You could think of other ways to handle those as well, such as expand the list of supported characters, all depending on the scope of your function.
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
You could think of other ways to handle those as well, such as expand the list of supported characters, all depending on the scope of your function.
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
You could think of other ways to handle those as well, such as expand the list of supported characters, all depending on the scope of your function.
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']
To add a few things to Mathias' excellent answer...
First let's clean it up a bit. We'll put the testing code inside a main()
function, as the entry point of the program:
def main():
# Testing permutations
print(perms("miss"))
print(perms("okay"))
print(perms("missifsffs"))
Then we can run main()
if this script is ran as entry point/stand alone, but not run it if it is imported into another script. See this Stack Overflow answer for details about how this works.
if __name__ == "__main__":
main()
We can rename some things to make them more clear, as well as add Python 3's type hints in the permutation function:
letter_equivalents_map = {x[0]:x for x in list(zip(
string.ascii_lowercase,
string.ascii_uppercase))}
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
return out
After running it I noticed a problem, that is, if the input string the function is called with is not strictly made of letters, the program crashes with an error:
print(permutate_lower_upper("hello,world")) # KeyError: ','
The simplest fix would be to handle the error if it happens with try...except
:
def permutate_lower_upper(input_str: str) -> list:
input_str = input_str.lower()
out = [""]
try:
for letter in input_str:
out = [y+x for y in out for x in letter_equivalents_map[letter]]
except KeyError:
out = [""]
print("String containts invalid, non-letter characters")
return out
Then if we call the function with the same string, we get this:
print(permutate_lower_upper("hello,world")) # String containts invalid, non-letter characters # ['']