diff --git a/cs171/Lect03/LectureProblems.py b/cs171/Lect03/LectureProblems.py new file mode 100644 index 0000000000000000000000000000000000000000..7844f726dbf4744f216ce2e379b2ec6dee0bd0cc --- /dev/null +++ b/cs171/Lect03/LectureProblems.py @@ -0,0 +1,31 @@ +# Any Imports +import random + +# Slide 10 + +# Write a function generateUsername() which takes the +# user's first, middle, and last names. A username is +# in the format abc1234, where the three letters are +# the person's first, middle, and last initial followed +# by a four-digit random integer + +def generateUsername(first, middle, last): + return random.randint(1000, 9999) + +# Slide 37 + +# Write a function isValidTriangle() which takes a list +# of three positive float values representing the lengths +# of the sides. Using the info on Slide 37, decide whether +# these three sides can be arranged to form a triangle + +def isValidTriangle(sides): + return sides[0] > 0 + +if __name__ == "__main__": + username = generateUsername("Anthony", "Joseph", "Drexel") + print(username) + + lengths = [8.6, 7.5, 3.0] + isTriangle = isValidTriangle(lengths) + print(isTriangle) \ No newline at end of file diff --git a/cs171/Lect03/grades.py b/cs171/Lect03/grades.py new file mode 100644 index 0000000000000000000000000000000000000000..0bffddc6c1d0c91e07d29611026c74f60f41e772 --- /dev/null +++ b/cs171/Lect03/grades.py @@ -0,0 +1,5 @@ + + +grades = [ float(input(f"Enter grade {i+1}: ")) for i in range(0,4)] +average = sum(grades) / len(grades) +print(f"Average grade: {average:.1f}") \ No newline at end of file diff --git a/cs171/Lect03/hello.py b/cs171/Lect03/hello.py new file mode 100644 index 0000000000000000000000000000000000000000..380903bae249f3ee98a3a271713a82f09dd30752 --- /dev/null +++ b/cs171/Lect03/hello.py @@ -0,0 +1,62 @@ +print("Hello Lecture 3!") + +name = "Moix" +longer = "This is a longer string of text" + #0123456789012345678901234567890 + +print(name) + +print(len(name)) +print(len("A literal string")) + +print(name[1:3]) +print(longer[ 1 : 10 : 2 ]) +print(len(longer[ 1 : 10 : 2 ])) + +print(3 + 2) +print("three" + "two") +print(str(1) + "three") + +digits = 98.6 + +print(float(int(float("99789.87")))) + +print(int(digits)) +print(digits) + +print(float("3e8")) + +print(ord('🐉')) +print(chr(128009 + 1)) + +print("She called him \"interesting\" the other day") +print('She called him "interesting" the other day') +print("Isn't is a \"contraction\"") +print("A\nB") + +price = 5 +item = "coffee" + +print(item + ":\t" + str(price)) +print("coffee:\t%.2f" % price) +print(f"{item}:\t{price:.2f}") + +names = ["you", "Carol", "Bob"] +names[0] = "anthony" +print(names.index("Bob")) + + + +print([f"{i} squared is {i**2}" for i in range(1, 10)]) + + +set1 = {"A", "B", "C"} +set2 = {"C", "A", "B", 7} + +print(set1) +print(set2) + +print(set1 == set2) + +print(7 in set1) +print(7 in set2) \ No newline at end of file diff --git a/cs171/Lect03/states.py b/cs171/Lect03/states.py new file mode 100644 index 0000000000000000000000000000000000000000..8cdfe5c9e143f4bfebb5f27b33e3009ced355805 --- /dev/null +++ b/cs171/Lect03/states.py @@ -0,0 +1,14 @@ + + +states = { "AZ" : "Arizona", + "CA" : "California", + "PA" : "Pennsylvania", + "OTH" : ["Washington DC", "Puerto Rico"] + } + +print(states["OTH"]) + +# del states["AZ"] + +print(states) + diff --git a/cs172/Lect03/len_demo.py b/cs172/Lect03/len_demo.py new file mode 100644 index 0000000000000000000000000000000000000000..2e4db5d571563c27fe6e7cf988dc0f900d7c32ee --- /dev/null +++ b/cs172/Lect03/len_demo.py @@ -0,0 +1,80 @@ +class EL: + + def __init__(self, inches=0, feet=0, yards=0): + self.__inches = inches % 12 + self.__feet = feet + inches // 12 + self.__yards = yards + self.__feet // 3 + self.__feet = self.__feet % 3 + + def getInches(self): + return self.__inches + + def getFeet(self): + return self.__feet + + def getYards(self): + return self.__yards + + def totalInches(self): + return self.__inches + self.__feet * 12 + self.__yards * 36 + + def addInches(self, additionalInches): + newTotalInches = self.totalInches() + additionalInches + tempLength = EL(newTotalInches) + self.__yards = tempLength.getYards() + self.__feet = tempLength.getFeet() + self.__inches = tempLength.getInches() + + def __add__(self, otherEL): + newEL = EL(self.totalInches() + otherEL.totalInches()) + return newEL + + def __mul__(self, intValue): + return EL(intValue * self.totalInches()) + + def __eq__(self, otherEL): + return self.totalInches() == otherEL.totalInches() + + def __lt__(self, otherEL): + return self.totalInches() < other.totalInches() + + def __le__(self, otherEL): + return self.totalInches() <= other.totalInches() + + def __ge__(self, otherEL): + return self.totalInches() >= other.totalInches() + + def __gt__(self, otherEL): + return self.totalInches() > other.totalInches() + + def __ne__(self, otherEL): + return not self == otherEL + + def __getitem__(self, item): + if item == 0: + return self.__inches + elif item == 1: + return self.__feet + elif item == 2: + return self.__yards + else: + raise Exception("Bad Index") + + def __str__(self): + return f"An English Length object of {self.__inches} inches, {self.__feet} feet, and {self.__yards} yards." + + +if __name__ == "__main__": + + lenA = EL(11) + lenB = EL(9) + lenC = lenA + lenB + + print(lenC.getInches(), "inches") + print(lenC.getFeet(), "feet") + print(lenC.getYards(), "yards") + print(lenC.totalInches(), "inches total") + + print(lenA != lenB) + + print(lenC)