X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ tex2pdf*
19_wod/wod.py
20_password/password.py
21_tictactoe/tictactoe.py
/venv
31 changes: 31 additions & 0 deletions 01_hello/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
"""
Tut author: Ken Youens-Clark
Author: Petra Power
Purpose: Say hello
"""

import argparse


# ----------------- Ken likes to put lines to visually identify individual functions
def get_args():
"""
Get 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():
"""
Pylint will require docstring at top of function to document what it does
"""
args = get_args()
print('Hello, ' + args.name + '!')


if __name__ == '__main__':
main()
39 changes: 39 additions & 0 deletions 02_crowsnest/crowsnest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""
Author : petra
Date : 2021-01-26
Purpose: Crow's Nest
"""

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():
"""Make a jazz noise here"""

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()
45 changes: 45 additions & 0 deletions 02_crowsnest/crowsnest_capital.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""
Author : petra
Date : 2021-01-26
Purpose: Crow's Nest
"""

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():
"""Make a jazz noise here"""

args = get_args()
word = args.word
char = word[0]
article = ''

if char.islower():
article = 'an' if word[0] in 'aeiou' else 'a'
else:
article = 'An' if word[0] in 'AEIOU' else 'A'

print(f'Ahoy, Captain, {article} {word} off the larboard bow!')


# --------------------------------------------------
if __name__ == '__main__':
main()
68 changes: 68 additions & 0 deletions 02_crowsnest/test_capital.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""tests for crowsnest.py"""

import os
from subprocess import getstatusoutput, getoutput

prg = './crowsnest_capital.py'
consonant_words = [
'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!'


# --------------------------------------------------
def test_exists():
"""exists"""

assert os.path.isfile(prg)


# --------------------------------------------------
def test_usage():
"""usage"""

for flag in ['-h', '--help']:
rv, out = getstatusoutput(f'{prg} {flag}')
assert rv == 0
assert out.lower().startswith('usage')


# --------------------------------------------------
def test_consonant():
"""brigantine -> a brigantine"""

for word in consonant_words:
out = getoutput(f'{prg} {word}')
assert out.strip() == template.format('a', word)


# --------------------------------------------------
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())


# --------------------------------------------------
def test_vowel():
"""octopus -> an octopus"""

for word in vowel_words:
out = getoutput(f'{prg} {word}')
assert out.strip() == template.format('an', word)


# --------------------------------------------------
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())
56 changes: 56 additions & 0 deletions 03_picnic/picnic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
"""
Author : petra <petra@localhost>
Date : 2021-01-30
Purpose: Picnic game
"""

import argparse


# --------------------------------------------------
def get_args():
"""Get command-line arguments"""

parser = argparse.ArgumentParser(
description='Picnic game',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('str',
metavar='str',
nargs='+', # one or more expected
type=str,
help='Item(s) to bring')

parser.add_argument('-s',
'--sorted',
help='Sort the items',
action='store_true') # if option is used by user, set to True, default is False

return parser.parse_args()


# --------------------------------------------------
def main():
"""Make a jazz noise here"""

args = get_args()
items = args.str
bringing = ''

if args.sorted:
items.sort()

if len(items) == 1:
bringing = items[0]
elif len(items) == 2:
bringing = ' and '.join(items)
else:
bringing = ', '.join(items[:len(items)-1]) + ', and ' + items[-1]

print(f'You are bringing {bringing}.')


# --------------------------------------------------
if __name__ == '__main__':
main()
X Tutup