mirror of
https://github.com/kristoferssolo/School.git
synced 2026-03-22 00:26:35 +00:00
Final changes and commit
This commit is contained in:
@@ -4,76 +4,85 @@
|
||||
|
||||
# task 1 - train
|
||||
def trains():
|
||||
# blue train from Jelgava to Riga
|
||||
blue_train_speed = 57
|
||||
blue_train_time_driven = 10 / 60
|
||||
# blue train from Jelgava to Riga
|
||||
blue_train_speed = 57
|
||||
blue_train_time_driven = 10 / 60
|
||||
|
||||
# green train form Riga to Jelgava
|
||||
green_train_speed = 60
|
||||
# green train form Riga to Jelgava
|
||||
green_train_speed = 60
|
||||
|
||||
# red train from Valka to Jelgava
|
||||
red_train_speed = 60
|
||||
red_train_time_driven = 4
|
||||
# red train from Valka to Jelgava
|
||||
red_train_speed = 60
|
||||
red_train_time_driven = 4
|
||||
|
||||
# distances
|
||||
distance_riga_jelgava = 42 # from Riga to Jelgava (blue and green train)
|
||||
distance_riga_valka = 164 # from Riga to Valka (red train)
|
||||
# distances
|
||||
distance_riga_jelgava = 42 # from Riga to Jelgava (blue and green train)
|
||||
distance_riga_valka = 164 # from Riga to Valka (red train)
|
||||
|
||||
def blue_green_train():
|
||||
blue_train_already_driven_distance = blue_train_speed * blue_train_time_driven # blue train driven distance in 10 min
|
||||
meeting_time = (distance_riga_jelgava - blue_train_already_driven_distance) / (blue_train_speed + green_train_speed) # time after which the two trains meet
|
||||
green_train_distance = meeting_time * green_train_speed # distance green train has driven
|
||||
meeting_distance = distance_riga_jelgava - green_train_distance # distance from meeting point to Riga
|
||||
def blue_green_train():
|
||||
blue_train_already_driven_distance = blue_train_speed * \
|
||||
blue_train_time_driven # blue train driven distance in 10 min
|
||||
meeting_time = (distance_riga_jelgava - blue_train_already_driven_distance) / (
|
||||
blue_train_speed + green_train_speed) # time after which the two trains meet
|
||||
green_train_distance = meeting_time * \
|
||||
green_train_speed # distance green train has driven
|
||||
meeting_distance = distance_riga_jelgava - \
|
||||
green_train_distance # distance from meeting point to Riga
|
||||
|
||||
return f"Zilais un zaļais vilciens tiksies {round(meeting_distance, 2)}km no Rīgas."
|
||||
return f"Zilais un zaļais vilciens tiksies {round(meeting_distance, 2)}km no Rīgas."
|
||||
|
||||
def red_train():
|
||||
red_train_distance_driven = red_train_time_driven * red_train_speed # red train driven distance in given time
|
||||
print(f"Sarkanais vilciens ir nobraucis {round(red_train_distance_driven, 2)}km.")
|
||||
if red_train_distance_driven > distance_riga_valka:
|
||||
return "Sarkanais vilciens ir pabraucis garām Rīgai."
|
||||
def red_train():
|
||||
red_train_distance_driven = red_train_time_driven * \
|
||||
red_train_speed # red train driven distance in given time
|
||||
print(
|
||||
f"Sarkanais vilciens ir nobraucis {round(red_train_distance_driven, 2)}km.")
|
||||
if red_train_distance_driven > distance_riga_valka:
|
||||
return "Sarkanais vilciens ir pabraucis garām Rīgai."
|
||||
|
||||
print(blue_green_train())
|
||||
print(red_train())
|
||||
print(blue_green_train())
|
||||
print(red_train())
|
||||
|
||||
|
||||
# task 2 - sheep
|
||||
def farm():
|
||||
sheep_amount = 255 # amount of sheep in farm
|
||||
sheep_price = 20.5 # price per sheep's wool
|
||||
wool_total_price = sheep_amount * sheep_price # price for all sheep wool
|
||||
sheep_amount = 255 # amount of sheep in farm
|
||||
sheep_price = 20.5 # price per sheep's wool
|
||||
wool_total_price = sheep_amount * sheep_price # price for all sheep wool
|
||||
|
||||
additional_sheep = 120 # additional sheep
|
||||
new_sheep_amount = sheep_amount + additional_sheep # sum of original and added sheep
|
||||
new_wool_total_price = new_sheep_amount * wool_total_price # price for original and added sheep wool
|
||||
additional_sheep = 120 # additional sheep
|
||||
# sum of original and added sheep
|
||||
new_sheep_amount = sheep_amount + additional_sheep
|
||||
# price for original and added sheep wool
|
||||
new_wool_total_price = new_sheep_amount * wool_total_price
|
||||
|
||||
ostrich_amount = 15 # amount of ostrich in farm
|
||||
ostrich_egg_price = 30 # price per egg
|
||||
ostrich_time = 2 # time required to get one ostrich egg
|
||||
days = 30 # the time when ostriches lay eggs
|
||||
ostrich_egg_total_price = ostrich_amount * ostrich_egg_price * days / ostrich_time # price for all ostrich eggs in 30 days
|
||||
ostrich_amount = 15 # amount of ostrich in farm
|
||||
ostrich_egg_price = 30 # price per egg
|
||||
ostrich_time = 2 # time required to get one ostrich egg
|
||||
days = 30 # the time when ostriches lay eggs
|
||||
ostrich_egg_total_price = ostrich_amount * ostrich_egg_price * \
|
||||
days / ostrich_time # price for all ostrich eggs in 30 days
|
||||
|
||||
if wool_total_price >= ostrich_egg_total_price:
|
||||
return "Iegūtās naudas pietiks, lai nopirktu visas mēneša olas."
|
||||
else:
|
||||
print("Iegūtās naudas nepietiks, lai nopirktu visas mēneša olas.")
|
||||
if new_wool_total_price >= ostrich_egg_total_price:
|
||||
return f"Ja aitu būtu par {additional_sheep}, tad iegūtās naudas pietiktu, lai nopirktu visas mēneša olas."
|
||||
else:
|
||||
return f"Ja aitu būtu par {additional_sheep}, tad iegūtās naudas nepietiktu, lai nopirktu visas mēneša olas."
|
||||
if wool_total_price >= ostrich_egg_total_price:
|
||||
return "Iegūtās naudas pietiks, lai nopirktu visas mēneša olas."
|
||||
else:
|
||||
print("Iegūtās naudas nepietiks, lai nopirktu visas mēneša olas.")
|
||||
if new_wool_total_price >= ostrich_egg_total_price:
|
||||
return f"Ja aitu būtu par {additional_sheep}, tad iegūtās naudas pietiktu, lai nopirktu visas mēneša olas."
|
||||
else:
|
||||
return f"Ja aitu būtu par {additional_sheep}, tad iegūtās naudas nepietiktu, lai nopirktu visas mēneša olas."
|
||||
|
||||
|
||||
def main():
|
||||
task = int(input("""Ivēlieties uzdevumu:
|
||||
task = int(input("""Ivēlieties uzdevumu:
|
||||
1 - pirmais uzdevums
|
||||
2 - otrais uzdevums
|
||||
"""))
|
||||
|
||||
if task == 1:
|
||||
trains()
|
||||
elif task == 2:
|
||||
print(farm())
|
||||
if task == 1:
|
||||
trains()
|
||||
elif task == 2:
|
||||
print(farm())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -3,22 +3,24 @@
|
||||
|
||||
|
||||
class SalaryBonus():
|
||||
def __init__(self):
|
||||
self.percent = .15 # percent
|
||||
self.salary = float(input("Mēneša algas apjoms: ")) # salary per month
|
||||
self.time_worked = int(input("Nostrādātais gadu skaits: ")) # number of years worked
|
||||
def __init__(self):
|
||||
self.percent = .15 # percent
|
||||
self.salary = float(input("Mēneša algas apjoms: ")) # salary per month
|
||||
# number of years worked
|
||||
self.time_worked = int(input("Nostrādātais gadu skaits: "))
|
||||
|
||||
def _calculate_bonus(self):
|
||||
if self.time_worked > 2: # if worked for more than 2 years
|
||||
salary_bonus = self.percent * self.salary * (self.time_worked - 2) # calculates salary bonus
|
||||
return f"Algas bonuss ir {round(salary_bonus, 2)}€"
|
||||
else:
|
||||
return "Algas bonuss nepienākas."
|
||||
def _calculate_bonus(self):
|
||||
if self.time_worked > 2: # if worked for more than 2 years
|
||||
salary_bonus = self.percent * self.salary * \
|
||||
(self.time_worked - 2) # calculates salary bonus
|
||||
return f"Algas bonuss ir {round(salary_bonus, 2)}€"
|
||||
else:
|
||||
return "Algas bonuss nepienākas."
|
||||
|
||||
|
||||
def main():
|
||||
print(SalaryBonus()._calculate_bonus())
|
||||
print(SalaryBonus()._calculate_bonus())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -4,36 +4,38 @@
|
||||
|
||||
# task 1 - prime numbers
|
||||
def is_prime(number):
|
||||
import math
|
||||
if number > 0:
|
||||
if number == 1:
|
||||
return "1 nav pirmsskaitlis"
|
||||
else:
|
||||
for i in range(2, int(math.sqrt(number)) + 1): # range from 2 to sqrt(n)
|
||||
if number % i == 0:
|
||||
return f"{number} nav pirmsskaitlis"
|
||||
return f"{number} ir pirmsskaitlis"
|
||||
else:
|
||||
return "Skaitlim jābūt lielākam par 0"
|
||||
import math
|
||||
if number > 0:
|
||||
if number == 1:
|
||||
return "1 nav pirmsskaitlis"
|
||||
else:
|
||||
for i in range(2, int(math.sqrt(number)) + 1): # range from 2 to sqrt(n)
|
||||
if number % i == 0:
|
||||
return f"{number} nav pirmsskaitlis"
|
||||
return f"{number} ir pirmsskaitlis"
|
||||
else:
|
||||
return "Skaitlim jābūt lielākam par 0"
|
||||
|
||||
|
||||
# task 2 - cities
|
||||
class Cities:
|
||||
def __init__(self, p0, perc, delta, p):
|
||||
self.p0 = p0 # initial population
|
||||
self.perc = float(perc[:-1]) / 100 # annual percentage population growth
|
||||
self.delta = delta # number of arrivals (departures) per year
|
||||
self.p = p # population size required
|
||||
def __init__(self, p0, perc, delta, p):
|
||||
self.p0 = p0 # initial population
|
||||
# annual percentage population growth
|
||||
self.perc = float(perc[:-1]) / 100
|
||||
self.delta = delta # number of arrivals (departures) per year
|
||||
self.p = p # population size required
|
||||
|
||||
def _calculate(self):
|
||||
years = 0
|
||||
while (True):
|
||||
self.p0 = self.p0 + self.p0 * self.perc + self.delta # calculate the new population in each year via the formula
|
||||
years += 1
|
||||
if self.p0 >= self.p: # if the initial population reaches the required
|
||||
return f"{self.p} iedzīvotāju skaits tiks sasniegts pēc {years} gadiem"
|
||||
if self.p0 < 0: # if the required population is unreachable
|
||||
return -1
|
||||
def _calculate(self):
|
||||
years = 0
|
||||
while (True):
|
||||
# calculate the new population in each year via the formula
|
||||
self.p0 = self.p0 + self.p0 * self.perc + self.delta
|
||||
years += 1
|
||||
if self.p0 >= self.p: # if the initial population reaches the required
|
||||
return f"{self.p} iedzīvotāju skaits tiks sasniegts pēc {years} gadiem"
|
||||
if self.p0 < 0: # if the required population is unreachable
|
||||
return -1
|
||||
|
||||
|
||||
city_0 = Cities(1000, "2%", 50, 1200)
|
||||
@@ -43,33 +45,33 @@ city_3 = Cities(1_500_000, "2.5%", 10_000, 2_000_000)
|
||||
|
||||
|
||||
def main():
|
||||
task = int(input("""Ivēlieties uzdevumu:
|
||||
task = int(input("""Ivēlieties uzdevumu:
|
||||
1 - pirmais uzdevums
|
||||
2 - otrais uzdevums
|
||||
"""))
|
||||
|
||||
if task == 1:
|
||||
print(is_prime(int(input("Ievadiet skaitli: "))))
|
||||
elif task == 2:
|
||||
city = int(input("""Izvēlieties pilsētu:
|
||||
if task == 1:
|
||||
print(is_prime(int(input("Ievadiet skaitli: "))))
|
||||
elif task == 2:
|
||||
city = int(input("""Izvēlieties pilsētu:
|
||||
0 - piemēra pilsēta
|
||||
1 - pirmā pilsēta
|
||||
2 - otrā pilsēta
|
||||
3 - trešā pilsēta
|
||||
"""))
|
||||
if city == 0:
|
||||
print(city_0._calculate())
|
||||
elif city == 1:
|
||||
print(city_1._calculate())
|
||||
elif city == 2:
|
||||
print(city_2._calculate())
|
||||
elif city == 3:
|
||||
print(city_3._calculate())
|
||||
else:
|
||||
print("Ievadīts nepareizs skaitlis.")
|
||||
else:
|
||||
print("Ievadīts nepareizs skaitlis.")
|
||||
if city == 0:
|
||||
print(city_0._calculate())
|
||||
elif city == 1:
|
||||
print(city_1._calculate())
|
||||
elif city == 2:
|
||||
print(city_2._calculate())
|
||||
elif city == 3:
|
||||
print(city_3._calculate())
|
||||
else:
|
||||
print("Ievadīts nepareizs skaitlis.")
|
||||
else:
|
||||
print("Ievadīts nepareizs skaitlis.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user