initial commit
This commit is contained in:
24
Python/OOP2/task1.py
Normal file
24
Python/OOP2/task1.py
Normal file
@ -0,0 +1,24 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
@dataclass(order=True)
|
||||
class Train:
|
||||
id: int = field(compare=False)
|
||||
to: str
|
||||
leaves_at: datetime = field(default_factory=datetime.now)
|
||||
|
||||
|
||||
now = datetime.now()
|
||||
trains = [Train(i, f"somewhere{i}", now + timedelta(hours=i)) for i in range(1, 6)]
|
||||
print(*trains, sep="\n", end="\n\n")
|
||||
print(*sorted(trains), sep="\n")
|
||||
|
||||
|
||||
def sort_by_id(trains: list[Train]) -> list[Train]:
|
||||
return sorted(trains, key=lambda train: train.id)
|
||||
|
||||
|
||||
def get_from_user() -> None:
|
||||
id = int(input())
|
||||
print(trains[id])
|
10
Python/OOP2/task10.py
Normal file
10
Python/OOP2/task10.py
Normal file
@ -0,0 +1,10 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class Foo1:
|
||||
text: str
|
||||
|
||||
@dataclass
|
||||
class Foo2(Foo1):
|
||||
num:int
|
||||
|
14
Python/OOP2/task2.py
Normal file
14
Python/OOP2/task2.py
Normal file
@ -0,0 +1,14 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Self
|
||||
|
||||
|
||||
@dataclass
|
||||
class Foo:
|
||||
a: int
|
||||
b: int
|
||||
|
||||
def sum(self: Self) -> int:
|
||||
return self.a + self.b
|
||||
|
||||
def greater(self: Self) -> int:
|
||||
return max(self.a, self.b)
|
21
Python/OOP2/task3.py
Normal file
21
Python/OOP2/task3.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import Self
|
||||
|
||||
from attrs import define, field, validators
|
||||
|
||||
|
||||
@define
|
||||
class time:
|
||||
hours: int = field(validator=validators.in_(range(24)))
|
||||
minutes: int = field(validator=validators.in_(range(60)))
|
||||
seconds: int = field(validator=validators.in_(range(60)))
|
||||
|
||||
def change(
|
||||
self: Self, hours: int | None, minutes: int | None, seconds: int | None
|
||||
) -> Self:
|
||||
if hours is not None:
|
||||
self.hours += hours
|
||||
if minutes is not None:
|
||||
self.minutes += minutes
|
||||
if seconds is not None:
|
||||
self.seconds += seconds
|
||||
return self
|
45
Python/OOP2/task4.py
Normal file
45
Python/OOP2/task4.py
Normal file
@ -0,0 +1,45 @@
|
||||
import random
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Self
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Base:
|
||||
id: int
|
||||
level: int = 1
|
||||
|
||||
|
||||
@dataclass
|
||||
class Hero(_Base):
|
||||
soliders: list["Solider"] = field(default_factory=list)
|
||||
|
||||
def level_up(self: Self) -> Self:
|
||||
self.level += 1
|
||||
return self
|
||||
|
||||
|
||||
@dataclass
|
||||
class Solider(_Base):
|
||||
def follow(self: Self, hero: Hero) -> None:
|
||||
print(f"Иду за героем {hero.id}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
amount_of_players = 2
|
||||
heroes = [Hero(i) for i in range(2)]
|
||||
for i in range(amount_of_players, amount_of_players * 3 + 1):
|
||||
solider = Solider(i)
|
||||
random.choice(heroes).soliders.append(solider)
|
||||
|
||||
amount_of_soliders = [len(hero.soliders) for hero in heroes]
|
||||
winner_id = amount_of_soliders.index(max(amount_of_soliders))
|
||||
winner = heroes[winner_id]
|
||||
loser_id = amount_of_soliders.index(max(amount_of_soliders))
|
||||
loser = heroes[loser_id]
|
||||
winner.level_up()
|
||||
print(winner)
|
||||
random.choice(loser.soliders).follow(winner)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
14
Python/OOP2/task5.py
Normal file
14
Python/OOP2/task5.py
Normal file
@ -0,0 +1,14 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Self
|
||||
|
||||
|
||||
@dataclass
|
||||
class Person:
|
||||
name: str
|
||||
age: int
|
||||
|
||||
def move(self: Self) -> None:
|
||||
print(f"{self.name} говорит")
|
||||
|
||||
def talk(self: Self) -> None:
|
||||
print(f"{self.name} идёт")
|
12
Python/OOP2/task6.py
Normal file
12
Python/OOP2/task6.py
Normal file
@ -0,0 +1,12 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Self
|
||||
|
||||
|
||||
@dataclass
|
||||
class Game:
|
||||
name: str
|
||||
year: int
|
||||
|
||||
# НАХ?
|
||||
def get_name(self: Self):
|
||||
return self.name
|
0
Python/OOP2/task7.py
Normal file
0
Python/OOP2/task7.py
Normal file
26
Python/OOP2/task8.py
Normal file
26
Python/OOP2/task8.py
Normal file
@ -0,0 +1,26 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Game:
|
||||
year: int
|
||||
name: str
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class PCGame(Game):
|
||||
pass
|
||||
|
||||
|
||||
class PS4Game(Game):
|
||||
pass
|
||||
|
||||
|
||||
class XboxGame(Game):
|
||||
pass
|
||||
|
||||
|
||||
class Mobiles(Game):
|
||||
pass
|
19
Python/OOP2/task9.py
Normal file
19
Python/OOP2/task9.py
Normal file
@ -0,0 +1,19 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
class Figure:
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Triangle:
|
||||
sides: tuple[int, int, int]
|
||||
height: int
|
||||
|
||||
def area(self) -> int:
|
||||
return self.sides[0] * self.height
|
||||
|
||||
|
||||
@dataclass
|
||||
class Circle:
|
||||
pass
|
13
Python/OOP2/test.py
Normal file
13
Python/OOP2/test.py
Normal file
@ -0,0 +1,13 @@
|
||||
import asyncio
|
||||
|
||||
|
||||
async def sleep_print(i: str) -> None:
|
||||
print(i)
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
async def main() -> int:
|
||||
await asyncio.gather(*[sleep_print(i) for i in range(5)])
|
||||
|
||||
|
||||
asyncio.run(main())
|
Reference in New Issue
Block a user