Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prac 09 feedback #6

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# CP1404 Practicals
# CP1404
## Practical work from Nele Rodenhagen
*I am a study abroad student from Germany*

Practical work for IT@JCU CP1404

I learned about clean code:
- good variable names are essential
- it is important to follow the conventions
- every function should only do one task

[Programming Pattern Page](https://github.com/CP1404/Starter/wiki/Programming-Patterns#decision-structures)

[CP1404 Instruction Repo](https://github.com/CP1404/Starter)
19 changes: 19 additions & 0 deletions prac_05/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
CP1404
collect email and name from user
"""

email = input("Email: ")
email_to_name = {}
while email != "":
name = (email.split("@")[0]).split(".")
name = (" ".join(name)).title()
print(f"Is your name {name}?")
answer = input("(Y/n) ").upper()
if answer != "Y" or not answer != "":
name = input("Name: ")
email_to_name[email] = name
email = input("Email: ")

for email, name in email_to_name.items():
print(f"{name} ({email})")
17 changes: 17 additions & 0 deletions prac_05/hex_colours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
CP1404 Practical
colours in HEX codes
"""

COLOR_TO_HEX = {'ALICEBLUE': '#f0f8ff', 'AMBER': '#ffbf00', 'AQUA': '#00ffff', 'BEIGE': '#f5f5dc',
'BRONZE': '#cd7f32', 'DARKGREEN': '#006400', 'DENIM': '#1560bd', 'GRAY': '#bebebe',
'MINT': '#3eb489', 'PINK': '#ffc0cb'}

color = input("Please insert a color: ").upper()

while color != "":
try:
print(COLOR_TO_HEX[color])
except KeyError:
print("You entered a false color")
color = input("Please insert a color: ").upper()
18 changes: 18 additions & 0 deletions prac_05/state_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
CP1404/CP5632 Practical
State names in a dictionary
File needs reformatting
"""

CODE_TO_NAME = {"QLD": "Queensland", "NSW": "New South Wales", "NT": "Northern Territory", "WA": "Western Australia",
"ACT": "Australian Capital Territory", "VIC": "Victoria", "TAS": "Tasmania"}
for code, name in CODE_TO_NAME.items():
print(f"{code} is {name}")

state_code = input("Enter short state: ").upper()
while state_code != "":
try:
print(state_code, "is", CODE_TO_NAME[state_code])
except KeyError:
print("Invalid short state")
state_code = input("Enter short state: ").upper()
55 changes: 55 additions & 0 deletions prac_05/wimbledon.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Year,Country,Champion,Country,Runner-up,Score in the final
1968,AUS,Rod Laver,AUS,Tony Roche,"6–3, 6–4, 6–2"
1969,AUS,Rod Laver,AUS,John Newcombe,"6–4, 5–7, 6–4, 6–4"
1970,AUS,John Newcombe,AUS,Ken Rosewall,"5–7, 6–3, 6–2, 3–6, 6–1"
1971,AUS,John Newcombe,USA,Stan Smith,"6–3, 5–7, 2–6, 6–4, 6–4"
1972,USA,Stan Smith,ROM,Ilie Năstase,"4–6, 6–3, 6–3, 4–6, 7–5"
1973,TCH,Jan Kodeš,URS,Alex Metreveli,"6–1, 9–8(7–5), 6–3"
1974,USA,Jimmy Connors,AUS,Ken Rosewall,"6–1, 6–1, 6–4"
1975,USA,Arthur Ashe,USA,Jimmy Connors,"6–1, 6–1, 5–7, 6–4"
1976,SWE,Björn Borg,ROM,Ilie Năstase,"6–4, 6–2, 9–7"
1977,SWE,Björn Borg,USA,Jimmy Connors,"3–6, 6–2, 6–1, 5–7, 6–4"
1978,SWE,Björn Borg,USA,Jimmy Connors,"6–2, 6–2, 6–3"
1979,SWE,Björn Borg,USA,Roscoe Tanner,"6–7(4–7), 6–1, 3–6, 6–3, 6–4"
1980,SWE,Björn Borg,USA,John McEnroe,"1–6, 7–5, 6–3, 6–7(16–18), 8–6"
1981,USA,John McEnroe,SWE,Björn Borg,"4–6, 7–6(7–1), 7–6(7–4), 6–4"
1982,USA,Jimmy Connors,USA,John McEnroe,"3–6, 6–3, 6–7(2–7), 7–6(7–5), 6–4"
1983,USA,John McEnroe,NZL,Chris Lewis,"6–2, 6–2, 6–2"
1984,USA,John McEnroe,USA,Jimmy Connors,"6–1, 6–1, 6–2"
1985,FRG,Boris Becker,USA,Kevin Curren,"6–3, 6–7(4–7), 7–6(7–3), 6–4"
1986,FRG,Boris Becker,TCH,Ivan Lendl,"6–4, 6–3, 7–5"
1987,AUS,Pat Cash,TCH,Ivan Lendl,"7–6(7–5), 6–2, 7–5"
1988,SWE,Stefan Edberg,FRG,Boris Becker,"4–6, 7–6(7–2), 6–4, 6–2"
1989,FRG,Boris Becker,SWE,Stefan Edberg,"6–0, 7–6(7–1), 6–4"
1990,SWE,Stefan Edberg,FRG,Boris Becker,"6–2, 6–2, 3–6, 3–6, 6–4"
1991,GER,Michael Stich,GER,Boris Becker,"6–4, 7–6(7–4), 6–4"
1992,USA,Andre Agassi,CRO,Goran Ivanišević,"6–7(8–10), 6–4, 6–4, 1–6, 6–4"
1993,USA,Pete Sampras,USA,Jim Courier,"7–6(7–3), 7–6(8–6), 3–6, 6–3"
1994,USA,Pete Sampras,CRO,Goran Ivanišević,"7–6(7–2), 7–6(7–5), 6–0"
1995,USA,Pete Sampras,GER,Boris Becker,"6–7(5–7), 6–2, 6–4, 6–2"
1996,NED,Richard Krajicek,USA,MaliVai Washington,"6–3, 6–4, 6–3"
1997,USA,Pete Sampras,FRA,Cédric Pioline,"6–4, 6–2, 6–4"
1998,USA,Pete Sampras,CRO,Goran Ivanišević,"6–7(2–7), 7–6(11–9), 6–4, 3–6, 6–2"
1999,USA,Pete Sampras,USA,Andre Agassi,"6–3, 6–4, 7–5"
2000,USA,Pete Sampras,AUS,Patrick Rafter,"6–7(10–12), 7–6(7–5), 6–4, 6–2"
2001,CRO,Goran Ivanišević,AUS,Patrick Rafter,"6–3, 3–6, 6–3, 2–6, 9–7"
2002,AUS,Lleyton Hewitt,ARG,David Nalbandian,"6–1, 6–3, 6–2"
2003,SUI,Roger Federer,AUS,Mark Philippoussis,"7–6(7–5), 6–2, 7–6(7–3)"
2004,SUI,Roger Federer,USA,Andy Roddick,"4–6, 7–5, 7–6(7–3), 6–4"
2005,SUI,Roger Federer,USA,Andy Roddick,"6–2, 7–6(7–2), 6–4"
2006,SUI,Roger Federer,ESP,Rafael Nadal,"6–0, 7–6(7–5), 6–7(2–7), 6–3"
2007,SUI,Roger Federer,ESP,Rafael Nadal,"7–6(9–7), 4–6, 7–6(7–3), 2–6, 6–2"
2008,ESP,Rafael Nadal,SUI,Roger Federer,"6–4, 6–4, 6–7(5–7), 6–7(8–10), 9–7"
2009,SUI,Roger Federer,USA,Andy Roddick,"5–7, 7–6(8–6), 7–6(7–5), 3–6, 16–14"
2010,ESP,Rafael Nadal,CZE,Tomáš Berdych,"6–3, 7–5, 6–4"
2011,SRB,Novak Djokovic,ESP,Rafael Nadal,"6–4, 6–1, 1–6, 6–3"
2012,SUI,Roger Federer,GBR,Andy Murray,"4–6, 7–5, 6–3, 6–4"
2013,GBR,Andy Murray,SRB,Novak Djokovic,"6–4, 7–5, 6–4"
2014,SRB,Novak Djokovic,SUI,Roger Federer,"6–7(7–9), 6–4, 7–6(7–4), 5–7, 6–4"
2015,SRB,Novak Djokovic,SUI,Roger Federer,"7–6(7–1), 6–7(10–12), 6–4, 6–3"
2016,GBR,Andy Murray,CAN,Milos Raonic,"6–4, 7–6(7–3), 7–6(7–2)"
2017,SUI,Roger Federer,CRO,Marin Čilić,"6–3, 6–1, 6–4"
2018,SRB,Novak Djokovic,RSA,Kevin Anderson,"6–2, 6–2, 7–6(7–3)"
2019,SRB,Novak Djokovic,SUI,Roger Federer,"7–6(7–5), 1–6, 7–6(7–4), 4–6, 13–12(7–3)"
2021,SRB,Novak Djokovic,ITA,Matteo Berrettini,"6–7(4–7), 6–4, 6–4, 6–3"
2022,SRB,Novak Djokovic,AUS,Nick Kyrgios,"4–6, 6–3, 6–4, 7–6(7–3)"
43 changes: 43 additions & 0 deletions prac_05/wimbledon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FILENAME = "wimbledon.csv"


def main():
in_file = open(FILENAME)
lines = in_file.readlines()[1:]

facts = generate_list(lines)

winner_countries, winners_to_count = calculate_winners_with_count_and_countries(facts)

print_winners_and_countries(winner_countries, winners_to_count)


def generate_list(lines):
facts = []
for line in lines:
facts.append((str(line).strip()).split(","))
return facts


def calculate_winners_with_count_and_countries(facts):
winners_to_count = {}
winner_countries = set()
for fact in facts:
print(fact[3])
winner_countries.add(fact[3])
if fact[2] in winners_to_count:
winners_to_count[fact[2]] += 1
else:
winners_to_count[fact[2]] = 1
return winner_countries, winners_to_count


def print_winners_and_countries(winner_countries, winners_to_count):
print("Wimbledon Champions:")
for winner, count in winners_to_count.items():
print(f"{winner} {count}")
print(f"These {len(winner_countries)} countries have won Wimbledon:")
print(", ".join(winner_countries))


main()
22 changes: 22 additions & 0 deletions prac_05/word_occurrences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
CP1404 Practical
count the occurrence of words in a text
Estimate: 20 Minutes
Actual: 17 Minutes
"""


text = input("Please insert a text: ")
words = text.split()

word_to_count = {}
for word in words:
if word in word_to_count:
word_to_count[word] += 1
else:
word_to_count[word] = 1

max_length = max([len(word) for word in word_to_count.keys()])

for word, count in sorted(word_to_count.items()):
print(f"{word:{max_length}} : {count:1}")
35 changes: 35 additions & 0 deletions prac_06/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""CP1404/CP5632 Practical - Car class example."""


class Car:
"""Represent a Car object."""

def __init__(self, name="", fuel=0):
"""Initialise a Car instance.

fuel: float, one unit of fuel drives one kilometre
"""
self.name = name
self.fuel = fuel
self._odometer = 0

def __str__(self):
return f"{self.name}, fuel={self.fuel}, odometer={self._odometer}"

def add_fuel(self, amount):
"""Add amount to the car's fuel."""
self.fuel += amount

def drive(self, distance):
"""Drive the car a given distance.

Drive given distance if car has enough fuel
or drive until fuel runs out return the distance actually driven.
"""
if distance > self.fuel:
distance = self.fuel
self.fuel = 0
else:
self.fuel -= distance
self._odometer += distance
return distance
25 changes: 25 additions & 0 deletions prac_06/guitar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
CP1404
estimated time: 20 Minutes
needed time: 26 Minutes
"""


class Guitar:

def __init__(self, name="", year=0, cost=0):
self.name = name
self.year = year
self.cost = cost

def __str__(self):
return "{} ({}) : ${}".format(self.name, self.year, self.cost)

def get_age(self):
return 2023 - self.year

def is_vintage(self):
if self.get_age() >= 50:
return True
else:
return False
11 changes: 11 additions & 0 deletions prac_06/guitar_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from guitar import Guitar

gibson = Guitar("Gibson L-5 CES", 1922, 16035.40)
another = Guitar("Another Guitar", 2013)
print(gibson)

print(f"{gibson.name} get_age() - expected 101. Got {gibson.get_age()}")
print(f"{another.name} get_age() - expected 10. Got {another.get_age()}")

print(f"{gibson.name} is_vintage() - expected True. Got {gibson.is_vintage()}")
print(f"{another.name} is_vintage() - expected False. Got {another.is_vintage()}")
17 changes: 17 additions & 0 deletions prac_06/guitars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from guitar import Guitar

print("My guitars!")
my_guitars = []
name = input("Name: ")

while name != "":
year = int(input("Year: "))
cost = float(input("Cost: $"))
new_guitar = Guitar(name, year, cost)
my_guitars.append(Guitar(name, year, cost))
name = input("Name: ")

print("These are my guitars:")
for i, guitar in enumerate(my_guitars, 1):
vintage_string = "vintage" if guitar.is_vintage() else ""
print(f"Guitar {i}: {guitar.name:>20} ({guitar.year}), worth $ {guitar.cost:.2f} {vintage_string}")
13 changes: 13 additions & 0 deletions prac_06/languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from programming_language import ProgrammingLanguage

python = ProgrammingLanguage("Python", "Dynamic", True, 1991)
ruby = ProgrammingLanguage("Ruby", "Dynamic", True, 1995)
visual_basic = ProgrammingLanguage("Visual Basic", "Static", False, 1991)
print(python)

languages = {python, ruby, visual_basic}

print("The dynamically typed languages are:")
for language in languages:
if language.is_dynamic():
print(language.name)
23 changes: 23 additions & 0 deletions prac_06/programming_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
CP1404
estimated time: 15 Minutes
needed time: 12 Minutes
"""


class ProgrammingLanguage:
def __init__(self, name="", typing="Static", reflection=True , year=0):
self.name = name
self.typing = typing.lower()
self.reflection = reflection
self.year = year

def __str__(self):
return f"{self.name}, {self.typing} Typing, {self.reflection}, {self.year}"

def is_dynamic(self):
if self.typing == "dynamic":
return True
else:
return False

23 changes: 23 additions & 0 deletions prac_06/used_car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
CP1404/CP5632 Practical - Client code to use the Car class.
Note that the import has a folder (module) in it.
This is why we name our folders with no spaces or capitals, as valid module names.
"""

from prac_06.car import Car


def main():
"""Demo test code to show how to use car class."""
my_car = Car("clara", 180)
my_car.drive(30)
print(f"Car has fuel: {my_car.fuel}")
print(my_car)

limo = Car("lara", 100)
limo.add_fuel(20)
print(f"{limo.fuel}")
limo.drive(115)
print(limo)

main()
33 changes: 33 additions & 0 deletions prac_07/guitar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
CP1404
estimated time: 20 Minutes
needed time: 26 Minutes
"""


class Guitar:

def __init__(self, name="", year=0, cost=0):
self.name = name
self.year = year
self.cost = cost

def __str__(self):
return "{} ({}) : ${}".format(self.name, self.year, self.cost)

def get_age(self):
return 2023 - self.year

def is_vintage(self):
if self.get_age() >= 50:
return True
else:
return False

def __lt__(self, other):
return self.year < other.year





8 changes: 8 additions & 0 deletions prac_07/guitars.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Gibson L-5 CES,1922,16035.4
Gretsch 6120 Chet Atkins,1956,10999.99
xxx,1998,888.0
Line 6 JTV-59,2010,1512.9
Fender Stratocaster,2014,765.4
Taylor PS10ce ,2014,9318.0
Martin Grand J12-16GTE,2015,2199.0
jjj,1997,977.0
Loading