一般化されたFiveThirtyEightシーケンス


17

このFiveThirtyEightのなぞなぞから適応。

バックグラウンド

次の無限シーケンスを調べます。

3 3 3 2 3 3 3 2 3 3 3 2 3 3 2 3 3 3 2 ...

シーケンスに1インデックスが付けられているとします。iシーケンスの番目の数はどのように多くの決定3の前にあるS i番目2と以前の次2秒。したがって、シーケンスはで始まるため、シーケンスを開始する3必要が3 3 3 2あり3、シーケンスの先頭に3 があるため、サブシーケンスは3 3 3 23回繰り返す必要があります。3 3 2シーケンスの4番目の数字はであるため、その後に到達します2

FiveThirtyEightのなぞなぞは、3対2の比率の制限を求めます(ここでは損しません)が、indexの後に累積比率が何であるかを尋ねることもできますi。たとえば、の比率はでi=4あり3/1 = 3、でi=15ある11/4 = 2.75

一般的になりましょう

数字を考えるnk、私たちはで始まる類似の配列にすることができn、元の配列は、インデックスの数が記述と同じようにi多くの方法を決定するnのは、前に現れi番目kと以前の次k秒。

例:

n=2, k=5 シーケンスを与える 2 2 5 2 2 5 2 2 2 2 2 5 2 2 5 ...

n=3, k=0 与える 3 3 3 0 3 3 3 0 3 3 3 0 0 3 3 3 0 ...

n=1, k=3 与える 1 3 1 1 1 3 1 3 1 3 1 3 1 1 1 3 1 ...

チャレンジ

関数/プログラムを作成し、それを使用して以下を実行します。入力として取る:

  • 正の整数 n
  • 非負の整数 k ≠ n
  • 正の整数 i > n

最初の2つの入力は、上記のシーケンスnk決定しi、インデックスです。例では1インデックスを使用していますが、0インデックスまたは1インデックスを使用する自由があります。インデックスが0の場合、制限はiですi ≥ n

3つの数字で、インデックスの数字までのシーケンスnのsとksの比率を出力しますi。出力の形式は、少なくとも5桁の精度を持つ10進値、またはのような比率としての正確な値の3524/837いずれか3524:837です。

10進数形式では、最後の桁を好きなように丸めることができます。末尾のゼロと空白は許可されます。

どちらの文字列形式でも、2つの数値は互いに素になるように正規化する必要があります。例えば、場合比が4分の22であった、11/2そして11:2許容されなく22/4はありません。

n   k   i      output
2   4   15     2.75     or   11/4
6   0   666    5.1101   or   557:109
50  89  64     63       or   63:1
3   2   1000   2.7453   or   733/267
9   12  345    9.4545   or   104/11

これは言語ごとのコードゴルフであるため、各言語で最も短いコードが勝者です。



整数のペアを比率として許可することをお勧めします。回答者が数字を分離する/:、チャレンジに不要な複雑さを追加する必要があります。
エリックアウトゴルファー

@EriktheOutgolfer進数があまりにも許可されている
dylnan

標準の浮動小数点数は10進出力に十分正確ですか?
モニカの復職-notmaynard

@iamnotmaynard私はそうはい、私はそうは思わfloat形式について厳密ではないよ
dylnan

回答:


5

、16バイト

¤/#ωȯ↑⁰J¹`C∞²N¹²

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

テストケースと同じ順序で入力を受け取ります。有理数を出力します。これには上付き文字が多すぎるように感じますが、それらを取り除く方法がわかりません...

説明

