Skip to content

Commit 997d56f

Browse files
authored
Switch case (TheAlgorithms#7995)
1 parent 44aa17f commit 997d56f

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

strings/string_switch_case.py

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import re
2+
3+
"""
4+
general info:
5+
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby
6+
7+
pascal case [ an upper Camel Case ]: https://en.wikipedia.org/wiki/Camel_case
8+
9+
camel case: https://en.wikipedia.org/wiki/Camel_case
10+
11+
kebab case [ can be found in general info ]:
12+
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby
13+
14+
snake case: https://en.wikipedia.org/wiki/Snake_case
15+
"""
16+
17+
18+
# assistant functions
19+
def split_input(str_: str) -> list:
20+
"""
21+
>>> split_input("one two 31235three4four")
22+
[['one', 'two', '31235three4four']]
23+
"""
24+
return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]", str_)]
25+
26+
27+
def to_simple_case(str_: str) -> str:
28+
"""
29+
>>> to_simple_case("one two 31235three4four")
30+
'OneTwo31235three4four'
31+
"""
32+
string_split = split_input(str_)
33+
return "".join(
34+
["".join([char.capitalize() for char in sub_str]) for sub_str in string_split]
35+
)
36+
37+
38+
def to_complex_case(text: str, upper: bool, separator: str) -> str:
39+
"""
40+
>>> to_complex_case("one two 31235three4four", True, "_")
41+
'ONE_TWO_31235THREE4FOUR'
42+
>>> to_complex_case("one two 31235three4four", False, "-")
43+
'one-two-31235three4four'
44+
"""
45+
try:
46+
string_split = split_input(text)
47+
if upper:
48+
res_str = "".join(
49+
[
50+
separator.join([char.upper() for char in sub_str])
51+
for sub_str in string_split
52+
]
53+
)
54+
else:
55+
res_str = "".join(
56+
[
57+
separator.join([char.lower() for char in sub_str])
58+
for sub_str in string_split
59+
]
60+
)
61+
return res_str
62+
except IndexError:
63+
return "not valid string"
64+
65+
66+
# main content
67+
def to_pascal_case(text: str) -> str:
68+
"""
69+
>>> to_pascal_case("one two 31235three4four")
70+
'OneTwo31235three4four'
71+
"""
72+
return to_simple_case(text)
73+
74+
75+
def to_camel_case(text: str) -> str:
76+
"""
77+
>>> to_camel_case("one two 31235three4four")
78+
'oneTwo31235three4four'
79+
"""
80+
try:
81+
res_str = to_simple_case(text)
82+
return res_str[0].lower() + res_str[1:]
83+
except IndexError:
84+
return "not valid string"
85+
86+
87+
def to_snake_case(text: str, upper: bool) -> str:
88+
"""
89+
>>> to_snake_case("one two 31235three4four", True)
90+
'ONE_TWO_31235THREE4FOUR'
91+
>>> to_snake_case("one two 31235three4four", False)
92+
'one_two_31235three4four'
93+
"""
94+
return to_complex_case(text, upper, "_")
95+
96+
97+
def to_kebab_case(text: str, upper: bool) -> str:
98+
"""
99+
>>> to_kebab_case("one two 31235three4four", True)
100+
'ONE-TWO-31235THREE4FOUR'
101+
>>> to_kebab_case("one two 31235three4four", False)
102+
'one-two-31235three4four'
103+
"""
104+
return to_complex_case(text, upper, "-")
105+
106+
107+
if __name__ == "__main__":
108+
__import__("doctest").testmod()

0 commit comments

Comments
 (0)