Skip to content

Commit 9ddbc92

Browse files
committed
more solutions, new password, template
1 parent c2529c1 commit 9ddbc92

File tree

13 files changed

+695
-23
lines changed

13 files changed

+695
-23
lines changed

apples_and_bananas/solution1.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 1: Iterate every character
44+
new_text = []
45+
for char in text:
46+
if char in 'aeiou':
47+
new_text.append(vowel)
48+
elif char in 'AEIOU':
49+
new_text.append(vowel.upper())
50+
else:
51+
new_text.append(char)
52+
text = ''.join(new_text)
53+
54+
print(text)
55+
56+
57+
# --------------------------------------------------
58+
if __name__ == '__main__':
59+
main()

apples_and_bananas/solution2.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 2: str.replace
44+
for v in 'aeiou':
45+
text = text.replace(v, vowel).replace(v.upper(), vowel.upper())
46+
47+
print(text)
48+
49+
50+
# --------------------------------------------------
51+
if __name__ == '__main__':
52+
main()

apples_and_bananas/solution3.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 3: str.translate
44+
trans = str.maketrans('aeiouAEIOU', vowel * 5 + vowel.upper() * 5)
45+
text = text.translate(trans)
46+
47+
print(text)
48+
49+
50+
# --------------------------------------------------
51+
if __name__ == '__main__':
52+
main()

apples_and_bananas/solution4.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 4: Use a list comprehension
44+
new_text = [
45+
vowel if c in 'aeiou' else vowel.upper() if c in 'AEIOU' else c
46+
for c in text
47+
]
48+
text = ''.join(new_text)
49+
50+
print(text)
51+
52+
53+
# --------------------------------------------------
54+
if __name__ == '__main__':
55+
main()

apples_and_bananas/solution5.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 5: Define a function, use list comprehension
44+
def new_char(c):
45+
return vowel if c in 'aeiou' else vowel.upper() if c in 'AEIOU' else c
46+
47+
text = ''.join([new_char(c) for c in text])
48+
49+
print(text)
50+
51+
52+
# --------------------------------------------------
53+
if __name__ == '__main__':
54+
main()

apples_and_bananas/solution6.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 6: Use a `map` to iterate with a `lambda`
44+
text = ''.join(
45+
map(
46+
lambda c: vowel if c in 'aeiou' else vowel.upper()
47+
if c in 'AEIOU' else c, text))
48+
49+
print(text)
50+
51+
52+
# --------------------------------------------------
53+
if __name__ == '__main__':
54+
main()

apples_and_bananas/solution7.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Apples and Bananas"""
3+
4+
import argparse
5+
import os
6+
import re
7+
8+
9+
# --------------------------------------------------
10+
def get_args():
11+
"""get command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(
14+
description='Apples and bananas',
15+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
16+
17+
parser.add_argument('text', metavar='str', help='Input text or file')
18+
19+
parser.add_argument('-v',
20+
'--vowel',
21+
help='The vowel to substitute',
22+
metavar='str',
23+
type=str,
24+
default='a',
25+
choices=list('aeiou'))
26+
27+
args = parser.parse_args()
28+
29+
if os.path.isfile(args.text):
30+
args.text = open(args.text).read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
vowel = args.vowel
42+
43+
# Method 7: `map` with the function
44+
def new_char(c):
45+
return vowel if c in 'aeiou' else vowel.upper() if c in 'AEIOU' else c
46+
47+
text = ''.join(map(new_char, text))
48+
49+
print(text)
50+
51+
52+
# --------------------------------------------------
53+
if __name__ == '__main__':
54+
main()

0 commit comments

Comments
 (0)