python 战舰_代码战争战舰受损或沉没Python
我不確定是什么原因導致我對下面的kata的解決方案失敗。在
如果我能很容易地測試3個案例。但是,這些測試用例的輸入不可用。在
這是我的解決方案的代碼,它不會因為斜船而失敗,只是偶爾會漏掉一次命中。在def damaged_or_sunk(board, attacks):
# reverse board rows
board = board[::-1]
# function state variables
attack_count = 0
ship_lengths, hits = {}, []
# return values stored in dict
return_hash = {"sunk" : 0,
"damaged" : 0,
"not_touched" : 0,
"points" : 0}
# check board
for attack in attacks:
attack_count += 1
attack_index = (attack[0] - 1, attack[1] - 1)
for row in board:
for column in row:
if column != 0:
if attack_count == 1:
if column in ship_lengths:
ship_lengths[column] += 1
else:
ship_lengths[column] = 1
ship_index = (row.index(column), board.index(row))
if ship_index == attack_index:
hits.append(column)
if len(hits) >= len(attacks):
break
# catagorize hits
for ship in ship_lengths.keys():
if hits.count(ship) == ship_lengths[ship]:
return_hash["sunk"] += 1
elif ship not in hits:
return_hash["not_touched"] += 1
else:
return_hash["damaged"] += 1
# calculate points
for return_val in return_hash.keys():
if return_val == "sunk":
return_hash["points"] += return_hash["sunk"]
elif return_val == "damaged":
return_hash["points"] += (return_hash["damaged"] / 2)
elif return_val == "not_touched":
return_hash["points"] -= return_hash["not_touched"]
return return_hash
總結
以上是生活随笔為你收集整理的python 战舰_代码战争战舰受损或沉没Python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原生Winows7系统添加USB3.0的
- 下一篇: mysql 改列定义_如何更改MySQL