分析你的椅子


11

此挑战基于Downgoat的Adjust your chair

挑战

您刚刚调整了新椅子!非常适合您。但是,车轮吱吱作响,您知道维修人员会更改调整。问题是,您没有标尺,因此您必须编写一个程序来为您测量。 维修人员只能等待这么长时间。因此,您的代码需要尽可能短。
可调节椅子的例子

例子

O
|
|
| _
| |
|_|_
  |
  |
  O

5,3,2,2,1

O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO

3,5,2,3,5

O
|      _
|______|______
 ______|______
 OOOOOOOOOOOOO

2,13,1,1,13

椅子零件

椅子具有各种组件:

O  <- Headrest
|
|  <- Backrest
|  _  <- Armrest
|  |
|__|__ <- Seat
   |  
   |   <- Leg
  _|_
  OOO  <- Wheels

详细的椅子说明

椅子的零件有:


头枕:靠背上方始终只有一个头枕

O
|

靠背:的数量|靠背高度

O
|
|

座椅:数量_座位宽度,还有一个|在中间的扶手上。

__|__

扶手:的数量|扶手高度。这将插入座椅中间。

_
|
|

腿:的数量|腿的高度

|
|

轮子:轮子位于腿下方居中。如果它们不止一个,那么除了中心轮之外的所有齿轮都将_在其上方。

_ _
OOO

输出量

给定一把椅子,您将输出各种变量。

输出应按以下顺序:

  1. 靠背高度
  2. 座椅宽度始终为奇数
  3. 扶手高度始终小于靠背高度
  4. 腿高
  5. 车轮数始终小于或等于座椅宽度始终为奇数

输出可能带有尾随换行符,或者如果是函数,则采用数组/列表形式。

排行榜

为了确保您的答案显示出来,请使用以下Markdown模板以标题开头。

# Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,可以将旧分数保留在标题中,方法是将它们打掉。例如:

# Ruby, <s>104</s> <s>101</s> 96 bytes

如果您想在标头中包含多个数字(例如,因为您的分数是两个文件的总和,或者您想单独列出解释器标志罚分),请确保实际分数是标头中的最后一个数字:

# Perl, 43 + 2 (-p flag) = 45 bytes

您还可以将语言名称设置为链接,然后该链接将显示在页首横幅代码段中:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

扶手是否被测量?
J Atkin

Answers:


3

Lua,187字节

我想我也许可以再打些高尔夫,但是我认为目前这很好。另外,如果输出需要用逗号分隔,我可以解决这个问题,但这仍然可以满足方法要求。

同样,输入一次馈入一行。输入车轮后,用空行按Enter键以完成输入。

