画时间线


23

给定代表日期的整数列表的输入,输出如下所示的ASCII美工时间轴:

<----------------------------->
  A     B  C           D    E

上面的时间轴是input的输出[1990, 1996, 1999, 2011, 2016]。请注意有关时间轴的几件事:

  • 输出的第一行是一个小于符号(<),多个破折号等于dateOfLastEvent - dateOfFirstEvent + 3(因为必须加一个破折号以包含最后一个日期,然后再加上两个破折号以进行填充),然后是大于符号(>)。

  • 在输出的第二行中,每个事件都放置在位置dateOfEvent - dateOfFirstEvent + 2(假设零索引)。因此,第一个事件放置在位置2,位置在的右侧,两个字符<,最后一个事件类似地放置在的左侧,两个字符>

  • 每个事件都由一个字母表示。事件1是A,事件2是B,等等。永远不会超过26个事件。如果需要,可以使用小写字母。

  • 没有尾随空格。程序末尾仅允许在末尾添加换行符。

此外,

  • 事件不一定按顺序给出。但是,日期仍然根据其在数组中的位置进行标记。例如,[2, 3, 1, 5, 4]必须输入的 输出

    <------->
      CABED
    
  • 您可能会获得一个或多个事件作为输入。例如,[12345]必须输入的输出

    <--->
      A
    
  • 您可能会假设输入内容永远不会包含重复的日期。

输入可以是整数/字符串的数组/列表,也可以是由任何非数字字符分隔的单个字符串。输入的日期范围为1 ≤ x ≤ 32767

因为这是,所以以字节为单位的最短代码将获胜。

测试用例:

32767 32715 32716 32750 32730 32729 32722 32766 32740 32762
<------------------------------------------------------->
  BC     G      FE         I         D           J   HA
2015 2014
<---->
  BA
1990 1996 1999 2011 2016
<----------------------------->
  A     B  C           D    E
2 3 1 5 4
<------->
  CABED
12345
<--->
  A

Answers:


5

Pyth,37 36 35 34字节

:*dlp++\<*\-+3-eJSQhJ">
"mhh-dhJQG

说明:(为此,\n为简单起见,换行符将替换为)

:*dlp++\<*\-+3-eJSQhJ">\n"mhh-dhJQG

                                    - autoassign Q = eval(input())
                                    - G = "abcdefghijklmnopqrstuvwxyz"

    p++\<*\-+3-eJSQhJ">\n"          -    print out the first line

            +3-eJSQhJ               -        Get the number of dashes
                 SQ                 -            sorted(Q)
                J                   -           autoassign J = ^
               e                    -          ^[-1]
              -                     -         ^-V
                   hJ               -          J[0]
            +3                      -        ^+3

         *\-                        -       ^*"-"
      +\<                           -      "<"+^
     +               ">\n"          -     ^+"-->\n"
    p                               -    print(^)

 *dl                                -  work out the number of spaces to print
   l                                -   len(^)
 *d                                 -  ^*" "
:                                 G - For i in V: ^[i] = G[i]
                          mhh-dhJQ  -  Work out the positions of the characters
                          m      Q  -  [V for d in Q]
                               hJ   -     J[0]
                             -d     -    d-^
                           hh       -   ^+2

在这里尝试!


5

PowerShell,120 108字节

param($a)$n,$m=($a|sort)[0,-1];"<$('-'*($b=$m-$n+3))>";$o=,' '*$b;$i=97;$a|%{$o[$_-$n+2]=[char]$i++};-join$o

接受输入,$a然后分别将$n$m设置为最小值和最大值。通过$(...)在字符串内执行代码块以生成适当数量的-字符,我们将输出下一部分的时间轴。然后,我们生成一个仅包含空格的相同长度的数组,并将输出字符设置为$i

然后,通过输入,我们可以循环$a使用|%{...}。每个循环我们都设置适当的$o值。最后,我们-join $o一起组成一个字符串。由于这留在管道上,因此输出是隐式的。

编辑以删除.TrimEnd()命令,因为$o始终保证的最后一个字符为字母。

PS C:\Tools\Scripts\golfing> .\draw-a-timeline.ps1 2015,2014,2000
<------------------>
  c             ba

