タイムラインを読む


11

タイムラインを描くに触発。

イベントのタイムラインを指定して、各イベントの日付を出力します。

タイムラインは次の形式で提供されます

    2000
--AF-C-------E--D---B--

最初の行は、既知の時点を示しています。既知の年は、常に既知の年の最初の桁の下の文字です

あなたは仮定するかもしれません:

  • タイムライン自体では、各文字は1年を表します。
  • 常に少なくとも1つのイベントがあります
  • イベントは順番になっている必要はありません
  • 間の範囲内のすべての文字Aと最も遠い文字が存在すると仮定することができます
  • 最大26のイベントがあります
  • 既知のポイントには右側にパディングがありません
  • 既知の点と出力の両方に負の数が存在する可能性があります
  • 2 ^ 32より大きい数値を処理する必要はありません。
  • 2 ^ 8より大きい範囲を処理する必要はありません。
  • 入力は大文字ではなく小文字で入力できます

このフォームでタイムラインを取り、キー日付を順番に出力するプログラムを作成する必要があります(A、B、C、D ...)

便利な形式で出力できますが、指定された形式で入力する必要があります。

テストケース:

    2000
--AF-C-------E--D---B--
[1998, 2016, 2001, 2012, 2009, 1999]

     10
--C-AB--D
[9, 10, 7, 13]

     0
--ABCDEFG--
[-3, -2, -1, 0, 1, 2, 3]

          5
--ABCDEFG--
[-3, -2, -1, 0, 1, 2, 3]

1
...指定された形式で入力します。あれは?改行を含む単一の文字列?または2つの文字列?
edc65

1
2つの文字列または改行付きの文字列

回答:


1

Pyth、16バイト

入力を大文字ではなく小文字で入力します。

VS@GJw+-sz/zdxJN

2

05AB1E19 18バイト

コード:

ð¢>UágF²N>.bkX-¹+,

説明:

ð¢                   # Count the number of spaces before the year starts
  >                  # Increment by 1
   U                 # Pop and store this into X
    á                # Keep the alphabetic characters of the second input
     g               # Take the length
      F              # For N in range(0, length), do...
       ²             #   Take the second input
        N>           #   Push N and increment by 1
          .b         #   Converts 1 to A, 2 to B, etc.
            k        #   Find the index of that letter in the second input
             X       #   Push X
              -      #   index - X
               ¹     #   Get the first input, which contains the year
                +    #   Add to the difference of the index
                 ,   #   Pop and output the sum

オンラインでお試しください!

CP-1252エンコードを使用します。


1

JavaScript(ES6)、72

(a,b,r=[])=>[...b].map((x,i)=>r[parseInt(x,36)-10]=+a+i-a.search`\\d`)&&r

テスト

f=(a,b,r=[])=>[...b].map((x,i)=>r[parseInt(x,36)-10]=+a+i-a.search`\\d`)&&r

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

;[['    2000','--AF-C-------E--D---B--'],
['     10','--C-AB--D'],
['     0','--ABCDEFG--'],
['          5','--ABCDEFG--']]
.forEach(t=>{
  var a=t[0],b=t[1],r=f(a,b)
  console.log(a+'\n'+b+'\n'+r+'\n')
})  
<pre id=O></pre>


1

Bash + coreutils、68

行1は引用されたコマンドラインオプションとして入力され、行2はSTDINから入力されます。

s="${1//[0-9]}"
fold -1|nl -v$[$1-${#s}]|sort -k2|sed '/-$/d;s/.$//'

1

Perl、58 + 1 = 59バイト

/\d/;$b=<>;for$c(A..Z){($z=index$b,$c)>-1&&say$_+$z-$-[0]}

壊す:

/\d/;                    # Match first digit in input string, this will set $-[0]
$b=<>;                   # Read next line (--A--CB--...) into $b
for $c (A..Z){           # Iterate over A, B, C, ... Z
  ($z=index$b,$c) >-1 && # If the character is found inside $b
  say $_+$z-$-[0]        #   then print
}

が必要-nと無料-M5.010

# Added line breaks for each test case
$ perl -nE'/\d/;$b=<>;for$c(A..Z){($z=index$b,$c)>-1&&say$_+$z-$-[0]}' tl
1998
2016
2001
2012
2009
1999

9
10
7
13

-3
-2
-1
0
1
2
3

-3
-2
-1
0
1
2
3
$ cat tl
    2000
--AF-C-------E--D---B--
     10
--C-AB--D
     0
--ABCDEFG--
          5
--ABCDEFG--

0

Pyth、22バイト

V+r\AJeSKwJ+xKN-izT/zd

いいえ、チャレンジを投稿する前にこれを書きませんでした。

説明:

                       - autoassign z = input()
 +r\AJeSKwJ            -  create range of letters
        Kw             -      autoassign K = input()
      eS               -     sorted(K)[-1] (get the biggest character)
     J                 -    autoassign J = ^
  r\A                  -   range("A", ^)
 +        J            -  ^ + J
V                      - for N in ^: V
               -izT/zd - Get the number at the start
                izT    -   int(z, 10)
               -       -  ^-V
                   /zd -   z.count(" ")
           +           - V+^
            xKN        -  K.index(N)

ここで試してみてください


0

Python 3、118

男、今日は長いPythonの答えの日です。

def f(p,l):o=sum(x<'0'for x in p);e={x:i-o+int(p[o:])for i,x in enumerate(l)if'@'<x};return list(map(e.get,sorted(e)))


0

Perl、80 79 71 67バイト

($a=<>)=~/\d/;$b=<>;say$a+$_-$-[0]for grep{$_+1}map{index$b,$_}A..Z

12バイトの@ dev-nullに感謝します!

($a=<>)=~/\d/;  # read first line of input, find position of first digit
                # (saved in the $- variable)
$b=<>;          # read the second line
                                           A..Z  # generate range 'A','B',...
                            map{index$b,$_}      # find index for each
                  grep{$_+1}                     # select only those != -1
              for                                # foreach of remaining...
say$a+$_-$-[0]                                   # calculate proper date
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.