CheckiOは、それによってあなたのコーディングスキルを向上させること、困難な課題と面白いタスクに対処するためのコードゲーム、PythonとJavaScriptに初心者から上級プログラマを対象として、このブログでは、チェックポイントと実装コードで問題を行うPythonで自分の考えを記録することが主です他の偉大な神はコードを書いたから、だけでなく、学びます。
CheckiO公式サイト: https://checkio.org/
マイCheckiOホーム: https://py.checkio.org/user/TRHX/
列のCheckiO問題の解決シリーズ: https://itrhx.blog.csdn.net/category_9536424.html
CheckiO問題のソースへのすべてのソリューション: https://github.com/TRHX/Python-CheckiO-Exercise
タイトル説明
[陸軍戦闘]:このタイトルは戦士が最後のドライバーとジャズの決闘、両側が今、援軍を呼び出して、このタスクでは、既存のクラスや関数に新しいタスクを追加し、次の対象を拡張しましたクラスや関数。新しいカテゴリがあるべきArmy()
、としているadd_units()
ユニットの数に追加するための方法が選択されるArmy()
最初の引数に2番目の引数は二戦部隊となり、戦争の部隊を追加する初めてとなります、加えて、あなたが作成する必要があるfight()
の機能Battle()
で最も強力な軍隊を決定するクラス、。以下の原理に従った戦い:
人生のうちの一つの値未満または0に等しい場合、兵士と最初の陸軍の兵士の間でまず第一に、そして戦争に参加する新しい兵士の必要性、および新しい、兵士が戦う第二決闘を、持っています兵士と生き残るために他の側の戦いで戦った兵士が、この場合には、最初の軍の勝利ならば、Battle()
この関数は返す必要がありますTrue
第二軍が勝利を返した場合、False
。
[リンク]:https://py.checkio.org/mission/army-battles/
[Enter]キー:戦士と軍を
[出力]:戦いの結果(TrueまたはFalse)
[例]:
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
fight(chuck, bruce) == True
fight(dave, carl) == False
chuck.is_alive == True
bruce.is_alive == False
carl.is_alive == True
dave.is_alive == False
fight(carl, mark) == False
carl.is_alive == False
my_army = Army()
my_army.add_units(Knight, 3)
enemy_army = Army()
enemy_army.add_units(Warrior, 3)
army_3 = Army()
army_3.add_units(Warrior, 20)
army_3.add_units(Knight, 5)
army_4 = Army()
army_4.add_units(Warrior, 30)
battle = Battle()
battle.fight(my_army, enemy_army) == True
battle.fight(army_3, army_4) == False
コードの実装
class Army():
def __init__(self):
self.health = 0
self.attack = 0
self.num = 0
def add_units(self, x, num):
self.health = x().health
self.attack = x().attack
self.num = num
class Warrior:
health = 50
attack = 5
is_alive = True
class Knight(Warrior):
attack = 7
def fight(army1, army2):
while army1.health > 0:
army2.health -= army1.attack
army1.health -= army2.attack
if army2.health > army1.health:
army1.is_alive = False
return False
else:
army2.is_alive = False
return True
class Battle:
def fight(self, army1, army2):
x2 = y2 = 0
while army1.num > 0 and army2.num > 0:
x1 = army1.health if x2 == 0 else x2
y1 = army2.health if y2 == 0 else y2
while True:
y1 -= army1.attack
if y1 <= 0:
x2 = x1
y2 = 0
army2.num -= 1
break
x1 -= army2.attack
if x1 <= 0:
y2 = y1
x2 = 0
army1.num -= 1
break
if army1.num:
return True
else:
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
print("Coding complete? Let's try tests!")
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
#fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
#battle tests
my_army = Army()
my_army.add_units(Knight, 3)
enemy_army = Army()
enemy_army.add_units(Warrior, 3)
army_3 = Army()
army_3.add_units(Warrior, 20)
army_3.add_units(Knight, 5)
army_4 = Army()
army_4.add_units(Warrior, 30)
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
assert battle.fight(army_3, army_4) == False
print("Coding complete? Let's try tests!")
大神解答
大神解答 NO.1
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
def fight(u1, u2):
while u1.is_alive and u2.is_alive:
u2.health -= u1.attack
if u2.is_alive:
u1.health -= u2.attack
return u1.is_alive
class Army:
def __init__(self):
self._units = []
def __getitem__(self, index):
return self._units[index]
def __len__(self):
return len(self._units)
def add_units(self, unit, count):
self._units.extend(unit() for _ in range(count))
class Battle:
def fight(self, a1, a2):
i1 = i2 = 0
try:
while True:
u1, u2 = a1[i1], a2[i2]
fight(u1, u2)
i1 += not u1.is_alive
i2 += not u2.is_alive
except IndexError:
return any(u.is_alive for u in reversed(a1))
大神解答 NO.2
class Battle:
def fight(self, unit_1, unit_2):
i = len(unit_1.a) - 1
j = len(unit_2.a) - 1
while i >= 0 and j >= 0:
while unit_1.a[i].hp > 0 and unit_2.a[j].hp > 0:
unit_2.a[j].hp -= unit_1.a[i].atak
if unit_2.a[j].hp > 0:
unit_1.a[i].hp -= unit_2.a[j].atak
if unit_1.a[i].hp > 0:
unit_2.a.pop()
j -= 1
# print(unit_1.a[i].hp)
else:
unit_1.a.pop()
i -=1
# print(unit_2.a[j].hp)
return len(unit_1.a) > 0
class Army:
def __init__(self):
self.a = []
def add_units(self, cl, n):
for i in range(n):
if cl == Warrior:
n = Warrior()
self.a.append(n)
else:
n = Knight()
self.a.append(n)
class Warrior:
def __init__(self):
self.hp = 50
self.atak = 5
if self.hp > 0:
self.is_alive = True
else:
self.is_alive = False
pass
class Knight(Warrior):
def __init__(self):
self.hp = 50
self.atak = 7
if self.hp >= 0:
self.is_alive = True
else:
self.is_alive = False
def fight(unit_1, unit_2):
n = 0
while unit_1.hp > 0 and unit_2.hp > 0:
unit_2.hp -= unit_1.atak
if unit_2.hp > 0:
unit_1.hp -= unit_2.atak
if unit_1.hp > 0:
unit_1.is_alive = True
unit_2.is_alive = False
else:
unit_2.is_alive = True
unit_1.is_alive = False
return unit_1.hp > 0