4

Ç - 294 287 220 191 184 178 174字节

在盯着一些疯狂的代码之后,我至少已经将它弄明白了……

注意: 第一个循环要求二进制文件的执行将atoi()on的结果赋予0 argv[0]。如果不是,这将导致二进制(名称)被作为事件包括在内。导致无效的示例:

$ 42/program 1 2 3
# 42/program gives 42 from argv[0], fail.

$ 1program 3 2 9
# 1program gives 1 from argv[0], fail.

$ 842 3 2 9
# 842 gives 842 from argv[0], fail.

不知道这是否是有效要求。

char y[32769];n,m;main(i,a)char**a;{for(;n=atoi(a[--i]);y[n>m?m=n:n]=64+i);for(;!y[++i];);printf("<");for(n=i;i<=m;i+=printf("-"))!y[i]?y[i]=' ':0;printf("-->\n  %s\n",y+n);}

跑:

./cabed 32767 32715 32716 32750 32730 32729 32722 32766 32740 32762
<------------------------------------------------------->
  BC     G      FE         I         D           J   HA

./cabed 2 1 3 5 4
<------->
  BACED

./cabed 2016
<--->
  A

./cabed 130 155 133 142 139 149 148 121 124 127 136
<------------------------------------->
  H  I  J  A  C  K  E  D     GF     B

取消高尔夫:

#include <stdio.h>
#include <stdlib.h>

char y[32769]; /* Zero filled as it is in global scope. */
int n, m;

int main(i, a) 
    char**a; 
{
    /* Loop argv and set y[argv[i] as int] = Letter, (Event name).
     * Set m = Max value and thus last data element in y. */
    for ( ; n = atoi(a[--i]); y[n > m ? m = n : n] = 64 + i)
        ;

    /* i = 0. Find first element in y that has a value. (Min value.) */
    for (; !y[++i]; )
        ;

    printf("<");

    /* Save min value / y-index where data starts to n.
     * Print dashes until y-index = max 
     * Build rest of event string by filling in spaces where no letters.*/
    for (n = i; i <= m; i += printf("-"))
        !y[i] ? y[i] = ' ' : 0;

    printf("-->\n  %s\n", y + n);

    return 0;
}

3

MATL,40 41字节

