编程难题和塔可卡车


23

Programming Puzzles&Code Golf编程室已开通了炸玉米饼卡车!这个词已经不复存在了,现在来自Stack Exchange网络上的所有用户都在尝试我们美味可口的ASCII 玉米饼。我们需要您的帮助,以确保每个人都及时得到他们的订单。食品卡车内没有太多空间,因此您需要代码尽可能短。

现在进行一些在职培训。

你的工作

编写一个完整的程序,从STDIN读取一个命令,或者编写一个将单个字符串作为输入的函数。玉米饼被打印到STDOUT,按订单生产。

接受订单

客户将通过STDIN或函数参数给您他们的订单。订单将以逗号分隔的所需浇头列表形式出现。浇头的分配顺序决定了它们在炸玉米饼中的出现顺序,首先列出的浇头在底部,最后一个在顶部。这是我们的库存:

  • 豆子
  • 白饭
  • 蔬菜
  • 生菜
  • 鳄梨
  • 酸奶油
  • 莎莎舞

客户可以订购少至1个浇头,但最多订购5个浇头。浇头不必不同。

您可能会假设客户在订购之前熟悉菜单,因此所有订单将仅包含我们库存的食材。也就是说,输入将始终有效。

服务炸玉米饼

客户要求将炸玉米饼打印到STDOUT。它们在食物中带有一些前导或尾随空格是很好的。

市场研究表明,每个人都想将炸玉米饼当作单词吃,而且所有大写字母的单词都更具风味。因此,我们将在所有大写形式中以无空格的形式列出浇头。

为了有艺术感的表达,我们不能只是将玉米饼中的东西放进去并称其为好,我们必须将浇头从左到右轻轻放置,然后根据需要包裹和重复。每个浇头至少要获得2条线。一旦我们检查了样品盘,一切将变得更加清晰。

样品盘

让我们看一些示例订单。

客户下单: Beans,Veggies,Rice,Lettuce,Sour Cream

我们提供:

   SOUR
  CREAMS
T LETTUC L
O ELETTU I
R RICERI T
T CERICE R
I VEGGIE O
L SVEGGI T
L BEANSB A
 A EANS L
  TORTIL

看起来很好吃,对不对?浇头会在6个字符后换行,并重复以填满2行,并截断为12个字符。第一个成分的顶行有6个字符,而第二行只有4个字符。这样可以确保它适合玉米饼的褶皱。同样,最后一个成分始终在其顶行得到4个字符,在其第二行得到6个字符。

如果客户连续订购两个相同的配料会怎样?继续为该成分的所有连续行包装该成分。

客户下单: Lettuce,Lettuce,Lettuce,Salsa

我们提供:

T  SALS  L
O ASALSA I
R LETTUC T
T ELETTU R
I CELETT O
L UCELET T
L TUCELE A
 A TTUC L
  TORTIL

客户下单: Guacamole

T        L
O        I
R        T
T        R
I        O
L  GUAC  T
L AMOLEG A
 A UACA L
  TORTIL

只有一种成分?在顶部给出4个额外字符的价值。

本月员工


烹饪愉快!

Answers:


3

JavaScript(ES6),269263字节

x=>eval('x=x.split`,`;p=o="";for(i=10;i--;){t=x[1]?x[i/2|0]:x[i>2|0];t!=p?r=(n=0,t?t.replace(s=" ",""):s).toUpperCase().repeat(99):0;m=r.slice(n,n+=p&&i?6:4);p&&i?0:m=s+m+s;p=t;o+=(i>7?t?s+s+m:"":i?"TORTILL"[7-i]+s+m+s+"LITROTA"[7-i]:` A${m}L`)+`\n`}')+"  TORTIL"

说明

x=>
  eval('                                          // use eval to save writin return and {}
    x=x.split`,`;                                 // convert ingredients to an array
    p=                                            // p = previous ingredient
      o="";                                       // o = output text
    for(i=10;i--;){                               // loop from top to bottom of taco
      t=x[1]?x[i/2|0]                             // t = ingredient text
        :x[i>2|0];                                // start a line early if 1 ingredient
      t!=p?r=                                     // r = repeated ingredient text
        (n=0,                                     // n = current index in repeated text
        t?t.replace(s=" ",""):s)                  // get the ingredient (or space)
        .toUpperCase().repeat(99):0;              // upper-case and repeat the ingredient
      m=r.slice(n,n+=p&&i?6:4);p&&i?0:m=s+m+s;    // m = text in the middle of the taco
      p=t;                                        // set p to current ingredient

      // Add the appropriate output
      o+=(i>7?t?s+s+m:"":i?"TORTILL"[7-i]+s+m+s+"LITROTA"[7-i]:` A${m}L`)+`\n`

    }                                             // implicit: eval returns o
  ')+"  TORTIL"                                   // return the output text

测试


作为赏金期结束时最短的答案,您将获得+50的赏金!在问题发布一周后,由于两个发布的答案中最短的一个,您将赢得梦co以求的复选标记。做得好,感谢您的参与!:)
Alex A.

