一个农民需要帮助计算他每天采摘水果所需的最少时间。
X
果园。Y
水果。如果果园没有果实,那么它将包含字符串"none“。1
单位。您将收到以下格式的输入:
X
*string*
*string*
*string* *string* *string* *string*
*string*
//ect.
Y
*string* *string*
*string* *string*
*string* *string*
*string* *string*
//ect.
X
是果园的数量
X
之后和Y
之前的所有东西都是一个含有/某个字符串的果园(S),每个字符串都是该果园的一个不同的果实。Y
是农民必须采摘水果的天数。
还很困惑吗?也许这能澄清问题:
输入
6
none
apple
orange pear pear
none
orange lemon pumpkin
pumpkin lettuce flowers peas
4
peas lettuce
apple orange
apple pumpkin
flowers orange
输出:[ 0, 1, 3, 1 ]
输入:
6
果园的数量6
果园,每个果园在一个新的线上。4
农民名单上的天数。4
水果,每一对水果都是在一个新的线上。输出:
0
,因为它们在同一个果园里。1
,因为它们相距一个果园。3
,因为它们相隔三个果园。1
,因为他们是一个果园相隔。带注释的输入/输出
6 orchards
a none
b apple
c orange pear pear
d none
e orange lemon pumpkin
f pumpkin lettuce flowers peas
--
4 fruits
peas lettuce 0
apple orange 1
apple pumpkin 3
flower orange 1
--
output: [ 0, 1, 3, 1 ]
以字节为单位的最短代码获胜。
我向沙箱提交了这个问题,但是我没有收到任何反馈,所以可以随意编辑。
发布于 2017-06-19 14:01:38
def p(a):l=[b.split()for b in a.split('\n')];X=int(l[0][0]);return[min(y-x for y in range(X)for x in range(y+1)if z[0]in l[x+1]+l[y+1]and z[1]in l[x+1]+l[y+1])for z in l[X+2:]]
这可以通过将整个内容作为字符串传入来调用。
p("""6
none
apple
orange pear pear
none
orange lemon pumpkin
pumpkin lettuce flowers peas
4
peas lettuce
apple orange
apple pumpkin
flowers orange""")
未镀金溶液
def problem(a):
# this will split on new line, and split each individual line into it's own list
lines=[b.split()for b in a.split('\n')]
# get the number of orchards
X=int(lines[0][0])
return[
# get the minimum of all possible differences
min(y-x # subtract y and x to get the difference
for y in range(X) # for each orchard, y
for x in range(y+1) # and each orchard up to y inclusive, x
if z[0] in lines[x+1]+lines[y+1] and z[1] in lines[x+1]+lines[y+1]) # if both fruits exist the x and y orchards
# for every day (z is a list containing 2 fruits as string)
for z in lines[X+2:]
]
发布于 2017-06-27 17:19:48
a=>(d=a.split('\n')).slice(e=+d[0]+2).map(f=>{p=e;for(g=(q=(t,s=0)=>d.slice(1,e-1).map(b=c=>c.split(' ')).findIndex((x,y)=>y>=t&x.includes(b(f)[s])))(0);++g;g=q(g))for(l=q(0,1);++l;l=q(l,1))p=p<(u=g>l?g-l:l-g)?p:u;return p})
用"6\nnone\napple\norange pear pear\nnone\norange lemon pumpkin\npumpkin lettuce flowers peas\n4\npeas lettuce\napple orange\napple pumpkin\nflowers orange"
调用
发布于 2017-06-28 11:00:52
{A=[head(parseInteger(pull()))|splitMany|enum]pull n;split|{f={|x|A|[_2]if[x in _1]}f a|{|i|f b|abs i-_}_|min}for a,b}
解释:
{
A=[
/* Pull line, convert to integer, and read that many lines */
head(parseInteger(pull()))|
/* Split all lines to arrays */
splitMany|
/* Enumerate, ie. give each line a number */
enum
]
/* Discard one line */
pull n;
/* Split each following line and push words to the stream */
split|
/* For each word a, b in the stream: */
{
/* Helper function to find lines that contain x */
f={|x|A|[_2]if[x in _1]}
/* Push numbers of lines that contain the first word to the stream */
f a|
/* For each line number i */
{|i|
/* Push numbers of lines that contain the second word to the stream */
f b|
/* Push the difference of two line numbers to the stream */
abs i-_
}_|
/* Push the smallest difference to the stream */
min
}for a,b
}
目前还没有TIO链接,因为TIO版的R da已经过时。
https://codegolf.stackexchange.com/questions/127185
复制相似问题