Alphabet wars - nuclear strike–5 kyu–Python解法
Codewars 是一个跟LeetCode类似的结题网站。
Codewars题解专栏:Codewars题解
题目地址:Kata Stats: Alphabet wars - nuclear strike | Codewars
Introduction
There is a war and nobody knows - the alphabet war!
The letters hide in their nuclear shelters. The nuclear strikes hit the battlefield and killed a lot of them.
Task
Write a function that accepts battlefield string and returns letters that survived the nuclear strike.
The battlefield string consists of only small letters, #,[ and ].
The nuclear shelter is represented by square brackets []. The letters inside the square brackets represent letters inside the shelter.
The # means a place where nuclear strike hit the battlefield. If there is at least one # on the battlefield, all letters outside of shelter die. When there is no any # on the battlefield, all letters survive (but do not expect such scenario too often ;-P ).
The shelters have some durability. When 2 or more # hit close to the shelter, the shelter is destroyed and all letters inside evaporate. The ‘close to the shelter’ means on the ground between the shelter and the next shelter (or beginning/end of battlefield). The below samples make it clear for you.
Example
abde[fgh]ijk => "abdefghijk" (all letters survive because there is no # )
ab#de[fgh]ijk => "fgh" (all letters outside die because there is a # )
ab#de[fgh]ij#k => "" (all letters dies, there are 2 # close to the shellter )
##abde[fgh]ijk => "" (all letters dies, there are 2 # close to the shellter )
##abde[fgh]ijk[mn]op => "mn" (letters from the second shelter survive, there is no # close)
#ab#de[fgh]ijk[mn]op => "mn" (letters from the second shelter survive, there is no # close)
#abde[fgh]i#jk[mn]op => "mn" (letters from the second shelter survive, there is only 1 # close)
[a]#[b]#[c] => "ac"
[a]#b#[c][d] => "d"
[a][b][c] => "abc"
##a[a]b[c]# => "c"
这道题目是5 kyu难度的,说难不难,说简单,要花费一定的时间。
Python解法如下:
def alphabet_war(battlefield):
if '#' not in battlefield:
return battlefield.replace('[', '').replace(']', '')
l = []
for i in range(0, len(battlefield)):
if battlefield[i] == '#': l.append('#')
elif battlefield[i] == '[': begin = i
elif battlefield[i] == ']': l.append(battlefield[begin:i+1])
for i in range(0, len(l)):
if l[i] == '#':
while i >= 0 and l[i] == '#': i -= 1
if i >= 0:
if l[i][-1] == '1': l[i] = l[i][:-1]+'2'
elif l[i][-1] == ']': l[i] = l[i][:-1]+'1'
i = i+1
while i < len(l) and l[i] == '#': i += 1
if i < len(l):
if l[i][0] == '1': l[i] = '2'+l[i][1:]
elif l[i][0] == '[': l[i] = '1'+l[i][1:]
res = ""
for i in range(0, len(l)):
if l[i] != '#'and l[i][0] != '2' and l[i][-1] != '2' \
and not (l[i][0] == '1'and l[i][-1] == '1'):
res = res+l[i][1:-1]
return res
从我的解法中可以看出有一定的冗余存在。
看了别人的解法,有很多人都用了正则表达式,但我感觉短时间内肯定想不出这种解法:
import re
def alphabet_war(b):
if '#' not in b:
return re.sub(r"[\[\]]","",b)
p = re.compile('([a-z#]*)\[([a-z]+)\](?=([a-z#]*))')
return ''.join( e[1] if (e[0]+e[2]).count('#') < 2 else'' for e in p.findall(b))

探讨了在Codewars上解决Alphabet Wars核打击问题的策略,此问题要求识别在战场字符串中核打击后的生存字母。解决方案涉及解析战场字符串,识别核避难所,并确定哪些字母在核打击后幸存。
347

被折叠的 条评论
为什么被折叠?



