File tree Expand file tree Collapse file tree 13 files changed +695
-23
lines changed
Expand file tree Collapse file tree 13 files changed +695
-23
lines changed Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments