数値をサイズアップする

Oct 03 2020

イントロ

与えられた整数\$< 100,\$ 数値の英語表現を拡張/短縮して、その値と同じ数の文字を使用します。

サンドボックス

ルール

番号を取る\$n\$、そしてこの辞書から単語でそれを取得します。この辞書は好きなように使うことができます。(リスト、リンクからの読み取り、コマンドライン引数、STDIN、変数など。)

これがJSON配列としてのファイルです。(Lyxalの礼儀)

次に、文字列を使用して次の手順を実行します。

  • 文字列の長さが数字よりも短い場合は、長さが数字と一致するまで、その文字の一部を所定の位置で繰り返します。
  • 最初と最後の文字を繰り返さないでください。また、他の文字の繰り返し回数は最大で1つ異なる必要があります(たとえば、一部の文字を5回繰り返し、他の文字を6回繰り返すことができます。どちらでもかまいません。丁度)。
  • 文字列の長さが数字よりも大きい場合は、最初と最後を除くすべての文字を削除して、長さが数字と一致するようにします。1と0は小さすぎるため、例外です。
  • 文字列内の文字の順序を維持する必要があります。
  • 例:
50 → f[ift]y → 3 letters must be duplicated 16 times
61 → s[ixtyon]e → 5 letters must be duplicated 10 times, 1 character must be duplicated 9 times

ステップバイステップの実行

11を例にとると、

(としてフォーマットされますword → length)

eleven → 6
 ^
elleven → 7
   ^
elleeven → 8
     ^
elleevven → 9
       ^
elleevveen → 10
  ^
ellleevveen → 11 (end)

例

2 → to
3 → the or tre or tee
4 → four
5 → fiive or fivve
7 → seevven or sevveen or seeveen 
10 → teeeeeeeen

Special cases:
0 → (Any null value/no output)
1 → o

受賞基準

これはコードゴルフです。各言語の最短コードが優先されます。

ドミニク・ヴァン・エッセンが主張するLoTMバウンティ!

この質問に対する最初のハスクの回答は、追加の取り組みの部分でここに記載されている基準を満たしている場合、Zgarbの報奨金に加えて+ 50repの報奨金を受け取ります。

回答

9 DominicvanEssen Oct 03 2020 at 18:24

R、(110 105 96) 9695バイト

うまくいけば、今正しい質問に答えてください...

function(n,a)cat(substring(b<-a[n+1],r<-sort(c(1,l<-nchar(b),rep(3:l-1,n))[1:n]*!!n),r),sep='')

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

ドー!質問を読んでください、愚かです!

さて、最初の(いくつかの)試みは正しい質問に答えていませんでした。なぜなら、私はチャレンジを正しく読んでおらず、最初と最後の文字が繰り返されるべきではないことに気づかなかったからです。偶然にも、ほぼ完全な書き直しが同じバイト数で出てきました!

どうやって?ゴルフされていないコード:

size_up=
function(n,a){              # n is number, a is array of text strings
  b=a[n+1]                  # get the text string for this number
  l=nchar(b)                # get the number of characters
  r=sort(                   # r = sort the indices of characters to output:
      c(1,l,                # we need index 1 and the last index (l)...
        rep(2:(l-1),n))     # then plenty of repeats of 2..(l-1) ...
      [1:n])                # and, from this, we'll use the first n.
  if(n)                     # only output anything if n>0 ...
    cat(substring(b,r,r),sep='')
                            # ...in which case concatenate the indexed
}                           # characters of the text string b.
7 Lynn Oct 05 2020 at 20:17

Haskell、59バイト

d!n|h:t<-d!!n=take n$h:[t!!div(i*length t-i)(n-1)|i<-[1..]]

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

使用法:["zero","one",...]!n。

たとえば、入力の11場合、次のようにインデックスが付けられた'e'後に出力され"leven"ます:$$ \ left \ lfloor \ frac {4} {10} \ right \ rfloor、\ left \ lfloor \ frac {8} {10} \ right \ rfloor 、\ dots、\ left \ lfloor \ frac {40} {10} \ right \ rfloor $$これは"lleevvveen"。(直感的には、式はのインデックス[0..4]を[0..10]に拡張し"leven"ます。)

