广义五十八序列


17

改编自这个五十八谜

背景

检查以下无限序列:

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

假设序列是1索引的。i序列中的th号确定3ith 之前2和之后的2s 之前有多少个s。因此,由于序列以a开头,3因此序列必须开始,3 3 3 2并且由于序列的开头有3个3s,因此子序列3 3 3 2必须重复自身3次。之后,您会到达,3 3 2因为序列中的第四个数字是2

FiveThirtyEight谜语要求限制三分之二的比率(在这里我不会破坏),但是您也可以询问index之后的累积比率i。例如在比i=4就是3/1 = 3,在i=15它的11/4 = 2.75

让我们得到一般

给定数字nk我们可以创建一个类似的序列,n就像描述的原始序列一样,索引处的数字i确定nik一个之前和之后的第一个之后出现多少个ks。

例子:

n=2, k=5 给出顺序 2 2 5 2 2 5 2 2 2 2 2 5 2 2 5 ...

n=3, k=03 3 3 0 3 3 3 0 3 3 3 0 0 3 3 3 0 ...

n=1, k=31 3 1 1 1 3 1 3 1 3 1 3 1 1 1 3 1 ...

挑战

编写一个函数/程序,并执行以下操作。作为输入:

  • 一个正整数 n
  • 非负整数 k ≠ n
  • 一个正整数 i > n

前两个输入nk如上所述确定一个序列,并且i是一个索引。我在示例中使用1索引,但是您可以自由使用0或1索引。如果索引为0,则对的限制ii ≥ n

使用这三个数字输出时,序列中ns与ks 的比值直到并包括索引处的数字i。输出的格式可以是精度至少为5位的十进制值,也可以是诸如3524/837或的精确值3524:837

十进制形式的最后一位数字可以四舍五入。允许尾随零和空格。

在任何一个字符串形式中,两个数字都需要进行归一化,以便它们互质。例如,如果该比例为22/4,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

这是每种语言的代码高尔夫球,因此每种语言中最短的代码是赢家。



我建议允许使用一对整数作为比率,需要答题者将数字分开,/否则:只会给挑战增加不必要的复杂性。
大公埃里克(Erik the Outgolfer)

@EriktheOutgolfer也允许使用十进制数字
dylnan

标准浮点数对于十进制输出是否足够精确?
恢复莫妮卡– notmaynard

@iamnotmaynard我对浮点格式不严格,是的,我认为是这样
dylnan

Answers:


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 3中94 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个字节: Colera Su
  • 从92字节减少到89字节: dylnan
  • 从89字节减少到87字节:ovs

不是.count(n)吗?
Colera Su

@ColeraSu谢谢。固定,不知道我怎么想念它。
尼尔,


@ColeraSu谢谢,更新。我将尝试开始使用exec。太酷了。
尼尔


4

果冻,22字节

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

在线尝试!

完整程序。带参数nki

有一个错误使此请求不必要地延长了1个字节。


使用了一些技巧-很好。想知道该错误的正确修复实际上应该是...
Jonathan Allan

@JonathanAllan令我震惊的是这条线,尽管不确定为什么要`使其生效。哦,您的答案与我的不同之处是,我忘了实施我用另一种语言找到的高尔夫> _>
越野选手埃里克(Erik the Outgolfer)

4

果冻 25  16 字节

-9个字节〜50%归因于Outgolfer的果冻答案Erik(1. ƙ即使解释器中的错误当前占用一个字节,也要使用新密钥快速; 2.使用映射重复来避免对当前序列进行计数和索引。)去给他点荣誉吧!

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

一个完整的程序采取三个参数: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

在线尝试!

好吧,感谢@Adám从这个答案中删除了56个字节。

这是一个译注Tradfn(TRAD itional ˚F unctio Ñ)取1个输入,它是一个3元素列表。

⎕PP←5不会将其添加到字节数中,因为它仅用于将P rint P精度限制为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.

1

R,88字节

function(n,k,i){s=rep(n,n);for(j in 1:i){s=c(s,k,rep(n,s[j]))};z=sum(s[1:i]==n);z/(i-z)}

在线尝试!


您可以摆脱for循环主体周围的花括号,因为只有一条语句。
朱塞佩

0

迅捷,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

从stdin的三个单独行中获取三个数字。输出精确的比率,例如104/1163

不打高尔夫球

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