6

Python 3,475个字节

n=range
s=input().upper().replace(" ","").split(",")[::-1]
s=sum(zip(s,s),tuple())
t=""
p=0
l=len(s)
if l==2:q=s[0];s=[q,q,q];l=3
r=[]
e=" "
f=e*4
d=[f," AL ","L  A","L  T","I  O","T  R","R  T","O  I","T  L",f,f]
for i in n(l):
 h=s[i]
 c=6
 a=""
 if i==0 or i==l-1:c=4
 if t!=h:
  p=0
  t=h
 for x in n(0,c):
  a+=h[p]
  p+=1
  if p==len(h):
   p=0
 if c==4:a=e+a+e
 r+=[a]
r=["TORTIL"]+r[::-1]
for i in n(0,11):
 y=10-i
 x=d[y]
 m=e*6
 if y<=l:m=r[y]
 print(x[:2]+m+x[2:])

可能太长了,但我还是发布一些内容!


我认为您可以替换r=(r+["TORTIL"])[::-1]r=["TORTIL"]+r[::-1]range(0,l)也可以range(l)
lirtosiast 2015年

1. Stack Exchange用四个空格替换制表符,因此很难按原样验证分数。重新替换它们后,我得到482。2.您可以通过将空格放在if l==2:q=s[0];s=[q,q,q];l=3一行上来节省空白。3. n(l)与相同n(0,l)
丹尼斯2015年

@Dennis我可以对#1做些什么吗?
vpzomtrrfrt 2015年

1
您可以只使用一个空格而不是制表符。Python 3仍然不允许混合它们。
丹尼斯

好的,我接受了您的建议,并将其缩短了一些。
vpzomtrrfrt 2015年

4

Ruby,376 375 368 363 362字节

->a{p='ALLITROT'.chars
s='LATORTIL'.chars
t=['  TORTIL']
c=[*a.split(?,).chunk(&:upcase)]
c.map.with_index{|x,i|n=x[1].size*2
w=x[0].tr(' ','')*9*n
[n+=i==0?1:0,w.sub!(/..../,' \0 ')]if i==c.size-1
w.chars.each_slice(6).take(n).reverse.map{|l|t=["#{p.shift||' '} #{l*''} #{s.shift||' '}"]+t}}
p.map{|x|t=[x+' '*8+s.shift]+t}
t[-2].sub! /(.{6})../,' \1'
t.join$/}

仍在进行中。

(奖金:可以使用任意数量的浇头,而不仅仅是5。主要是因为我一开始没有看到该规则> _ <)

非高尔夫版本:

#!/usr/bin/env ruby

def make_taco ingredients
    # These three variables make up the tortilla.
    prefixes = 'ALLITROT'.chars
    suffixes = 'LATORTIL'.chars
    taco = ['  TORTIL']
    # .chunk is a Ruby builtin that's *incredibly* useful for this challenge.
    chunks = ingredients.split(',').chunk{|x| x}.to_a
    # Loop through every chunk of equal ingredients.
    chunks.each_with_index do |ingredient, idx|
        # Get the number of lines the group of ingredients should take up.
        count = ingredient[1].length * 2
        # *9 because that's guaranteed to be enough ingredient.
        wrapped = ingredient[0].upcase.sub(' ','') * 9 * count
        # If this is the last element...
        if idx == chunks.length - 1
            # Add spaces strategically to position the top "layer."
            wrapped.sub! /..../, ' \0 '
            # If this is also the first element...
            if idx == 0
                # We need to make one more row.
                count += 1
            end
        end
        # Arrange the ingredient like so, and then for each "layer"...
        wrapped.chars.each_slice(6).take(count).reverse.each do |line|
            # Add it to the taco, along with prefixes/suffixes if they exist.
            taco.push "#{prefixes.shift || ' '} #{line * ''} " +
                "#{suffixes.shift || ' '}"
        end
    end
    # Fill in the rest of the prefixes and suffixes we didn't use.
    prefixes.each do |prefix|
        taco.push prefix + ' ' * 8 + suffixes.shift
    end
    # Fix the "offset" on the second-to-last line.
    taco[1].sub! /(.{6})../, ' \1'
    # Since we've been building the taco bottom-up, reverse, join, and return.
    taco.reverse.join("\n")
end

puts make_taco 'Beans,Veggies,Rice,Lettuce,Sour Cream'
puts
puts make_taco 'Lettuce,Lettuce,Lettuce,Salsa'
puts
puts make_taco 'Guacamole'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.