take n$は0の場合をサポートするために使用されます(それ以外の場合、出力はです"z")が、の[1..]代わりに書き込むこともできる[1..n-1]ため、正味のコストは7ではなく4バイトになります。

7 DominicvanEssen Oct 05 2020 at 15:47

殻、28の 27 25 22 21バイト

編集:Zgarbのおかげで-2バイト、Jo Kingのおかげで-3バイト、そしてZgarbのおかげで再び-1バイト(最後の4バイトは作業して理解するのに時間がかかりました;説明を追加しましたが私のようなハスク初心者にとっては簡単ではありません...)

SṀ!(O↑¹:1S:ȯ*¹…2←L)!→

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

「今月の言語」に触発された私の最初のハスクの答え。私のR回答の
アプローチのポート。実用的なプログラムの書き方を理解するだけでかなり満足しているので(やったー!)、それはほぼ間違いなく、それほどゴルフ的ではありません...

どうやって?(Zgarb&Jo Kingのゴルフの後にコメント:今はかなり複雑です...):

まず、すべての暗黙の引数を(上付き文字⁰と²)として入力します。

SṀ!(O↑²:1S:ȯ*²…2←L)!→²⁰

コメントされたコード:

S                               # S is a 'hook' combinator to recycle argument x: Sfgx == fx(gx)
 Ṁ!                             # f is M! = map the list-index function across a list of indexes, with the list to index given by argument x
   (O↑²:1S:ȯ*²…2←L)             # g is (O↑²:1S:ȯ*²…2←L) = function to generate the list of indexes from argument x (see below)
                   !→²⁰         # x is !→²⁰ = the text name of the number (for instance, "twenty"; see below)

                                # Explanation of g:
         S                      # S 'hook' combinator again, so Sfgy here == fy(gy)
          :                     # f is : = prefix with y
           ȯ                    # g is given by ȯ*²…2←
                                # ȯ is 3-function combinator: ȯfgh == single function fgh
                ←               #   subtract 1
              …2                #   and construct a series from 2..this value,  
            *²                  #   and repeat this series n times, with n given by argument 2 (first given program argument);
                                #   so ȯ*²…2← is the function that repeats 2..x, n times, for argument y
                 L              # y is the length of the argument x
                                # So: up to here, we have :Lx *²…2←Lx
       :1                       # Now prefix with 1...
     ↑²                         # then select the first n elements, with n given by argument 2 (first given program argument)...
    O                           # and sort the result.  This is the list of indexes from x.
    
                                # Explanation of x:
                   !  ⁰         # element of argument 1 (second given), indexed by...
                    →²          # argument 2 (first given) +1
6 JonathanAllan Oct 04 2020 at 04:51

ゼリー、20バイト

ị©L⁸,_2œs/Ẉ⁸>Ø.¤j®x"

左側の数字と右側の数字名のリスト(ゼリーインデックス順)を受け入れるダイアディックリンク。これにより、文字のリストが生成されます。

オンラインでお試しください!または、100個すべてを参照してください。

どうやって?

ị©L⁸,_2œs/Ẉ⁸>Ø.¤j®x" - Link: N, Words
ị                    - (N) index into (Words) -> the word
 ©                   - copy for later
  L                  - length
   ⁸                 - chain's left argument -> N
    ,                - pair -> [N, length(Word)]
     _2              - subtract two -> [N-2, length(Word)-2]
         /           - reduce using:
       œs            -   split (implicit range [1..N-2]) into (length(Word)-2)
                         approximately equal parts
          Ẉ          - length of each -> sizes (call this [s1, s2, ...])
               ¤     - nilad followed by link(s) as a nilad:
           ⁸         -   chain's left argument -> N
             Ø.      -   bits -> [0,1]
            >        -   greater than? -> [N>0, N>1]
                j    - join -> [N>0, s1, s2, ..., N>1]
                 ®   - recall from earlier -> the word
                   " - zip with:
                  x  -   repeat
6 Jonah Oct 06 2020 at 04:31

J、40の39バイト

[{.[(]#~1:0 _1}2+/@}.($[:}:@}.@=#\))>@{

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

5 HyperNeutrino Oct 03 2020 at 14:39

Python 3、98バイト

def f(n,a):x,*s,y=a[n];l=len(s);d=n-2;a[0]=(x+"".join(s[i]*(d//l+(i<d%l))for i in range(l))+y)[:n]

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


-ovsのおかげで8バイトpxegerのおかげで-2バイト

(保存されたバイトの1つが2つの最適化の間で重複しているため、2つの間に-9バイト)

-ovsのおかげでさらに9バイト

5 Arnauld Oct 03 2020 at 16:08

JavaScriptの(ES6)、 78の 73バイト

期待し(n)(dictionary)ます。ゼロの場合は空の文字列を返します。

n=>d=>(s=d[n--],g=k=>k<n?s[~k?k/~-n*(s.length-2)+1|0||2:0]+g(k+1):'')(-1)

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

5 GalenIvanov Oct 03 2020 at 19:18

K(OK) 、50の 46、40バイト

-ovsのおかげで4バイト

-coltimのおかげで6バイト

{x#,/(1,(+/'(-2+#y@x;0N)#2_x#1),1)#'y@x}

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

4 corvus_192 Oct 04 2020 at 06:27

さび、203バイト

|i,a|if i<2{a[i].truncate(i)}else
if let[f,m@..,l]=&*a[i].clone(){let mut v:Vec<_>=(0..m.len()).cycle().take(i-2).collect();v.sort();a[i]=v.iter().map(|&j|m[j]).collect();a[i].insert(0,*f);a[i].push(*l)}

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

タイプのクロージャーfn(usize,&mut [Vec<char>])。結果はに書き込まれa[i]ます。

4 ovs Oct 04 2020 at 13:54

APL(Dyalogユニコード)、39の 37 36バイト

-Razetimeのおかげで2バイト!-Jo Kingの
おかげで1バイト!

仮定し⎕IO←0ます。

⊃∘⌷{⍵↑⍺/⍨1,⍨1,(⍵-2)(⌊÷+|⍨>∘⍳⊢)≢2↓⍺}⊣

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

説明

この関数は、辞書を右の引数として、整数を左の引数として取ります。

⊃∘⌷右の辞書の左のインデックスにある単語を取得する関数です。
⊣左の恒等関数です。
次に{ ... }、単語を左引数⍺として、整数を右引数として、内部関数が呼び出されます⍵。

≢2↓⍺最初の2文字を除いた単語の長さ(a)です。
⍵-2整数入力から2(b)を引いたものです。

これらの2つの引数を使用して、関数⌊÷+|⍨>∘⍳⊢が呼び出され
⍳⊢ます0 1 ... a-1。インデックスベクトルを作成します。
|⍨を計算しb mod aます。
>これをインデックスベクトルと比較します。これにより、b mod a 1'sとa - b mod a 0' sのブールベクトルが生成されます。
⌊÷の床ですb÷a。
+これをブールベクトルに追加します。

1,を追加し1、を1,⍨追加し1ます。
⍺/vに示されてvいるように、単語xからできるだけ多くのアイテムを選択します。例:1 3 2 2 2 1/'eleven'≡'ellleevveen'。これは⍨、括弧を避けるためにここで転流()されます。
⍵↑次に、必要な文字数を取得します。これが要求される0と1。

3 Neil Oct 03 2020 at 18:31

木炭、29バイト

NθF⊕θSηFθ§η∧ι⊖÷×⊕⁻ιθ⁻²Lη∨⁻²θ¹

オンラインでお試しください!リンクは、コードの詳細バージョンへのリンクです。入力を数値として受け取り、次に辞書を受け取ります。F⊕θSη2行目を最初の行に指定された長さにサイズ変更するプログラムで結果を削除します。厄介なことに、2最初と最後の文字の両方がエッジケースであるため、処理するのが最も難しい数字でした。説明:

Nθ

入力n。

F⊕θSη

のエントリまで、辞書を読んでくださいn。

Fθ

n文字をループします。(⭆θまた動作します。)

§η

計算されたインデックスによって与えられた辞書の単語の文字を出力します。

∧ι

出力の最初の文字の場合、計算されるインデックスは常に0です。

⊖÷×⊕⁻ιθ⁻²Lη∨⁻²θ¹

それ以外の場合は、文字列の末尾までの距離を0..n-2から0..l-2(l辞書の単語の長さ)までの距離を切り上げてスケーリングします。これにより0/0forが発生するためn=2、1この場合、分母は強制的に変換されることに注意してください。例n=5, five:

i=0 o=0
i=1 o=3-ceil((4-1)*(4-2)/(5-2))=3-ceil(3*2/3)=3-2=1
i=2 o=3-ceil((4-2)*(4-2)/(5-2))=3-ceil(2*2/3)=3-2=1
i=3 o=3-ceil((4-3)*(4-2)/(5-2))=3-ceil(1*2/3)=3-1=2
i=4 o=3-ceil((4-4)*(4-2)/(5-2))=3-ceil(0*2/3)=3-0=3

したがって、出力インデックスは0, 1, 1, 2, 3結果になりfiiveます。(実際には、計算は負のインデックスを使用して行われるため、実際のインデックスは0, -3, -3, -2, -1です。つまり、実際には切り上げられるのではなく、切り捨てられます。)

3 KjetilS. Oct 04 2020 at 03:41

Perl 5、77バイト

sub{@w=@{$d[$n=pop]};$h=$n<4?2:(@w-2)/($n-2);join'',@w[map.99+$h*$_,0..$n-1]}

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

3 corvus_192 Oct 04 2020 at 04:21

Scala、93バイト

i=>a=>{val f+:m:+l=a(i);(f+:Seq.fill(i)(m.indices).flatten.take(i-2).sorted.map(m):+l)take i}

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

タイプの関数を定義しますInt=>Seq[Seq[Char]]=>Seq[Char]。

説明:

i => a => {                  // define a curried lambda function with two arguments
  val f+:m:+l = a(i)         // destructure the string to extract the first and last letter
  (                          // return...
    f +:                     // the first letter prepended to
      Seq.fill(i)(m.indices) // the numbers from 0 to i-2, all repeated i times
                             // for "seven", this is 7 instances of [0,1,2]
      .flatten               // flatten this list
      .take(i-2)             // take the first i-2  numbers from the list
      .sorted                // sort them
      .map(m)                // and use them as indices for the middle part of the string
    :+l                      // append l
  )
  take i                     // to handle 0 and 1, take i letters from the result
}
2 KevinCruijssen Oct 05 2020 at 14:51

05AB1E、20バイト

è⍍₂и¾šηε{®ª}0šδè¹ù

単語のリストを追加の2番目の入力として受け取ります。
文字のリストとして出力します。

オンラインそれを試してみたり、すべてを確認\$[0,99]\$テストケース。

説明:

è                  # Index the (implicit) input-integer into the (implicit) string-list
 ā                 # Push a list in the range [1,string-length] (without popping)
  ¨¨               # Remove the last two values to make the range [1,length-2]
    ₂и             # Repeat this list 26 times: [1,2,3] → [1,2,3,1,2,3,1,2,3,...]
      ¾š           # Prepend a 0 to this list
        η          # Take all prefixes
         ε         # Map each prefix-list to:
          {        #  Sort the list
           ®ª      #  And append a -1
         }0š       # After the map: prepend a 0 to the list of lists
             δ     # Map over each list:
              è    #  Index it in the string that's still on the stack
                   #  (modulair 0-based, so the -1 indexes into the last character)
               ¹ù  # Keep the list of characters of a length equal to the first input
                   # (after which the result is output implicitly as result)

怠惰な無限の結果を出力することが許可されている場合は、代わりに-1バイトにする₂иことができます。オンラインで試してください。Þ

1 GalenIvanov Oct 06 2020 at 19:56

ファクター、201バイト

: s ( n d -- s ) dupd 2dup nth length 2 - pick 1 - 1 max dup [ / ] dip swap
<repetition> dup first [ + ] accumulate [ 1 + >integer ] map nip [ nth ] dip
dupd swap nths [ 1 head ] dip append swap head ;

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

1 AZTECCO Oct 07 2020 at 01:09

Japt、25バイト

gV
_uUÊ-2 Ä}hV[0UÊÉ] Í®gU

それを試してみてください

Input :
 U = dictionary
 V = number

gV          - get element from dictionary

hV[0UÊÉ]    - build a list of V indexes,
              starting with [0 , last idx] 
              and calling the following function on last element to generate next items.
_uUÊ-2 Ä}   - returns number modulo( literal length -2) + 1

Example : 11 - eleven
[0,5] -> 5%4+1
[0,5,2] -> 2%4+1
[0,5,2,3]
[0,5,2,3,4,1,..]
     
ͮgU         - sort and maps to literal

-P flag to join result