岛上的灯塔有闪烁的代码,让船只知道他们看到的是哪座灯塔。输入应该是S和L的字符串。下面是4个灯塔的代码示例,作为字符串列表:
['SLS', 'SLL', 'SSS', 'LSL']其中:
S=短时间(1秒)
L=长跑(3秒)
“长”和“短”都后面跟着1秒,除了代码中的最后一个,后面是一个7秒的长暂停,以表示代码的结束。上面示例中的第一个灯塔的代码与这个眨眼的计划相对应,以秒为单位:
1点,1点,3点,1点,1点,7点的重复
N灯塔同时开放,每个灯塔都在其日程开始时。编写一个程序,它接受一系列闪烁代码(以上述格式的字符串)并输出完全为零、一、二、三、.的总秒数。在灯塔运行的第一个小时之后,灯塔在同一时间运行(因此输出的总数总是3600)。
Input -> Output
['SLS', 'SLL', 'SSS', 'LSL'] -> 1125, 890, 652, 590, 343
['S', 'L', 'SS'] -> 2250, 630, 540, 180
['SLSL', 'S'] -> 1850, 1450, 300
['SLS'] -> 2314, 1286
['SS', 'SS', 'L'] -> 2520, 360, 0, 720可以有一个或多个灯塔(代码字符串)。
所有灯塔同时运行,每个灯塔都在其代码计划的开始。
代码可以是任意长度的>= 1,并且不一定是唯一的(然而离岸混乱可能会发生)。
为了清晰起见,第一个例子中的652是两个灯塔(而不是2个或更多)在运行的时间,以秒为单位。
以字节表示的最短代码获胜(密码-高尔夫)。
发布于 2021-07-12 20:28:37
ε€C9%Å10.ý˜7Å0«60n∍}øOZÝ¢ε } # for each lighthouse in the implicit input:
€C # convert each character from binary
9% # modulo 9: S -> 28 %9 = 1, L -> 21%9 = 3
Å1 # for each number in the resulting list get a list of that many 1's
0.ý # intersperse with 0's
˜ # flatten into a list of 1's and 0's
7Å0« # append 7 0's
60n∍ # extend the list to length 60^2=3600 by cycling the values
ø # after the loop: transpose the list of lists
O # sum every one of the 3600 sublists
ZÝ # get the range [0 .. max(a)]
¢ # for each value in the range, count occurences in the long list尝试一步一步地输出S/L到0/1的转换:在网上试试!
发布于 2021-07-13 07:04:41
需要一个字符串数组。返回一个对象。
a=>(n=3600,g=L=>n--?g(o[a.map(s=>t+=~~(s=s.replace(/./g,c=>c>g?10:1110))[n%(s.length+6)],t=0),t]=-~o[t]):o)(o={})发布于 2021-07-14 13:15:50
非常简单的方法:将代码字符串转换为1和0的序列,表示每个灯塔打开或关闭的秒数;然后根据同时打开的灯塔数累计秒的总和。
def f(h):
t=[0]*(len(h)+1)
for i in range(3600):t[[c[i]for c in[(x.replace('S','10').replace('L','1110')+'0'*6)*450for x in h]].count('1')]+=1
print(t)带注释的非高尔夫函数
def f(lighthouses):
"""
function that takes as input the list of lighthouses' blink codes
"""
# replace in the codes 'S' and 'L' with '1's
# corresponding to the effective ON duration in seconds
# '0' is needed for the OFF separation period
lighthouses = [x.replace('S', '10') for x in lighthouses]
lighthouses = [x.replace('L', '1110') for x in lighthouses]
# LONG OFF period
# only 6s because one '0' is already added on the last character
lighthouses = [x + '0'*6 for x in lighthouses]
# repeat the sequence for at least 1h
# minimum code duration 'S' = 8s
# 3600/8=450 then repeat 450 times to be sure to have at least 1h
lighthouses = [x * 450 for x in lighthouses]
# initialize the list to keep the total number of seconds
tot = [0] * (len(lighthouses) + 1)
# loop over 1h of repeated sequences
for i in range(3600):
# increase the counter
# the index corresponds to how many lighthouses are ON at the same time
# counting how many '1's there are at a given position in all the sequences
tot[[c[i] for c in lighthouses].count('1')] += 1
# outputs the list with total seconds
print(tot)https://codegolf.stackexchange.com/questions/231261
复制相似问题