Skip to content
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
6 changes: 4 additions & 2 deletions .idea/Python3_labs.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added student_exercises/DG_11_Classes_and_OOP.pptx
Binary file not shown.
45 changes: 45 additions & 0 deletions student_exercises/Ex6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#! /usr/bin/python
# Exercise 6, string formatting and regular expressions.
import re

from labs.Ex6 import postcode

infile = open('postcodes.txt', 'r')

# Variables for counting valid and invalid codes (part b)
valid = 0
invalid = 0

for postcode in infile:
# The variable 'postcode' contain the line read from the file.

# Ignore empty lines.
if postcode.isspace(): continue

# TODO (a): Remove newlines, tabs and spaces.
postcode = re.sub(r"\s+", r"", postcode)

# TODO (a): Convert to uppercase.
postcode = postcode.upper()

# TODO (a): Insert a space before the final digit and 2 letters.
postcode = re.sub(r"(\d[A-Z]{2})$", r" \1", postcode)

# Print the reformatted postcode.
print(postcode)

# TODO (b) Validate the postcode, returning a match object 'm'.
m = re.search(r"[A-Z]{1,2}\d{2}\s+\d[A-Z]{2}$", postcode) # TODO (b) Replace this line with a call to re.match().

if m:
valid = valid + 1
else:
invalid = invalid + 1

print(f"Valid postcodes {valid}")
print(f"Invalid postcodes {invalid}")

infile.close()

# TODO (b) Print the valid and invalid totals.

6 changes: 6 additions & 0 deletions student_exercises/GOT/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script does
"""
Docstring:
"""
58 changes: 58 additions & 0 deletions student_exercises/GOT/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#! /usr/bin/env python
# Author: BersuitX
# Description: This about script copy and optionally filter
# source collection to a destination collection using different methods.
"""
Docstring:
"""
from random import random, randint

import tank
import sys

def main():
winner = None
erik_tank = tank.Tank("german", "tiger")
zane_tank = tank.Tank("american", "sherman")
aram_tank = tank.Tank("british", "churchill")

aram_tank.accel(randint(1, 100))
erik_tank.accel(randint(1,100))
zane_tank.accel(randint(1,100))

aram_tank.rotate_left(randint(0,360))
erik_tank.rotate_left(randint(0, 360))
zane_tank.rotate_left(randint(0, 360))

aram_tank.accel(randint(1,100))
erik_tank.accel(randint(1, 100))
zane_tank.accel(randint(1, 100))

aram_tank.shoot()
erik_tank.shoot()
zane_tank.shoot()

aram_tank.take_damage(randint(1, 99))
erik_tank.take_damage(randint(1,99))
zane_tank.take_damage(randint(1,99))

# print(f"Health of Erik-s tanks is {erik_tank._health}")
# print(f"Health of Aram's tanks is {aram_tank._health}")
# print(f"Health of Zane's tanks is {zane_tank._health}")
# print(f"Status of Erik's and Zane's is {erik_tank + zane_tank}")
# # erik_tank.set_health(100 - erik_tank._health)
# print(f"Health of Erik-s tanks is {erik_tank._health}")
print(aram_tank.__dict__)
print(erik_tank.__dict__)
print(zane_tank.__dict__)

# if erik_tank._health > zane_tank._health > aram_tank._health:
# winner = erik_tank.country

print(f"And the winner is: {winner}")

return None

if __name__ == "__main__":
main()
sys.exit(0)
57 changes: 57 additions & 0 deletions student_exercises/GOT/tank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script copy and optionally filter
# source collection to a destination collection using different methods.
"""
Docstring:
"""
import vehicule

class Tank(vehicule.Vehicle):
# Attributes/Data + Behaviour/Methods
def __init__(self, country, model):
super().__init__(country, model)
self._location = {'x':0, 'y':0, 'z':0}
self._direction = 0
self._shells = 20
self._health = 100
# No Explicit Return, as called implicitly.


def accel(self, increase):
self._speed += increase
return None

def decel(self, decrease):
self._speed -= decrease
return None

def rotate_left(self, degrees):
self._direction -= degrees % 360
return None

def rotate_right(self, degrees):
self._direction += degrees % 360
return None

def shoot(self):
self._shells -= 1
return None

def take_damage(self, damage):
self._health -= damage
return None

def __add__(self, other):
return self._health + other._health

def __del__(self):
print("Boom..!")
return None

def get_health(self):
return self._health