0lY2in:)GtX<-I+(t~32w(ctn45lbX"60hP62hcw

在线尝试!

0          % array initiallized to 0
lY2        % string 'ABC...Z'
in:)       % input array. Take as many letters as its length
GtX<-I+    % push input again. Duplicate, subtract minimum and add 3
(          % assign selected letter to those positions. Rest entries are 0
t~32w(     % replace 0 by 32 (space)
c          % convert to char
tn45lbX"   % duplicate, get length. Generate array of 45 ('-') repeated that many times
60hP62h    % prepend 60 ('<'), postpend 62 ('>')
c          % convert to char
w          % swap. Implicit display

2

Ruby,83个字符

->a{n,m=a.minmax
s=' '*(d=m-n+3)
l=?@
a.map{|i|s[i-n+2]=l.next!}
puts ?<+?-*d+?>,s}

样品运行:

irb(main):001:0> ->a{n,m=a.minmax;s=' '*(d=m-n+3);l=?@;a.map{|i|s[i-n+2]=l.next!};puts ?<+?-*d+?>,s}[[32767,32715,32716,32750,32730,32729,32722,32766,32740,32762]]
<------------------------------------------------------->
  BC     G      FE         I         D           J   HA

2

JavaScript(ES6),124

l=>(l.map(v=>r[v-Math.min(...l)]=(++i).toString(36),r=[],i=9),`<-${'-'.repeat(r.length)}->
  `+[...r].map(x=>x||' ').join``)

测试

F=
l=>(l.map(v=>r[v-Math.min(...l)]=(++i).toString(36),r=[],i=9),`<-${'-'.repeat(r.length)}->
  `+[...r].map(x=>x||' ').join``)

console.log=x=>O.textContent+=x+'\n'

test= [[32767,32715,32716,32750,32730,32729,32722,32766,32740,32762],
[2015,2014],[1990,1996,1999,2011,2016],[2,3,1,5,4],[12345]]

test.forEach(x=>console.log(x+'\n'+F(x)+'\n'))
<pre id=O></pre>


2

PHP,129个 126 125 121 117 115字节

使用ISO 8859-1编码。

$l=min([$h=max($z=$argv)]+$z)-3;echo~str_pad(Ã,$h-$l,Ò).~ÒÁõ;for(;$l<$h;)echo chr((array_search(++$l,$z)-1^32)+65);

像这样运行(-d仅出于美观目的而添加):

php -r '$l=min([$h=max($z=$argv)]+$z)-3;echo~str_pad(Ã,$h-$l,Ò).~ÒÁõ;for(;$l<$h;)echo chr((array_search(++$l,$z)-1^32)+65);' 1990 1996 1999 2016 2011 2>/dev/null;echo

非高尔夫版本:

// Get the highest input value.
$h = max($z = $argv);

// Get the lowest value, setting the first argument (script name) to the highest
// so it is ignored.
$l = min([$h] + $z);

// Output the first line.
echo "<".str_repeat("-",$h - $l + 3).">\n  ";

// Iterate from $l to $h.
for(;$l <= $h;)
    // Find the index of the current iteration. If found, convert the numeric
    // index to a char. If not found, print a space.
    echo ($s = array_search($l++, $z)) ? chr($s+64) : " ";
  • 通过打印循环中的前导空格并更改<=为,节省了3个字节<
  • 使用str_pad代替节省了一个字节str_repeat
  • 通过使用按位逻辑将0(false)转换为32,以及从0到97的所有值开始转换,节省了4个字节。然后将该数字转换为char。
  • 使用否定扩展ASCII产生,和换行符<,节省了4个字节->
  • 在填充之后而不是之前通过否定字符串节省了2个字节

1

Perl,109个字节

为包括+1 -p

$l=A;s/\d+/$h{$&}=$l++/ge;($a,$z)=(sort keys%h)[0,-1];$o.=$h{$_}//$"for$a..$z;$_='<'.'-'x($z-$a+3).">$/  $o"

预期在stdin:用空格分隔的数字上输入。例:

$ echo 2016 2012 2013 | perl -p file.pl
<------->
  BC  A

有点可读:

$l=A;                                   # Intialize $l with the letter A
s/\d+/$h{$&}=$l++/ge;                   # construct %h hash with number->letter
($a,$z) = (sort keys %h)[0,-1];         # grab min/max numbers
$o .= $h{$_} // $" for $a..$z;          # construct 2nd line: letter or space
$_= '<' . '-' x ($z-$a+3) . ">$/  $o"   # construct 1st line, merge both lines to $_ output

1

Python 2 173 172 182字节

由于缺少Python,这是我的第一篇文章:

import sys
d=dict([(int(v),chr(65+i))for(i,v)in enumerate(sys.argv[1:])])
k=sorted(d.keys())
f=k[0]
s=k[-1]-f+3
o=list(" "*s)
for i in k:o[i-f+2]=d[i]
print "<"+"-"*s+">\n"+"".join(o)

原始外观如下:

import sys

dates = dict([(int(v), chr(65+i)) for (i,v) in enumerate(sys.argv[1:])])
keys = sorted(dates.keys())
first = keys[0]
out_size = keys[-1] - first + 3
out = list(" " * out_size)
for date in keys: out[date - first + 2] = dates[date]
print "<" + "-" * out_size + ">\n" + "".join(out)

1
您需要import sys高尔夫版本。
Mego,2016年

好的,我会的,但是我从来没有见过高尔夫版本中的任何进口产品,所以我就忽略了它
BloodyD 2016年

0

Groovy,106 99个字符

{n=it.min()
o=[l="A"]
it.each{o[it-n]=l++}
"<${"-"*(it.max()-n+3)}>\n  "+o.collect{it?:" "}.join()}

样品运行:

groovy:000> print(({n=it.min();o=[l="A"];it.each{o[it-n]=l++};"<${"-"*(it.max()-n+3)}>\n  "+o.collect{it?:" "}.join()})([32767,32715,32716,32750,32730,32729,32722,32766,32740,32762]))
<------------------------------------------------------->
  BC     G      FE         I         D           J   HA
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.