diff --git a/.gitignore b/.gitignore index 68ca39690..8d9f40edd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,9 +11,6 @@ tex2pdf* .coverage .idea .vscode -02_crowsnest/crowsnest.py -03_picnic/picnic.py -04_jump_the_five/jump.py 05_howler/howler.py 06_wc/wc.py 07_gashlycrumb/gashlycrumb.py diff --git a/01_hello/hello.py b/01_hello/hello.py new file mode 100644 index 000000000..964bf37cb --- /dev/null +++ b/01_hello/hello.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +""" +Author: Jeffrey Schmid-Paz +Purpose: Say hello +""" + +import argparse + + +def get_args(): + """Get the command-line arguments""" + parser = argparse.ArgumentParser(description="Say hello") + parser.add_argument( + "-n", "--name", metavar="name", default="World", help="Name to greet" + ) + return parser.parse_args() + + +def main(): + """Run the program""" + args = get_args() + print("Hello, " + args.name + "!") + + +if __name__ == "__main__": + main() diff --git a/01_hello/test.py b/01_hello/test.py index d0cedd1c2..6d05ffeb0 100755 --- a/01_hello/test.py +++ b/01_hello/test.py @@ -4,7 +4,7 @@ import os from subprocess import getstatusoutput, getoutput -prg = './hello.py' +prg = 'hello.py' # -------------------------------------------------- diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py new file mode 100644 index 000000000..6c533dfd3 --- /dev/null +++ b/02_crowsnest/crowsnest.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +""" +Author : Jeffrey Schmid-Paz +Date : 2023-01-02 +Purpose: Warn captain for a something near the ship +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Crow's Nest -- choose the correct article", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument("word", metavar="word", help="A word") + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Run the program""" + + args = get_args() + word = args.word + article = "an" if word[0].lower() in "aeiou" else "a" + + print(f"Ahoy, Captain, {article} {word} off the larboard bow!") + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/02_crowsnest/test.py b/02_crowsnest/test.py index d4e25c6c7..5ac387931 100755 --- a/02_crowsnest/test.py +++ b/02_crowsnest/test.py @@ -4,15 +4,32 @@ import os from subprocess import getstatusoutput, getoutput -prg = './crowsnest.py' +prg = "crowsnest.py" consonant_words = [ - 'brigantine', 'clipper', 'dreadnought', 'frigate', 'galleon', 'haddock', - 'junk', 'ketch', 'longboat', 'mullet', 'narwhal', 'porpoise', 'quay', - 'regatta', 'submarine', 'tanker', 'vessel', 'whale', 'xebec', 'yatch', - 'zebrafish' + "brigantine", + "clipper", + "dreadnought", + "frigate", + "galleon", + "haddock", + "junk", + "ketch", + "longboat", + "mullet", + "narwhal", + "porpoise", + "quay", + "regatta", + "submarine", + "tanker", + "vessel", + "whale", + "xebec", + "yatch", + "zebrafish", ] -vowel_words = ['aviso', 'eel', 'iceberg', 'octopus', 'upbound'] -template = 'Ahoy, Captain, {} {} off the larboard bow!' +vowel_words = ["aviso", "eel", "iceberg", "octopus", "upbound"] +template = "Ahoy, Captain, {} {} off the larboard bow!" # -------------------------------------------------- @@ -26,10 +43,10 @@ def test_exists(): def test_usage(): """usage""" - for flag in ['-h', '--help']: - rv, out = getstatusoutput(f'{prg} {flag}') + for flag in ["-h", "--help"]: + rv, out = getstatusoutput(f"{prg} {flag}") assert rv == 0 - assert out.lower().startswith('usage') + assert out.lower().startswith("usage") # -------------------------------------------------- @@ -37,8 +54,8 @@ def test_consonant(): """brigantine -> a brigantine""" for word in consonant_words: - out = getoutput(f'{prg} {word}') - assert out.strip() == template.format('a', word) + out = getoutput(f"{prg} {word}") + assert out.strip() == template.format("a", word) # -------------------------------------------------- @@ -46,8 +63,8 @@ def test_consonant_upper(): """brigantine -> a Brigatine""" for word in consonant_words: - out = getoutput(f'{prg} {word.title()}') - assert out.strip() == template.format('a', word.title()) + out = getoutput(f"{prg} {word.title()}") + assert out.strip() == template.format("a", word.title()) # -------------------------------------------------- @@ -55,8 +72,8 @@ def test_vowel(): """octopus -> an octopus""" for word in vowel_words: - out = getoutput(f'{prg} {word}') - assert out.strip() == template.format('an', word) + out = getoutput(f"{prg} {word}") + assert out.strip() == template.format("an", word) # -------------------------------------------------- @@ -64,5 +81,5 @@ def test_vowel_upper(): """octopus -> an Octopus""" for word in vowel_words: - out = getoutput(f'{prg} {word.upper()}') - assert out.strip() == template.format('an', word.upper()) + out = getoutput(f"{prg} {word.upper()}") + assert out.strip() == template.format("an", word.upper()) diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py new file mode 100644 index 000000000..74d2ee086 --- /dev/null +++ b/03_picnic/picnic.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +""" +Author : Jeffrey Schmid-Paz +Date : 2023-01-05 +Purpose: Create a list of items to bring along a picnic +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Picnic game", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument("item", metavar="str", help="Item(s) to bring", nargs="+") + + parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true") + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + items = args.item + + if args.sorted == True: + items.sort() + + items_to_bring = "" + if len(items) >= 3: + items.insert(-1, "and ") + items_to_bring = ", ".join(items[:-1]) + items[-1] + elif len(items) == 2: + items_to_bring = " and ".join(items) + else: + items_to_bring = items[0] + + print(f"You are bringing {items_to_bring}.") + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/03_picnic/test.py b/03_picnic/test.py index 281fab552..f70b1ad7f 100755 --- a/03_picnic/test.py +++ b/03_picnic/test.py @@ -4,7 +4,7 @@ import os from subprocess import getoutput -prg = './picnic.py' +prg = "picnic.py" # -------------------------------------------------- @@ -18,17 +18,17 @@ def test_exists(): def test_usage(): """usage""" - for flag in ['', '-h', '--help']: - out = getoutput(f'{prg} {flag}') - assert out.lower().startswith('usage') + for flag in ["", "-h", "--help"]: + out = getoutput(f"{prg} {flag}") + assert out.lower().startswith("usage") # -------------------------------------------------- def test_one(): """one item""" - out = getoutput(f'{prg} chips') - assert out.strip() == 'You are bringing chips.' + out = getoutput(f"{prg} chips") + assert out.strip() == "You are bringing chips." # -------------------------------------------------- @@ -36,7 +36,7 @@ def test_two(): """two items""" out = getoutput(f'{prg} soda "french fries"') - assert out.strip() == 'You are bringing soda and french fries.' + assert out.strip() == "You are bringing soda and french fries." # -------------------------------------------------- @@ -44,9 +44,10 @@ def test_more_than_two(): """more than two items""" arg = '"potato chips" coleslaw cupcakes "French silk pie"' - out = getoutput(f'{prg} {arg}') - expected = ('You are bringing potato chips, coleslaw, ' - 'cupcakes, and French silk pie.') + out = getoutput(f"{prg} {arg}") + expected = ( + "You are bringing potato chips, coleslaw, " "cupcakes, and French silk pie." + ) assert out.strip() == expected @@ -54,15 +55,15 @@ def test_more_than_two(): def test_two_sorted(): """two items sorted output""" - out = getoutput(f'{prg} -s soda candy') - assert out.strip() == 'You are bringing candy and soda.' + out = getoutput(f"{prg} -s soda candy") + assert out.strip() == "You are bringing candy and soda." # -------------------------------------------------- def test_more_than_two_sorted(): """more than two items sorted output""" - arg = 'bananas apples dates cherries' - out = getoutput(f'{prg} {arg} --sorted') - expected = ('You are bringing apples, bananas, cherries, and dates.') + arg = "bananas apples dates cherries" + out = getoutput(f"{prg} {arg} --sorted") + expected = "You are bringing apples, bananas, cherries, and dates." assert out.strip() == expected diff --git a/04_jump_the_five/jump.py b/04_jump_the_five/jump.py new file mode 100644 index 000000000..a0f3b8890 --- /dev/null +++ b/04_jump_the_five/jump.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +""" +Author : Jeffrey Paz-Schmid +Date : 2023-01-22 +Purpose: Encode Phone-Numbers +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Jump the Five", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument("text", metavar="str", help="Input text") + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + + jumper = { + "1": "9", + "2": "8", + "3": "7", + "4": "6", + "5": "0", + "6": "4", + "7": "3", + "8": "2", + "9": "1", + "0": "5", + } + + print("".join([jumper.get(char, char) for char in args.text])) + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/04_jump_the_five/test.py b/04_jump_the_five/test.py index 5dc89e311..c19dfac42 100755 --- a/04_jump_the_five/test.py +++ b/04_jump_the_five/test.py @@ -4,7 +4,7 @@ import os from subprocess import getstatusoutput -prg = './jump.py' +prg = "jump.py" # -------------------------------------------------- @@ -18,19 +18,19 @@ def test_exists(): def test_usage(): """usage""" - for flag in ['-h', '--help']: - rv, out = getstatusoutput(f'{prg} {flag}') + for flag in ["-h", "--help"]: + rv, out = getstatusoutput(f"{prg} {flag}") assert rv == 0 - assert out.lower().startswith('usage') + assert out.lower().startswith("usage") # -------------------------------------------------- def test_01(): """test""" - rv, out = getstatusoutput(f'{prg} 123-456-7890') + rv, out = getstatusoutput(f"{prg} 123-456-7890") assert rv == 0 - assert out == '987-604-3215' + assert out == "987-604-3215" # -------------------------------------------------- @@ -39,4 +39,4 @@ def test_02(): rv, out = getstatusoutput(f'{prg} "That number to call is 098-765-4321."') assert rv == 0 - assert out.rstrip() == 'That number to call is 512-340-6789.' + assert out.rstrip() == "That number to call is 512-340-6789."