def set_health(self, newhealth):
self._health = newhealth
return None
21 changes: 21 additions & 0 deletions student_exercises/GOT/vehicule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /usr/bin/env python
# Author: BersuitX
# Description: This about script copy and optionally filter
# source collection to a destination collection using different methods.
"""
Vehicle Base Class
"""

class Vehicle:
def __init__(self, country, model):
self.country = country
self.model = model
self._speed = 0

def accel(self, increase):
self._speed += increase
return None

def decel(self, decrease):
self._speed -= decrease
return None
6 changes: 6 additions & 0 deletions student_exercises/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script does
"""
Docstring:
"""
6 changes: 6 additions & 0 deletions student_exercises/calc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script does
"""
Docstring:
"""
32 changes: 32 additions & 0 deletions student_exercises/calc/adv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script does advance calculator functions
"""
Docstring: Includes mod, power, and sqrt
"""

import sys

def mod(x, z):
""" return Remainder of x divided by z """
return x % z

def power(x, z):
""" Return power of x to z """
return x**z

def sqrt(x):
""" Return square root of x"""
return round(x**0.5, 2)

def main():
print("Advanced Calculator App")
print("-" * 30)
print(f"100 % 30 = {mod(100, 30)}")
print(f"100 ** 3 = {power(100, 3)}")
print(f"\N{square root}100 = {sqrt(100)}")
return None

if __name__ == "__main__":
main()
sys.exit(0)
35 changes: 35 additions & 0 deletions student_exercises/calc/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script does
"""
Docstring:
"""
import sys

def add(*args):
total = 0
for num in args:
total += num
return total

def mul(*args):
total = 1
for num in args:
total *= num
return total

def div(x, y):
return round(x/y, 3)

def main():
print("Basic Calculator App")
print(f"4 + 3 = {add(4,3)}")
print(f"4 + 3 + 2 = {add(4,3,2)}")
print(f"4 * 3 = {mul(4, 3)}")
print(f"4 * 3 * 2 = {mul(4,3,2)}")
print(f"4 / 3 = {div(4,3)}")
return None

if __name__ == "__main__":
main()
sys.exit(0)
34 changes: 34 additions & 0 deletions student_exercises/calc/calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script does
"""
Docstring:
"""

import sys
import basic
import adv

def main():
menu = """
Calculator App
--------------
1. test basic
2. test Adv
"""

print(menu)
option = input("Choose 1 or 2: ")
if option == "1":
print(f"100 + 50 + 25 + 12.5 = {basic.add(100, 50, 25, 12.5)}")
print(f"100 * 50 * 25 * 12.5 = {basic.mul(100, 50, 25, 12.5)}")
elif option == "2":
print(f"Modulus of 100 by 45 = {adv.mod(100, 45)}")
print(f"Square root of 25 = {adv.sqrt(25)}")
else:
print("Invalid options")
return None

if __name__ == "__main__":
main()
sys.exit(0)
54 changes: 54 additions & 0 deletions student_exercises/demo_advance_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#! /usr/bin/python
# Author: BersuitX
# Description: This about script copy and optionally filter
# source collection to a destination collection using different methods.
"""
Docstring:
"""

# Source collection - list of string
students = ['euler', 'jorge', 'aram', 'mark', 'elizabeth', 'jeremy', ' erik', 'javonn',
'kirill', 'euler', 'aram']

# Iterate through the collection and copy to a destination
# Using an ITERATOR loop plus source, optional condition and expression
wee_names = []
for name in students:
if len(name) <= 5:
wee_names.append(name.upper())

print(f"1.Short names = {wee_names}")

# Using an ITERATOR loop plus source, user function for filtering, an expression
def filter_names(name):
if len(name) <= 5:
return True
else:
return False

wee_names = []
for name in students:
if filter_names(name):
wee_names.append(name.upper())

print(f"2.Short names = {wee_names}")

# Using built-in functions

wee_names = list(filter(filter_names, students))
print(f"3.Short names = {wee_names}")

wee_names = list(filter(lambda name:len(name) <= 5, students))
print(f"4.Short names = {wee_names}")

wee_names = [ name.upper() for name in students if len(name) <=5 ]
print(f"5.Short names = {wee_names}")

wee_names = [ (name.upper(), len(name)) for name in students if len(name) <=5 ]
print(f"6.Short names = {wee_names}")

wee_names = { name.upper(): len(name) for name in students if len(name) <=5 }
print(f"6.Short names = {wee_names}")

wee_names = { name.upper() for name in students if len(name) <=5 }
print(f"8.Short names = {wee_names}")
Loading