タイムラインを描く


23

日付を表す整数のリストの入力が与えられた場合、次のようなASCIIアートタイムラインを出力します。

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

上記のタイムラインはinputの出力です[1990, 1996, 1999, 2011, 2016]。タイムラインに関するいくつかのことに注意してください。

  • 出力の最初の行は小なり記号(<)、ダッシュの数に等しいdateOfLastEvent - dateOfFirstEvent + 3(最後の日付を含めるために1つ追加し、次にパディングのためにさらに2つ追加する必要があるため)、その後に大なり記号(>)です。

  • 出力の2行目では、各イベントが位置に配置されdateOfEvent - dateOfFirstEvent + 2ます(インデックスがゼロの場合)。したがって、最初のイベントは2、の2文字右の位置に配置され<、最後のイベントは、同様にの左の2文字に配置され>ます。

  • 各イベントは文字で表されます。イベント1はA、イベント2はBなどです。26を超えるイベントはありません。必要に応じて小文字を使用できます。

  • 末尾の空白はありません。許可される余分な空白は、プログラムの最後にある末尾の改行のみです。

さらに、

  • イベントは必ずしも順番に与えられるわけではありません。ただし、日付は依然として配列内の位置に従ってラベル付けされます。たとえば、の入力は[2, 3, 1, 5, 4]出力する必要が あります

    <------->
      CABED
    
  • 入力として1つ以上のイベントが与えられる場合があります。たとえば、の入力は[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

回答:


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

C - 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

ルビー、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ゴルフバージョンで必要です。
メゴ


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
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.