c={}i=1t=""while c[i-1]~=""do c[i]=io.read():gsub("%s+","")t=t..c[i]a=(not a and c[i]:find("_")and i or a)s=(not s and c[i]:find("_|_")and i or s)i=i+1 end print(s-1,c[s]:len()-1,s-a,#c-s-2,c[#c-1]:len())

不打高尔夫球

c={}
i=1
while c[i-1]~=""do 
    c[i]=io.read():gsub("%s+","")          --remove spaces
    a=(not a and c[i]:find"_"and i or a)   --armrest position
    s=(not s and c[i]:find"_|_"and i or s) --seat position
    i=i+1
end
print(s-1, c[s]:len()-1, s-a, #c-s-2, c[#c-1]:len())

(位置是从上到下测量的,因此顶部的“ O”是位置1,车轮是最大位置。

  • 靠背高度是座椅的位置减一,以补偿顶部的“ O”。
  • 座椅尺寸是指座椅位置上的弦的长度减去一,以补偿靠背。
  • 扶手高度是座椅位置减去扶手位置。
  • 腿高是椅子的高度(#c)减去座椅的位置再减去2,以补偿车轮和座椅的高度。
  • 轮数是最终弦的长度。

3

Groovy,161个字节!!!

好极了!!最后没有!

f={s->a=s.split(/\n/)
b=a.findIndexOf{it.contains('|_')}
d=b-a.findIndexOf{it.contains('_')}
print"$b,${a[b].count('_')+1},$d,${a.size()-b-2},${s.count('O')-1}"}

取消高尔夫:

f={String s ->
    split = s.split(/\n/)
    bottomOfChairBack = split.findIndexOf {it.contains('|_')}
    armHeight = bottomOfChairBack-split.findIndexOf {it.contains('_')}
    width = split[bottomOfChairBack].count('_')+1
    height = split.size() - bottomOfChairBack - 2

    wheelCount = s.count('O')-1
    return [bottomOfChairBack, width, armHeight, height, wheelCount]
}

非高尔夫程序的测试:

assert f('''O
|
|
| _
| |
|_|_
  |
  |
  O''') == [5, 3, 2, 2, 1]

assert f('''O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO''') == [3,5,2,3,5]

assert f('''O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO''') == [3,5,2,3,5]

assert f('''O
|      _
|______|______
 ______|______
 OOOOOOOOOOOOO''') == [2,13,1,1,13]

2

Pyth,57 54 53 50字节

可能可以进一步打高尔夫球。-3个字节,感谢issacg提供的单个字符串技巧。

=kjb.z
=H/k+b\|
-/k\_=G-/k\O2
--/k\|H=Nt/k+bd
N
hG

说明:

=kjb.z
=k              Assign k
     z          Input
  jb.           Join list by newlines

=H/k+b\|
=H              Assign H
  / +b\|        Count occurrences of "\n|"
   k            In input
                (Implicit: print backrest height)

-/k\_=G-/k\O2
     =G         Assign G
       -/k\O2   The number of wheels minus 1
-/k\_           Count the number of "_"
                (Implicit: print seat width)

--/k\|H=Nt/k+bd
       =N       Assign N
          /k+bd Count the number of lines starting with " "
         t      Subtract 1 (N is now the leg height)
  /k\|          Count the number of "|"
 -    H         Subtract the "|" for the backrest
-               Subtract leg height
                (Implicit: print armrest height)

N               Print leg height

hG              Print the number of wheels

1
要创建一个单字符字符串,请使用\。所以"_"=\_
isaacg

哎呀,我没办法击败它;)
J Atkin

2

Perl,93 + 2 = 95 90 + 1 = 91 83 + 1 = 84字节

显然,输出不需要逗号分隔

调用perl -n chair.pl chairInput(该标志罚1B)。

END{print$b,2+$u-$o,$a,$.-$b-2,$o-1}$u+=s/_//g;$o+=s/O//g;s/^\|//&&$b++&&/\|/&&$a++

取消高尔夫:

END{         # Put the END block first to save 1 ;
    print
        $b,   
    2+$u-$o,
    $a,
    $.-$b-2, # $. is the number of lines total
    $o-1
}
$u+=s/_//g; # count _s incrementally
$o+=s/O//g; # count Os incrementally
s/^\|// && $b++ # it's backrest if it starts with |
    && /\|/ && $a++ # and it's armrest if it has another one

先前版本:

调用 perl -0n chair.pl < chairInput

s/^\|//&&$b++?/\|/&&$a++:$h++for split"
",$_;$,=",";print$b,2+s/_//g-($o=s/O//g),$a,$h-3,$o-1

说明:

s/^\|// && $back++   # the backrest is all lines starting with |
    ? /\|/ && $arm++ # the armrest is all of those lines with another |
    : $height++      # otherwise it counts for the seat height
    for split"
",$_;       # literal newline for 1 byte saved
$,=",";     # output separator
print
    $back,
    2+s/_//g-($o_count=s/O//g),  # you can find the seat size
                                 # from the different between the number
                                 # of Os and _s
    $arm,
    $height-3,
    $o_count-1

1

Python 3中,160个 158字节

该代码仅在以下情况下有效:1)armrest height > 0否则,_计数中断; 2)seat width > 1否则,扶手阻塞一号宽度座椅,并且_计数中断。

def f(s):
 a=s.split("\n");x=[];y=[];l=len(a)
 for i in range(l):
  m=a[i].count("_")
  if m:x+=i,;y+=m,
 return x[1],y[1]+1,x[1]-x[0],l-x[1]-2,s.count("O")-1
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.