Files
30-days-of-py/day3/excersise.py
2026-01-22 16:18:49 -06:00

187 lines
4.5 KiB
Python

# 1
age:int = 20
# 2
height: float = 1.76
# 3
complexNum: complex = 1 + 3j
def areaOfTriangle(base:float, height: float) -> float:
return (0.5*base*height)
def sideOfTriangle(a: int, b: int, c: int) -> int:
return (a+b+c)
def dataOfRectangle(length: float, width: float) -> [float, float]:
return [length*width, (2*(length+width))]
def dataOfCircle(radius:float) -> [float, float]:
pi = 3.14
return [pi * (radius**2), pi*(radius*2)]
def slopeOne(a:float, b:float) -> (float,float,float):
slope = a
y_inter= b
x_inter= -b/a if a != 0 else None
return (slope, x_inter, y_inter)
def slopeBtwePnt(p1:float, p2:float) -> float:
x1, y1 = p1
x2, y2 = p2
return (y2-y1)/(x2-x1)
def eucDist(p1:float,p2:float) -> float:
x1,y1 = p1
x2,y2 = p2
return ((x2 - x1)**2 + (y2 - y1)**2)**(1/2)
def f(x:float):
return ((x*x) + (6*x) + 9)
def findLength(string1: str, string2:str) -> bool:
return (len(string1) == len(string2))
def onIsInSentence(string1: str, string2:str) -> bool:
return ("on" in string1 and "on" in string2)
def isInSentence(string1:str, string2:str) -> bool:
return (string1 in string2)
def convertLenght(string: str) -> str:
return (str)((float)(len(string)))
def divAndRem(number:int) -> bool:
return (number % 2 == 0)
def floorDiv(num1:int,num2:int) -> bool:
return (num1 // num2 == int(2.7))
def typeCheck(element1, element2) -> bool:
return (type(element1) == type(element2))
def checkIntnFloat(element1:str, element2:int) -> bool:
return (int(float(element1)) == element2)
def payment(hours: int, rate: float) -> float:
return hours*rate*7
def secondsLived(years: int):
return years*365.25*24*60*60
def table(num: int):
new_list = [num,1]
for i in range(3):
new_list.append(new_list[-1]*new_list[0])
return new_list
# 4
tri_base = int(input("Enter base: "))
tri_heig = int(input("enter height: "))
print("The area of triangle is:", areaOfTriangle(tri_base, tri_heig))
# 5
tri_a = int(input("Enter side a: "))
tri_b = int(input("Enter side b: "))
tri_c = int(input("Enter side c: "))
print("The perimeter of triangle is:", sideOfTriangle(tri_a,tri_b,tri_c))
# 6
rec_w = int(input("\nEnter width of rectangle: "))
rec_h = int(input("Enter height of rectangle: "))
recData = dataOfRectangle(rec_w,rec_h)
print(f"Area of rectangle is {recData[0]}\nPerimeter of rectangle is {recData[1]}.\n")
# 7
cir_radius = int(input("Enter radius of a circle: "))
dataCir = dataOfCircle(cir_radius)
print(f"Area of circle: {dataCir[0]}\nCircumference of circle is: {dataCir[1]}\n")
# 8, 9, 10 SLOPE
m1,x_int,y_int = slopeOne(2,-2)
m2= slopeBtwePnt((2,2),(6,10))
dist = eucDist((2,2),(6,10))
print(f"Slope1: {m1}\nx-intercept: {x_int}\ny-intercept: {y_int}")
print(f"Slope2: {m2}\nDistance: {dist}")
print(f"Are slops ecual? {m1==m2}\n")
# 11 VALUE OF Y
print(f"y = {f(2)}")
print(f"{f(-3)} = 0\n")
# 12 LENGTH AND COMPARISON
l_str1 = "python"
l_str2 = "dragon"
print(f"are {l_str1} and {l_str2} equal?", findLength(l_str1, l_str2))
# 13 AND
print(f"is 'on' in {l_str1} and in {l_str2}?", onIsInSentence(l_str1,l_str2))
print()
# 14 substrings
string = "I hope this course is not full of jargon"
substring = "jargon"
print(f"is {substring} in {string}", isInSentence(substring, string))
print()
# 15 NOT
print(f"is 'on' not in {l_str1} and in {l_str2}?", not onIsInSentence(l_str1,l_str2))
# 16 Convert length to float and then to string
convertedLength = convertLenght(l_str1)
print(f"Length in {l_str1} is\n{type(convertedLength)} -> {convertedLength}")
# 18 Floor Division
floor_num1 = 7
floor_num2 = 3
print(f"Floor division for {floor_num1} and {floor_num2} is ecual to integer for '2.7'", floorDiv(floor_num1,floor_num2))
print()
# 17 Evenness
test_even_number = 4
print(f"is {test_even_number} even?", divAndRem(4))
print()
# 19 Type equal
tyec_val1 = '10'
tyec_val2 = 10
print(f"Are {tyec_val1} and {tyec_val2} types equal? {typeCheck(tyec_val1,tyec_val2)}")
print()
# 20 integer equal
intec_val1 = '9.8'
intec_val2 = 10
print(f"is {intec_val1} equal to {intec_val2}?", checkIntnFloat(intec_val1,intec_val2))
print()
# 23 Lists
print(table(1))
print(table(2))
print(table(3))
print(table(4))
print(table(5))
print()
# 21 Weekly payments
hours = int(input("Enter hours: "))
rate = float(input("Enter rate per hour: "))
print(f"Your weekly earning is: {payment(hours, rate)}")
print()
# 22 Years lived
years_lived = int(input("Enter number of years you lived: "))
print(f"You have lived for {secondsLived(years_lived)} seconds aproximately")
print()