-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Switch case #7995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch case #7995
Changes from 9 commits
f97cda9
b9013e5
d4d1ef8
c4b81c2
a4a47ab
2e1aa1a
a493d79
80372da
cca3470
d11c7c9
358abac
479a637
f38c544
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
def remove_digit(num: int) -> int: | ||
""" | ||
|
||
returns the biggest possible result | ||
that can be achieved by removing | ||
one digit from the given number | ||
|
||
>>> remove_digit(152) | ||
52 | ||
>>> remove_digit(6385) | ||
685 | ||
>>> remove_digit(-11) | ||
1 | ||
>>> remove_digit(2222222) | ||
222222 | ||
>>> remove_digit("2222222") | ||
Traceback (most recent call last): | ||
TypeError: only integers accepted as input | ||
>>> remove_digit("string input") | ||
Traceback (most recent call last): | ||
TypeError: only integers accepted as input | ||
""" | ||
|
||
if isinstance(num, int): | ||
num_str = str(abs(num)) | ||
num_transpositions = [list(num_str) for char in range(len(num_str))] | ||
for index in range(len(num_str)): | ||
num_transpositions[index].pop(index) | ||
return sorted( | ||
meg-1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(int("".join(list(transposition))) for transposition in num_transpositions), | ||
reverse=True, | ||
)[0] | ||
else: | ||
raise TypeError("only integers accepted as input") | ||
|
||
|
||
if __name__ == "__main__": | ||
__import__("doctest").testmod() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import re | ||
|
||
""" | ||
general info: | ||
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby | ||
|
||
pascal case [ an upper Camel Case ]: https://en.wikipedia.org/wiki/Camel_case | ||
|
||
camel case: https://en.wikipedia.org/wiki/Camel_case | ||
|
||
kebab case [ can be found in general info ]: | ||
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby | ||
|
||
snake case: https://en.wikipedia.org/wiki/Snake_case | ||
""" | ||
|
||
# assistant functions | ||
def split_input(str_: str) -> str: | ||
meg-1 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]", str_)] | ||
|
||
|
||
def to_simple_case(str_: str) -> str: | ||
meg-1 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
string_split = split_input(str_) | ||
return "".join( | ||
["".join([char.capitalize() for char in sub_str]) for sub_str in string_split] | ||
) | ||
|
||
|
||
def to_complex_case(text: str, upper: bool, separator: str) -> str: | ||
meg-1 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
try: | ||
string_split = split_input(text) | ||
if upper: | ||
res_str = "".join( | ||
[ | ||
separator.join([char.upper() for char in sub_str]) | ||
for sub_str in string_split | ||
] | ||
) | ||
else: | ||
res_str = "".join( | ||
[ | ||
separator.join([char.lower() for char in sub_str]) | ||
for sub_str in string_split | ||
] | ||
) | ||
return res_str | ||
except IndexError: | ||
return "not valid string" | ||
|
||
|
||
# main content | ||
def to_pascal_case(text: str) -> str: | ||
""" | ||
>>> to_pascal_case("one two 31235three4four") | ||
'OneTwo31235three4four' | ||
""" | ||
return to_simple_case(text) | ||
|
||
|
||
def to_camel_case(text: str) -> str: | ||
""" | ||
>>> to_camel_case("one two 31235three4four") | ||
'oneTwo31235three4four' | ||
""" | ||
try: | ||
res_str = to_simple_case(text) | ||
return res_str[0].lower() + res_str[1:] | ||
except IndexError: | ||
return "not valid string" | ||
|
||
|
||
def to_snake_case(text: str, upper: bool) -> str: | ||
""" | ||
>>> to_snake_case("one two 31235three4four", True) | ||
'ONE_TWO_31235THREE4FOUR' | ||
>>> to_snake_case("one two 31235three4four", False) | ||
'one_two_31235three4four' | ||
""" | ||
return to_complex_case(text, upper, "_") | ||
|
||
|
||
def to_kebab_case(text: str, upper: bool) -> str: | ||
""" | ||
>>> to_kebab_case("one two 31235three4four", True) | ||
'ONE-TWO-31235THREE4FOUR' | ||
>>> to_kebab_case("one two 31235three4four", False) | ||
'one-two-31235three4four' | ||
""" | ||
return to_complex_case(text, upper, "-") | ||
|
||
|
||
if __name__ == "__main__": | ||
__import__("doctest").testmod() |
Uh oh!
There was an error while loading. Please reload this page.