首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >高效农业

高效农业
EN

Code Golf用户
提问于 2017-06-19 02:47:01
回答 3查看 447关注 0票数 11

Introduction

一个农民需要帮助计算他每天采摘水果所需的最少时间。

挑战

  • 这个农民有X果园。
  • 每个果园都有Y水果。如果果园没有果实,那么它将包含字符串"none“。
  • 农夫有一份清单,这份清单上有他必须摘的水果。
  • 这位农民只会按顺序从名单上下来。
  • 你必须计算一下农夫每天采摘水果需要多长时间。

更多关于果园的

  • 所有的果园都排在一排。
  • 每个果园都是与下一个和前一个完全不同的1单位。
  • 农民可以上上下下,但不能从一个果园跳到另一个果园。

输入输出

您将收到以下格式的输入:

代码语言:javascript
运行
复制
X
*string*
*string*
*string* *string* *string* *string*
*string*
//ect.
Y
*string* *string*
*string* *string*
*string* *string*
*string* *string*
//ect.

X是果园的数量

  • X之后和Y之前的所有东西都是一个含有/某个字符串的果园(S),每个字符串都是该果园的一个不同的果实。

Y是农民必须采摘水果的天数。

  • 每一天都由两根不同的果实组成。
  • 你必须找出这些字符串在哪个果园中,并计算差额。

输入规则:

  1. 每个水果名称字符串将是一个没有空格的单词。

实例化

还很困惑吗?也许这能澄清问题:

输入

代码语言:javascript
运行
复制
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,因为他们是一个果园相隔。

带注释的输入/输出

代码语言:javascript
运行
复制
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 ]

如何赢得

以字节为单位的最短代码获胜。

规则

卸荷器

我向沙箱提交了这个问题,但是我没有收到任何反馈,所以可以随意编辑。

EN

回答 3

Code Golf用户

发布于 2017-06-19 14:01:38

176个字节,Python3

代码语言:javascript
运行
复制
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:]]

这可以通过将整个内容作为字符串传入来调用。

代码语言:javascript
运行
复制
p("""6
none
apple
orange pear pear
none
orange lemon pumpkin
pumpkin lettuce flowers peas
4
peas lettuce
apple orange
apple pumpkin
flowers orange""")

未镀金溶液

代码语言:javascript
运行
复制
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:]
    ]
票数 2
EN

Code Golf用户

发布于 2017-06-27 17:19:48

JavaScript (ES6),224个字节

代码语言:javascript
运行
复制
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"调用

票数 0
EN

Code Golf用户

发布于 2017-06-28 11:00:52

罗达,118个字节

代码语言:javascript
运行
复制
{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}

解释:

代码语言:javascript
运行
复制
{
    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已经过时。

票数 0
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/127185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档