¤/#ωȯ↑⁰J¹`C∞²N¹²  Inputs are n, k, i.
             N    Starting with the natural numbers [1,2,3..
   ωȯ             do this until a fixed point is reached:
                    Argument is a list s.
           ∞²       Take the infinite list [n,n,n..
         `C         and split it to the lengths in s.
       J¹           Join the resulting blocks with k.
     ↑⁰             Take the first i elements.
                  Call the result x.
¤             ¹²  For each of n and k,
  #               count their number of occurrences in x
 /                and perform exact division on the results.

4

Pythonの394の 92 89 87バイト

def g(n,k,i):o,t=[n],0;exec('o+=[n]*o[t]+[k];t+=1;'*i);return-1-i/(o[1:i+1].count(n)-i)

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

クレジット

  • :94〜92バイトから減少コレラ蘇
  • 92バイトから89バイトに削減:dylnan
  • 89バイトから87バイトに削減:ovs

そうではありません.count(n)か?
コレラSu

@ColeraSuありがとう。どうやってそれを逃したかわからない、修正。
ニール


@ColeraSuありがとう、更新しました。execの使用を開始しようとします。それはいいね。
ニール


4

ゼリー、22バイト

³ẋЀ;€⁴Ẏḣ⁵
ẋ`;ÇÐLLƙ`÷/

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

完全なプログラム。引数を取りnki

これが不必要に1バイト長くする必要があるバグがあります。


いくつかのトリックを使用しました-いいですね。バグの正しい修正が本当に...どうあるべきかについて、疑問に思う
ジョナサン・アラン

@JonathanAllan私を驚かせたのはこの行ですが、なぜ置くのがうまくいくのか分かりません`。ああ、どこであなたの答えが異なることは_>>私は、私は別の言語で見つかったゴルフを実装するのを忘れていることである
エリックOutgolfer

4

ゼリー 25  16 バイト

Erik the OutgolferのJellyの回答に起因する-9バイト〜50%(1. ƙ現在1バイトのコストがかかっているインタープリターのバグがある場合でも、新しいキーをすばやく使用します。 。)彼にクレジットを与えてください!

³ẋЀj⁴ṁ⁵µÐLLƙ`÷/

3つの引数を取り、完全なプログラムは:nki結果を印刷しています。

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

どうやって?

³ẋЀj⁴ṁ⁵µÐLLƙ`÷/ - Main link
        µ        - monadic chain separation
         ÐL      - apply the monadic chain to the left repeatedly until no change occurs:
³                -   program's 1st argument, n
  Ѐ             -   map across the current sequence (initially just n)
 ẋ               -     repeat (the first run give triangle of n i.e. [[n],[n,n],...,[n]*n]
     ⁴           -     program's 2nd argument, k
    j            -     join
       ⁵         -     program's 3rd argument, i
      ṁ          -     mould like (repeat the list to fill, or cut it, to length i)
            ƙ    - keyed application, map over groups of identical items:
             `   - (this has an arity of 2, make it monadic by repeating the argument)
           L     -   length -> [numberOfNs, numberOfKs]
               / - reduce with:
              ÷  -   division -> [numberOfNs / numberOfKs]
                 - implicit print (a single item list just prints its content)

入力を実行する例n=2k=3i=30

Start the "loop until no change", ÐL
Note: Initial left argument, n=2, implicitly range-ified by Ѐ to become [1,2]
1. mapped repeat of n: [[2],[2,2]]
          join with k: [2,3,2,2]
         mould like i: [2,3,2,2,2,3,2,2,2,3,2,2,2,3,2,2,2,3,2,2,2,3,2,2,2,3,2,2,2,3]

2. mapped repeat of n: [[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2]]
          join with k: [2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2]
         mould like i: [2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2]
                          ^different to previous result

3. mapped repeat of n: [[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2]]
          join with k: [2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2]
         mould like i: [2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2]
                                  ^different to previous result

4. mapped repeat of n: [[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2]]
          join with k: [2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2]
         mould like i: [2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2]
                                                      ^different to previous result

5. mapped repeat of n: [[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2],[2,2],[2,2,2],[2,2]]
          join with k: [2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2]
         mould like i: [2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,2,3,2,2,3,2,2,3,2,2,3,2]
                       all the same as the previous result; stop loop and yield this.

length applied to equal elements: [length([2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]), length([3,3,3,3,3,3,3,3,3])]
                                = [21,9]
reduce by division              = [21/9] = [2.3333333333333335]
implicit print                  2.3333333333333335


2

APL(Dyalog Unicode)126 70バイト

k n i←⎕
j←⍴v←⍬
:While j<i
v,←k,⍨n⍴⍨{v≢⍬:jvn}j+←1
:End
÷/+/¨n k⍷¨⊂jv

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

さて、この回答から56バイトを削除してくれた@Adámに感謝します。

これはniladic Tradfn(あるトラッド itional F unctio N 3要素のリストである1つの入力を取ります)。

⎕PP←5P rint Pを制限するためにのみ使用されるため、バイトカウントに追加されません。にrecision 5桁。

∇fそして彼らは、コードの一部ではないだから、バイト数にtradfnための唯一の区切り文字を追加していません。

使い方:

k n i←⎕                    Take input (←⎕) for k, n and i.
j←⍴v←⍬                     Assign (←) an empty vector (⍬) to v, then assign its shape (⍴, which is 0) to j.
:While j<i                 while j<i, do:
v,←k,⍨n⍴⍨{v≢⍬:jvn}j+←1  this:
                     j+←1  increment j (+←1)
          {v≢⍬:     }      if (:) v does not match (≢) 
               jv         return the jth element of v (v[j])
                  n       else (⋄) return n
      n⍴⍨                  shape (⍴) n as the result (repeats n result times)
   k,⍨                     append (,⍨) k
v,←                        append to (,←) v
:End                       End while loop
÷/+/¨n k⍷¨⊂jv             then:
           jv             shape (⍴) v as j (truncates v to j elements)
                          enclose the resulting vector
         ¨                 for each element
                          find (returns a boolean vector)
     n k                   n and k (⍷ will return a boolean vector for each)
  +/¨                      cumulative sum of each vector (returns the number of times n and k appear in v)
÷/                         divide them and implicitly return the result.


0

Swift、152バイト

func f(n:Int,k:Int,i:Int){var a=[0];(1...i).map{a+=(0..<(a.count>$0 ?a[$0]:n)).map{_ in n}+[k]};let m=a[1...i].filter{n==$0}.count;print("\(m)/\(i-m)")}

Javaよりも短くなりますか?

説明

func f(n:Int,k:Int,i:Int){
  var a=[0]                                    // Initialize the array (the zero is to
                                               //  define the type of the array and will be
                                               //  ignored by the code)
  (1...i).map{                                 // Repeat i times (more than enough):
    a+=(0..<(a.count>$0 ?a[$0]:n)).map{_ in n} //  Add the right amount of n:s to the array
      +[k]                                     //  Add k to the array
  }                                            // End repeat
  let m=a[1...i].filter{n==$0}.count           // Count the amount of n:s in the first
                                               //  i elements of the array
  print("\(m)/\(i-m)")                         // Print the result
}



0

ゼファー、284バイト

input n as Integer
input k as Integer
input m as Integer
set s to Array(m)
for i from 1 to n
set s[i]to n
next
set s[i]to k
set N to n
set K to 1
for a from 2 to m
for b from 1 to s[a]
inc i
if i<=m
set s[i]to n
inc N
end if
next
inc i
if i<=m
set s[i]to k
inc K
end if
next
print N/K

3つの別々の行でstdinから3つの数値を取得します。104/11またはなどの正確な比率を出力します63ます。

非ゴルフ

input n as Integer
input k as Integer
input maxIndex as Integer

set sequence to Array(maxIndex)
for i from 1 to n
    set sequence[i] to n
next
set sequence[i] to k

set nCount to n
set kCount to 1

for a from 2 to maxIndex
    for b from 1 to sequence[a]
        inc i
        if i <= maxIndex
            set sequence[i] to n
            inc nCount
        end if
    next
    inc i
    if i <= maxIndex
        set sequence[i] to k
        inc kCount
    end if
next

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