CheckIO-INCINERATOR
- 题目1:The Warriors
- 题目2:Army Battles
- 题目3:The Defenders
- 题目4:The Vampires
- 题目5:The Lancers
- 题目6:The Healers
- 题目7:Straight Fight
- 题目8:The Weapons
- 题目9:The Warlords
- 题目10:Army Units
- 题目11:3 Chefs
- 题目12:Building Base
- 题目13:Capital City
- 题目14:Dialogues
- 题目15:Every Person is Unique
- 题目16:Friends
- 题目17:Geometry Figures
- 题目18:Hacker Language
- 题目19:Microwave Ovens
- 题目20:Multicolored Lamp
- 题目21:Party Invitations
- 题目22:Text Editor
- 题目23:Voice TV Control
有些题目尚未解锁,解锁之后再更新
题目1:The Warriors
class Warrior:
def __init__(self):
self.health = 50
self.strenght = 5
self.is_alive = True
class Knight(Warrior):
def __init__(self):
self.health = 50
self.strenght = 7
self.is_alive = True
def fight(unit_1, unit_2):
while(unit_1.is_alive and unit_2.is_alive):
unit_2.health -= unit_1.strenght
if(unit_2.health<=0):
unit_2.is_alive = False
break
unit_1.health -= unit_2.strenght
if(unit_1.health<=0):
unit_1.is_alive = False
break
return unit_1.is_alive
题目2:Army Battles
class Warrior:
health = 50
strenght = 5
is_alive = True
class Knight(Warrior):
health = 50
strenght = 7
is_alive = True
class Army():
def __init__(self):
self.arm = []
def add_units(self,unit,num):
for i in range(num):
new_unit = unit()
self.arm.append(new_unit)
return self.arm
class Battle():
def fight(self,armclass1,armclass2):
arm1 = armclass1.arm
arm2 = armclass2.arm
while(len(arm1)>0 and len(arm2)>0):
if fight(arm1[0],arm2[0]) == True:
arm2.remove(arm2[0])
else:
arm1.remove(arm1[0])
if len(arm1)>0:
return True
else:
return False
def fight(unit_1, unit_2):
while (unit_1.is_alive and unit_2.is_alive):
unit_2.health -= unit_1.strenght
if (unit_2.health <= 0):
unit_2.is_alive = False
break
unit_1.health -= unit_2.strenght
if (unit_1.health <= 0):
unit_1.is_alive = False
break
return unit_1.is_alive
题目3:The Defenders
class Warrior:
health = 50
attack = 5
defender = 0
is_alive = True
class Knight(Warrior):
health = 50
attack = 7
defender = 0
is_alive = True
class Defender(Warrior):
health = 60
attack = 3
defender = 2
is_alive = True
class Army():
def __init__(self):
self.arm = []
def add_units(self, unit, num):
for i in range(num):
new_unit = unit()
self.arm.append(new_unit)
return self.arm
class Battle():
def fight(self, armclass1, armclass2):
arm1 = armclass1.arm
arm2 = armclass2.arm
while (len(arm1) > 0 and len(arm2) > 0):
if fight(arm1[0], arm2[0]) == True:
arm2.remove(arm2[0])
else:
arm1.remove(arm1[0])
if len(arm1) > 0:
return True
else:
return False
def update_health(unit1,unit2):
if (unit2.attack - unit1.defender) > 0:
loss = (unit2.attack - unit1.defender)
else:
loss = 0
unit1.health = unit1.health - loss
return unit1.health
def fight(unit_1, unit_2):
while (unit_1.is_alive and unit_2.is_alive):
unit_2.health = update_health(unit_2,unit_1)
if (unit_2.health <= 0):
unit_2.is_alive = False
break
unit_1.health = update_health(unit_1,unit_2)
if (unit_1.health <= 0):
unit_1.is_alive = False
break
return unit_1.is_alive
题目4:The Vampires
class Warrior:
health = 50
attack = 5
defender = 0
vampirism = 0
is_alive = True
class Knight(Warrior):
health = 50
attack = 7
defender = 0
vampirism = 0
is_alive = True
class Defender(Warrior):
health = 60
attack = 3
defender = 2
vampirism = 0
is_alive = True
class Vampire(Warrior):
health = 40
attack = 4
defender = 0
vampirism = 50
is_alive = True
class Army():
def __init__(self):
self.arm = []
def add_units(self, unit, num):
for i in range(num):
new_unit = unit()
self.arm.append(new_unit)
return self.arm
class Battle():
def fight(self, armclass1, armclass2):
arm1 = armclass1.arm
arm2 = armclass2.arm
while (len(arm1) > 0 and len(arm2) > 0):
if fight(arm1[0], arm2[0]) == True:
arm2.remove(arm2[0])
else:
arm1.remove(arm1[0])
if len(arm1) > 0:
return True
else:
return False
def update_health(unit1,unit2): #unit1攻击unit2
if unit1.attack - unit2.defender > 0:
fluctuate = unit1.attack - unit2.defender
else:
fluctuate = 0
unit2.health = unit2.health - fluctuate
unit1.health = unit1.health + fluctuate * unit1.vampirism/100
return unit1.health,unit2.health
def fight(unit_1, unit_2):
while (unit_1.is_alive and unit_2.is_alive):
unit_1.health,unit_2.health = update_health(unit_1,unit_2)
if (unit_2.health <= 0):
unit_2.is_alive = False
break
unit_2.health,unit_1.health = update_health(unit_2,unit_1)
if (unit_1.health <= 0):
unit_1.is_alive = False
break
return unit_1.is_alive
题目5:The Lancers
class Warrior:
def __init__(self,health=50,attack=5):
self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60,attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40,attack=4)
self.vampirism = 50
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Army(list):
def __init__(self):
self.units = []
def add_units(self, unit, n):
self.extend(unit() for _ in range(n))
class Battle:
def fight(self,army_1,army_2):
while army_1:
if fight(army_1[0],army_2[0],army_1,army_2):
del army_2[0]
else:
del army_1[0]
if not army_2:
return True
return False
def fight(unit_1,unit_2,army_1=None,army_2=None):
def hit(unit_1,unit_2,army_2):
##unit2掉血
if isinstance(unit_2,Defender):
damage = max(0,unit_1.attack - unit_2.defense)
else:
damage = unit_1.attack
unit_2.health -= damage
##unit1技能
if isinstance(unit_1,Vampire):
unit_1.health +=damage *unit_1.vampirism/100
if isinstance(unit_1,Lancer) and army_2 is not None and len(army_2)>1:
army_2[1].health -= 0.5 * damage
while unit_1.is_alive:
hit(unit_1,unit_2,army_2)
if not unit_2.is_alive:
return True
hit(unit_2,unit_1,army_1)
return False
题目6:The Healers
class Warrior:
def __init__(self,health=50,attack=5):
self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60,attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40,attack=4)
self.vampirism = 50
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60,attack=0)
self.healing = 2
def heal(self,other):
other.health = min(other.health +self.healing,type(other)().health)
class Army(list):
def __init__(self):
self.units = []
def add_units(self, unit, n):
self.extend(unit() for _ in range(n))
class Battle:
def fight(self,army_1,army_2):
while army_1:
if fight(army_1[0],army_2[0],army_1,army_2):
del army_2[0]
else:
del army_1[0]
if not army_2:
return True
return False
def fight(unit_1,unit_2,army_1=None,army_2=None):
def hit(unit_1,unit_2,army_1,army_2):
##unit2掉血
if isinstance(unit_2,Defender):
damage = max(0,unit_1.attack - unit_2.defense)
else:
damage = unit_1.attack
unit_2.health -= damage
##技能
if isinstance(unit_1,Vampire):
unit_1.health +=damage *unit_1.vampirism/100
if isinstance(unit_1,Lancer) and army_2 is not None and len(army_2)>1:
army_2[1].health -= 0.5 * damage
if army_1 is not None and len(army_1)>1 and isinstance(army_1[1],Healer):
army_1[1].heal(unit_1)
while unit_1.is_alive:
hit(unit_1,unit_2,army_1,army_2)
if not unit_2.is_alive:
return True
hit(unit_2,unit_1,army_2,army_1)
return False
题目7:Straight Fight
class Warrior:
def __init__(self, health=50, attack=5):
self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.healing = 2
def heal(self, other):
other.health = min(other.health + self.healing, type(other)().health)
class Death(Warrior):
def __init__(self):
super().__init__(health=0,attack=0)
class Army(list):
def __init__(self):
self.units = []
def add_units(self, unit, n):
self.extend(unit() for _ in range(n))
class Battle:
def fight(self, army_1, army_2):
while army_1:
if fight(army_1[0], army_2[0], army_1, army_2):
del army_2[0]
else:
del army_1[0]
if not army_2:
return True
return False
def straight_fight(self,army_1,army_2):
while army_1 and army_2:
for x in zip(army_1,army_2):
fight(x[0],x[1])
army_1 = [x for x in army_1 if x.is_alive]
army_2 = [x for x in army_2 if x.is_alive]
return bool(army_1)
def fight(unit_1, unit_2, army_1=None, army_2=None):
def hit(unit_1, unit_2, army_1, army_2):
##unit2掉血
if isinstance(unit_2, Defender):
damage = max(0, unit_1.attack - unit_2.defense)
else:
damage = unit_1.attack
unit_2.health -= damage
##技能
if isinstance(unit_1, Vampire):
unit_1.health += damage * unit_1.vampirism / 100
if isinstance(unit_1, Lancer) and army_2 is not None and len(army_2) > 1:
army_2[1].health -= 0.5 * damage
if army_1 is not None and len(army_1) > 1 and isinstance(army_1[1], Healer):
army_1[1].heal(unit_1)
while unit_1.is_alive:
hit(unit_1, unit_2, army_1, army_2)
if not unit_2.is_alive:
return True
hit(unit_2, unit_1, army_2, army_1)
return False
别人的方法
class Warrior:
def __init__(self, health=50, attack=5):
self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
def hit(self, other):
other.loss(self.attack)
def damage(self, attack):
return attack
def loss(self, attack):
self.health -= self.damage(attack)
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
def damage(self, attack):
return max(0, attack - self.defense)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
def hit(self, other):
super().hit(other)
self.health += other.damage(self.attack) * self.vampirism // 100
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.heal_rate = 2
def heal(self, other):
other.health += self.heal_rate
def fight(unit_1, unit_2):
while 1:
unit_1.hit(unit_2)
if unit_2.health <= 0:
return True
unit_2.hit(unit_1)
if unit_1.health <= 0:
return False
class Army:
def __init__(self):
self.units = []
def add_units(self, unit_class, count):
for _ in range(count):
self.units.append(unit_class())
@property
def first_alive_unit(self):
for unit in self.units:
if unit.is_alive:
return unit
def next_unit(self, unit):
i = self.units.index(unit)
if i + 1 < len(self.units):
return self.units[i + 1]
@property
def is_alive(self):
return self.first_alive_unit is not None
@property
def alive_units(self):
return [u for u in self.units if u.is_alive]
class Battle:
@staticmethod
def hit(army_1, army_2):
unit_1 = army_1.first_alive_unit
unit_2 = army_2.first_alive_unit
unit_22 = army_2.next_unit(unit_2)
unit_1.hit(unit_2)
if unit_22 and isinstance(unit_1, Lancer):
unit_22.loss(unit_1.attack // 2)
unit_12 = army_1.next_unit(unit_1)
if unit_12 and isinstance(unit_12, Healer):
unit_12.heal(unit_1)
@classmethod
def fight(cls, army_1, army_2):
while army_1.is_alive and army_2.is_alive:
unit_1 = army_1.first_alive_unit
unit_2 = army_2.first_alive_unit
while 1:
cls.hit(army_1, army_2)
if unit_2.health <= 0:
break
cls.hit(army_2, army_1)
if unit_1.health <= 0:
break
return army_1.is_alive
@classmethod
def straight_fight(cls, army_1, army_2):
while army_1.is_alive and army_2.is_alive:
for unit_1, unit_2 in zip(army_1.alive_units, army_2.alive_units):
fight(unit_1, unit_2)
return army_1.is_alive
题目8:The Weapons
class Warrior:
def __init__(self, health=50, attack=5, defense=0, vampirism=0, heal_power=0):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
self.max_health = health
@property
def is_alive(self):
return self.health > 0
def heal(self, other):
other.health = min(other.health + self.heal_power, other.max_health)
def equip_weapon(self, weapon):
self.max_health += weapon.health
self.health += weapon.health
self.attack += weapon.attack
if self.defense>0:
self.defense += weapon.defense
if self.vampirism>0:
self.vampirism += weapon.vampirism
if self.heal_power>0:
self.heal_power += weapon.heal_power
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.heal_power = 2
class Weapon():
def __init__(self, health=0, attack=0, defense=0, vampirism=0, heal_power=0):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
class Sword(Weapon):
def __init__(self):
super().__init__(health=5,attack=2)
class Shield(Weapon):
def __init__(self):
super().__init__(health=20,attack=-1,defense=2)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(health=-15,attack=5,defense=-2,vampirism=10)
class Katana(Weapon):
def __init__(self):
super().__init__(health=-20,attack=6,defense=-5,vampirism=50)
class MagicWand(Weapon):
def __init__(self):
super().__init__(health=30,attack=3,heal_power=3)
class Army(list):
def __init__(self):
self.units = []
def add_units(self, unit, n):
self.extend(unit() for _ in range(n))
self.units = self
class Battle:
def fight(self, army_1, army_2):
while army_1:
if fight(army_1[0], army_2[0], army_1, army_2):
del army_2[0]
else:
del army_1[0]
if not army_2:
return True
return False
def straight_fight(self, army_1, army_2):
while army_1 and army_2:
for x in zip(army_1, army_2):
fight(x[0], x[1])
army_1 = [x for x in army_1 if x.is_alive]
army_2 = [x for x in army_2 if x.is_alive]
return bool(army_1)
def fight(unit_1, unit_2, army_1=None, army_2=None):
def hit(unit_1, unit_2, army_1, army_2):
##unit2掉血
damage = max(0, unit_1.attack - unit_2.defense)
unit_2.health -= damage
##技能
unit_1.health = min(unit_1.health + int(damage * unit_1.vampirism / 100),unit_1.max_health)
if isinstance(unit_1, Lancer) and army_2 is not None and len(army_2) > 1:
army_2[1].health -= 0.5 * damage
if army_1 is not None and len(army_1) > 1:
army_1[1].heal(unit_1)
while unit_1.is_alive:
hit(unit_1, unit_2, army_1, army_2)
if not unit_2.is_alive:
return True
hit(unit_2, unit_1, army_2, army_1)
return False
题目9:The Warlords
注意straight_fight时warlord不限制数量!!!!
class Warrior:
def __init__(self, health=50, attack=5, defense=0, vampirism=0, heal_power=0):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
self.max_health = health
@property
def is_alive(self):
return self.health > 0
def heal(self, other):
other.health = min(other.health + self.heal_power, other.max_health)
def equip_weapon(self, weapon):
self.max_health += weapon.health
self.health += weapon.health
self.attack += weapon.attack
if self.defense>0:
self.defense += weapon.defense
if self.vampirism>0:
self.vampirism += weapon.vampirism
if self.heal_power>0:
self.heal_power += weapon.heal_power
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.heal_power = 2
class Warlord(Warrior):
def __init__(self):
super().__init__(health=100,attack=4,defense=2)
class Weapon():
def __init__(self, health=0, attack=0, defense=0, vampirism=0, heal_power=0):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
class Sword(Weapon):
def __init__(self):
super().__init__(health=5,attack=2)
class Shield(Weapon):
def __init__(self):
super().__init__(health=20,attack=-1,defense=2)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(health=-15,attack=5,defense=-2,vampirism=10)
class Katana(Weapon):
def __init__(self):
super().__init__(health=-20,attack=6,defense=-5,vampirism=50)
class MagicWand(Weapon):
def __init__(self):
super().__init__(health=30,attack=3,heal_power=3)
class Army(list):
def __init__(self):
self.units = []
def add_units(self, unit, n):
# if isinstance(unit(),Warlord):
# if all(not isinstance(x,Warlord) for x in self):
# n = 1
# else:
# n = 0
self.extend(unit() for _ in range(n))
self.units = self
def clean_up(self):
for i in range(len(self)-1,-1,-1):
if not self[i].is_alive:
del self[i]
def clean_Warlord(self):
type_self = [type(x) for x in self]
while type_self.count(Warlord) > 1:
index = type_self.index(Warlord)
del self[index]
del type_self[index]
def move_units(self):
type_self = [type(x) for x in self]
if Warlord in type_self:
##lancer or other
try:
index = type_self.index(Lancer)
exchange(self,index,0)
type_self = [type(x) for x in self]
except:
index = [x for x, y in list(enumerate(type_self)) if (y != Healer and y != Warlord)]
if len(index):
index = index[0]
exchange(self,index,0)
type_self = [type(x) for x in self]
finally:
##healer
index = [x for x, y in list(enumerate(type_self)) if y == Healer]
for i in range(len(index)):
exchange(self,index[i],i+1)
type_self = [type(x) for x in self]
##Warlord
index = type_self.index(Warlord)
exchange(self,index,len(type_self)-1)
type_self = [type(x) for x in self]
class Battle:
def fight(self, army_1, army_2):
army_1.clean_Warlord()
army_2.clean_Warlord()
army_1.move_units()
army_2.move_units()
while army_1:
if fight(army_1[0], army_2[0], army_1, army_2):
army_2.clean_up()
army_2.move_units()
else:
army_1.clean_up()
army_1.move_units()
if not army_2:
return True
return False
def straight_fight(self, army_1, army_2):
while army_1 and army_2:
for x in zip(army_1, army_2):
fight(x[0], x[1])
army_1.clean_up()
army_2.clean_up()
return bool(army_1)
def fight(unit_1, unit_2, army_1=None, army_2=None):
def hit(unit_1, unit_2, army_1, army_2):
##unit2掉血
damage = max(0, unit_1.attack - unit_2.defense)
unit_2.health -= damage
##技能
unit_1.health = min(unit_1.health + int(damage * unit_1.vampirism / 100),unit_1.max_health)
if isinstance(unit_1, Lancer) and army_2 is not None and len(army_2) > 1:
army_2[1].health -= 0.5 * damage
if army_1 is not None and len(army_1) > 1:
army_1[1].heal(unit_1)
while unit_1.is_alive:
hit(unit_1, unit_2, army_1, army_2)
if not unit_2.is_alive:
return True
hit(unit_2, unit_1, army_2, army_1)
return False
def exchange(x,a1,a2):
##a1为原本位置
##a2为目标位置
if a1<a2:
for i in range(a1,a2):
x[i] , x[i+1] = x[i+1], x[i]
if a1>a2:
for i in range(a1,a2,-1):
x[i] , x[i-1] = x[i-1],x[i]
return x
题目10:Army Units
class Army:
pass
class Swordsman:
def __init__(self,name,area):
self.name = name
self.area = area
self.occupation = 'swordsman'
if self.area == 'Asian':
self.title = 'Samurai'
if self.area == 'European':
self.title = 'Knight'
def introduce(self):
return self.title + ' ' + self.name + ', ' +self.area + ' ' + self.occupation
class Lancer:
def __init__(self,name,area):
self.name = name
self.area = area
self.occupation = 'lancer'
if self.area == 'Asian':
self.title = 'Ronin'
if self.area == 'European':
self.title = 'Raubritter'
def introduce(self):
return self.title + ' ' + self.name + ', ' +self.area + ' ' + self.occupation
class Archer:
def __init__(self,name,area):
self.name = name
self.area = area
self.occupation = 'archer'
if self.area == 'Asian':
self.title = 'Shinobi'
if self.area == 'European':
self.title = 'Ranger'
def introduce(self):
return self.title + ' ' + self.name + ', ' +self.area + ' ' + self.occupation
class AsianArmy(Army):
def __init__(self, area='Asian'):
self.area = area
def train_swordsman(self,name):
return Swordsman(name,self.area)
def train_lancer(self,name):
return Lancer(name,self.area)
def train_archer(self,name):
return Archer(name,self.area)
class EuropeanArmy(Army):
def __init__(self,area = 'European'):
self.area = area
def train_swordsman(self,name):
return Swordsman(name,self.area)
def train_lancer(self,name):
return Lancer(name,self.area)
def train_archer(self,name):
return Archer(name,self.area)
其他人的答案
将类作为参数传递到下一级
class Army:
def train_swordsman(self, name): return Swordsman(self, name)
def train_lancer(self, name): return Lancer(self, name)
def train_archer(self, name): return Archer(self, name)
class Soldier:
def __init__(self, army, name):
self.army = army
self.name = name
def introduce(self):
return f'{self.army.warriors[self.category]} {self.name}, {self.army.region} {self.category}'
class Swordsman(Soldier):
category = 'swordsman'
class Lancer(Soldier):
category = 'lancer'
class Archer(Soldier):
category = 'archer'
class AsianArmy(Army):
region = "Asian"
warriors = {'swordsman': 'Samurai', 'lancer': 'Ronin', 'archer': 'Shinobi'}
class EuropeanArmy(Army):
region = "European"
warriors = {'swordsman': 'Knight', 'lancer': 'Raubritter', 'archer': 'Ranger'}
题目11:3 Chefs
class AbstractCook:
def __init__(self):
self.food = ''
self.drink = ''
self.food_sum_price = 0
self.food_num = 0
self.drink_sum_price = 0
self.drink_num = 0
def add_food(self,num,price):
self.food_num += num
self.food_sum_price += price * num
def add_drink(self,num,price):
self.drink_num += num
self.drink_sum_price += price * num
def total(self):
return f"{self.food}: {self.food_sum_price}, {self.drink}: {self.drink_sum_price}, Total: {self.food_sum_price + self.drink_sum_price}"
class JapaneseCook(AbstractCook):
def __init__(self):
super().__init__()
self.food = 'Sushi'
self.drink = 'Tea'
class RussianCook(AbstractCook):
def __init__(self):
super().__init__()
self.food = 'Dumplings'
self.drink = 'Compote'
class ItalianCook(AbstractCook):
def __init__(self):
super().__init__()
self.food = 'Pizza'
self.drink = 'Juice'
题目12:Building Base
class Building:
def __init__(self, south, west, width_WE, width_NS, height=10):
self.south = south
self.west = west
self.width_WE = width_WE
self.width_NS = width_NS
self.height = height
def corners(self):
return {"north-west":[self.width_NS+self.south,self.west],
"north-east":[self.width_NS+self.south,self.width_WE+self.west],
"south-west":[self.south,self.west],
"south-east":[self.south,self.width_WE+self.west]}
def area(self):
return self.width_WE*self.width_NS
def volume(self):
return self.width_NS*self.width_WE*self.height
def __repr__(self):
return f"Building({self.south}, {self.west}, {self.width_WE}, {self.width_NS}, {self.height})"
题目13:Capital City
class Capital:
__init_first = True
__special = None
def __new__(cls, *args, **kwargs):
if cls.__special == None:
cls.__special = object.__new__(cls)
return cls.__special
def __init__(self, city_name):
if self.__init_first:
self.city_name = city_name
self.__class__.__init_first = False
def name(self):
return self.city_name
题目14:Dialogues
class Human:
def __init__(self, name):
self.chat, self.name = None, name
def send(self, message):
self.chat.history += [(self.name, message)]
Robot = Human
class Chat:
def __init__(self):
self.history, self.human, self.robot = [], None, None
def connect_human(self, human):
human.chat, self.human = self, human
def connect_robot(self, robot):
robot.chat, self.robot = self, robot
def show_human_dialogue(self):
return '\n'.join('%s said: %s' % (name, msg) for name, msg in self.history)
def show_robot_dialogue(self):
translate = lambda msg: ''.join('10'[c in 'aeiouAEIOU'] for c in msg)
return '\n'.join('%s said: %s' % (name, translate(msg))
for name, msg in self.history)
题目15:Every Person is Unique
class Person:
def __init__(self, first_name, last_name, birth_date, job, working_years, salary, country, city, gender='unknown'):
self.first_name = first_name
self.last_name = last_name
self.birth_date = birth_date
self.job = job
self.working_years = working_years
self.salary = salary
self.country = country
self.city = city
self.gender = gender
def name(self):
return self.first_name + ' ' +self.last_name
def age(self):
return 2017 - int(self.birth_date.split(".")[-1])
def work(self):
if self.gender == "male":
return "He is a " + self.job
if self.gender == "female":
return "She is a " + self.job
if self.gender == "unknown":
return "Is a " + self.job
def money(self):
money = self.working_years * 12 * self.salary
return insert_str(str(money))
def home(self):
return "Lives in " + self.city + ", " + self.country
def insert_str(string):
count = -1
sumstr = ''
for x in string[::-1]:
count += 1
if count % 3 == 0 and count <len(string):
x = x + ' '
sumstr = x + sumstr
return sumstr.rstrip(' ')
题目16:Friends
未解锁
题目17:Geometry Figures
import cmath
class Parameters():
def __init__(self,side):
self.side = side
def choose_figure(self,shape):
self.shape = shape
def area(self):
return self.shape.area(self.side)
def perimeter(self):
return self.shape.perimeter(self.side)
def volume(self):
return self.shape.volume(self.side)
class Circle:
def area(self,r):
return round(r*r*cmath.pi,2)
def perimeter(self,r):
return round(2*cmath.pi*r,2)
def volume(self,r):
return 0
class Triangle:
def __init__(self):
self.side = 3
def perimeter(self,r):
return round(self.side * r,2)
def area(self,r):
return round(pow(3,0.5)/4 * r * r,2)
def volume(self,r):
return 0
class Square(Triangle):
def __init__(self):
self.side = 4
def area(self,r):
return r*r
class Pentagon(Triangle):
def __init__(self):
self.side = 5
def area(self,r):
return round(1.72048*r*r,2)
class Hexagon(Triangle):
def __init__(self):
self.side = 6
def area(self,r):
return round((3*pow(3,0.5)/2)*r*r,2)
class Cube:
def volume(self,r):
return r*r*r
def perimeter(self,r):
return r*12
def area(self,r):
return 6*r*r
题目18:Hacker Language
class HackerLanguage:
message = ""
def write(self,s):
self.message += s
def delete(self,n):
self.message = self.message[:-n]
def send(self):
seq = ''
for x in self.message:
if x.isalpha() or x == ' ':
if x == ' ':
seq += '1000000'
else:
seq += chr_to_bin(x)
else:
seq += x
return seq
def read(self,message):
i = 0
chra = ''
while 1:
try:
while 1:
if all(x in '01' for x in message[i:i+6]):
if message[i:i+7] == '1000000':
chra += ' '
else:
chra += chr(int(message[i:i+7],2))
i += 7
else:
chra += message[i]
i += 1
except:
break
return chra
def chr_to_bin(string):
return ''.join([str((ord(string)>>y)&1) for y in range(6,-1,-1)])
题目19:Microwave Ovens
class MicrowaveBase:
def __init__(self):
self.min = 0
self.sec = 0
def set_time(self,time):
self.min = min(int(time.split(':')[0]),90)
self.sec = int(time.split(':')[1])
if self.min == 90:
self.sec = 0
def add_time(self,time):
if time[-1]=='s':
self.sec += int(time[:-1])
self.min += self.sec // 60
self.sec %= 60
if time[-1]=='m':
self.min += int(time[:-1])
if self.min >= 90:
self.min = 90
self.sec = 0
def del_time(self,time):
if time[-1]=='s':
self.sec -= int(time[:-1])
self.min += self.sec // 60
self.sec = self.sec % 60
if time[-1]=='m':
self.min -= int(time[:-1])
if self.min < 0:
self.min = 0
self.sec = 0
class Microwave1(MicrowaveBase):
def __init__(self):
super().__init__()
def show_time(self):
time = (str(self.min).rjust(2,'0')+':'+str(self.sec).rjust(2,'0'))
return '_'+time[1:]
class Microwave2(MicrowaveBase):
def __init__(self):
super().__init__()
def show_time(self):
time = (str(self.min).rjust(2,'0')+':'+str(self.sec).rjust(2,'0'))
return time[:-1]+'_'
class Microwave3(MicrowaveBase):
def __init__(self):
super().__init__()
def show_time(self):
return str(self.min).rjust(2,'0')+':'+str(self.sec).rjust(2,'0')
class RemoteControl:
def __init__(self,micro):
self.set_time = micro.set_time
self.del_time = micro.del_time
self.add_time = micro.add_time
self.show_time = micro.show_time
题目20:Multicolored Lamp
class Friend:
def __init__(self,name):
self.name = name
self.party = "No party..."
self.time = ''
def show_invite(self):
if self.time != '':
return self.party + ': ' + self.time
else:
return self.party
class Party():
def __init__(self,party_name):
self.party_name = party_name
self.friend = []
def add_friend(self,friend):
self.friend.append(friend)
def del_friend(self,friend):
self.friend.remove(friend)
def send_invites(self,time):
for i in range(len(self.friend)):
(self.friend[i]).time = time
(self.friend[i]).party = self.party_name
题目21:Party Invitations
未解锁
题目22:Text Editor
class Text:
def __init__(self):
self.text = ''
self.font = False
def write(self,string):
if self.font == True:
self.text = self.text[:-len(self.ex_font)] + string + self.ex_font
else:
self.text += string
def show(self):
return self.text
def restore(self,text):
self.text = text
def set_font(self,string):
string = '['+string+']'
if self.font == False:
self.text = string + self.text + string
self.font = True
else:
self.text = string+self.text[len(self.ex_font):-len(self.ex_font)]+string
self.ex_font = string
class SavedText:
def __init__(self):
self.version = []
def save_text(self,text):
self.version.append(text.text)
def get_version(self,index):
return self.version[index]
题目23:Voice TV Control
class VoiceCommand:
def __init__(self,channels=[]):
self.channels = channels
self.current = 0
def first_channel(self):
self.current = 0
return self.channels[0]
def last_channel(self):
self.current = -1
return self.channels[-1]
def turn_channel(self,num):
self.current = num-1
return self.channels[num-1]
def next_channel(self):
if self.current == len(self.channels)-1:
self.current = -1
self.current += 1
return self.channels[self.current]
def previous_channel(self):
self.current -= 1
return self.channels[self.current]
def current_channel(self):
return self.channels[self.current]
def is_exist(self,num):
if type(num) == int:
if num >len(self.channels):
return "No"
else:
return "Yes"
if type(num) == str:
try:
self.channels.index(num)
return "Yes"
except:
return "No"