126 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import math
 | |
| import random
 | |
| 
 | |
| 
 | |
| def primfactors(n, divisor=2):
 | |
|     if n == 1:
 | |
|         return []
 | |
| 
 | |
|     if not n % divisor:
 | |
|         return [divisor] + primfactors(n // divisor, divisor)
 | |
| 
 | |
|     return primfactors(n, divisor + 1)
 | |
| 
 | |
| 
 | |
| def harmon(n, val=0):
 | |
|     if not n:
 | |
|         return 0
 | |
| 
 | |
|     val += harmon(n - 1, 1 / n)
 | |
|     return val
 | |
| 
 | |
| 
 | |
| def usum(n, res=1):
 | |
|     if not n:
 | |
|         return 0
 | |
| 
 | |
|     res += usum(n - 1, res + 2)
 | |
|     return res
 | |
| 
 | |
| 
 | |
| def a4(x):
 | |
|     return (lambda x: 7 * x)(x)
 | |
| 
 | |
| 
 | |
| def b4(x):
 | |
|     return (lambda x: math.pow(x, 3 / 5))(x)
 | |
| 
 | |
| 
 | |
| def reverse(a: list):
 | |
|     if len(a) == 1:
 | |
|         return a
 | |
| 
 | |
|     # Reverse first and last
 | |
|     a[0], a[-1] = a[-1], a[0]
 | |
| 
 | |
|     # return [a[0], reverse(a[1:-2]), a[-1]]
 | |
|     return [a[0]] + reverse(a[1:-1]) + [a[-1]]
 | |
| 
 | |
| 
 | |
| class Ticket:
 | |
|     event = ""
 | |
|     price_class = 0
 | |
| 
 | |
|     def __init__(self, price, num, is_valid) -> None:
 | |
|         self.price = price
 | |
|         self.num = num
 | |
|         self.is_valid = is_valid
 | |
| 
 | |
|     def __str__(self):
 | |
|         return f"Price: {self.price}; \n Number: {self.num} \nValid: {self.is_valid}"
 | |
| 
 | |
|     def __repr__(self) -> str:
 | |
|         return f"Price: {self.price}; \n Number: {self.num} \nValid: {self.is_valid}"
 | |
| 
 | |
|     def convert(self, course):
 | |
|         new_price = self.price * course
 | |
|         print(f"Price with course: {course} is {new_price}")
 | |
|         return new_price
 | |
| 
 | |
|     def get_is_valid(self):
 | |
|         return self.is_valid
 | |
| 
 | |
| 
 | |
| def fill_tickets():
 | |
|     return [
 | |
|         Ticket(i * 10, num=random.random() * i, is_valid=True if i % 3 else False)
 | |
|         for i in range(12)
 | |
|     ]
 | |
| 
 | |
| 
 | |
| def writeTicketFile(ticket_list: list):
 | |
|     with open("ticket_list.txt", "+w") as f:
 | |
|         f.writelines(
 | |
|             [f"Ticket {i}: {t.__str__()} \n\n" for t, i in enumerate(ticket_list)]
 | |
|         )
 | |
| 
 | |
| 
 | |
| class TicketA(Ticket):
 | |
|     price_class = "A"
 | |
| 
 | |
|     def write_to_file(self):
 | |
|         with open(f"{self.price_class}.txt", "a") as file:
 | |
|             file.write(self.__str__() + "\n")
 | |
| 
 | |
| 
 | |
| class TicketB(Ticket):
 | |
|     price_class = "B"
 | |
| 
 | |
|     def write_to_file(self):
 | |
|         with open(f"{self.price_class}.txt", "a") as file:
 | |
|             file.write(self.__str__() + "\n")
 | |
| 
 | |
| 
 | |
| class TicketC(Ticket):
 | |
|     price_class = "C"
 | |
| 
 | |
|     def write_to_file(self):
 | |
|         with open(f"{self.price_class}.txt", "a") as file:
 | |
|             file.write(self.__str__() + "\n")
 | |
| 
 | |
| 
 | |
| class TicketD(Ticket):
 | |
|     price_class = "D"
 | |
| 
 | |
|     def write_to_file(self):
 | |
|         with open(f"{self.price_class}.txt", "a") as file:
 | |
|             file.write(self.__str__() + "\n")
 | |
| 
 | |
| 
 | |
| def main():
 | |
|     writeTicketFile(fill_tickets())
 | |
| 
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     main()
 | 
