月曜日のミニゴルフ#4:JARVIS(Just Another Rather Vast set of Integer Sequences)


22

月曜日のミニゴルフ:毎週月曜日に(願わくば!)投稿された一連の短い質問。
(すみません、また遅くなりました。基本的に昨日も今日もコンピューターから離れていました。)

私たちプログラマー(特にコード愛好家)は、任意の整数シーケンスが大好きです。これらのシーケンス専用のサイト全体があり現在約200,000のエントリがあります。この課題では、これらのシーケンスのさらに別のセットを実装します。

チャレンジ

あなたの課題は、整数Nを取り込んで10を基数とする整数のシーケンスを出力するプログラムまたは関数を作成することです。次の各整数は次のように決定されます。

  • 1から始めます。
  • 前の整数の基数10表現の各桁Dに対して:

    • Dが0の場合、現在の整数に1を追加します。
    • そうでなければ、現在の整数にDを掛けます。

詳細

  • 0 < N <2 31と仮定できます。
  • シーケンス内の各整数は、10未満の数に達するまで、入力番号から始めて出力する必要があります。
  • 出力は、配列、またはスペース、コンマ、改行、またはこれらの組み合わせで区切られた文字列です。
  • 末尾のスペースや改行を使用できますが、末尾のコンマは使用できません
  • 先行ゼロはありません。

例1: 77

この例はかなり簡単です。

77 = 1*7*7 = 49
49 = 1*4*9 = 36
36 = 1*3*6 = 18
18 = 1*1*8 = 8

したがって、適切な出力は77 49 36 18 8です。

例2: 90

ここにあります:

90 = 1*9+1 = 10
10 = 1*1+1 = 2

したがって、出力はになります90 10 2

例3: 806

方程式を左から右に読む:

806 = 1*8+1*6 = 54 (((1*8)+1)*6)
 54 = 1*5*4   = 20
 20 = 1*2+1   = 3

出力はでなければなりません806 54 20 3

テストケース

各行の最初の数値は入力であり、完全な行は予想される出力です。

77 49 36 18 8
90 10 2
249 72 14 4
806 54 20 3
1337 63 18 8
9999 6561 180 9
10000 5
8675309 45369 3240 25 10 2
9999999 4782969 217728 1568 240 9
1234567890 362881 2304 28 16 6

参考として、10〜100の適切な次の整数を次に示します。

Current | Next
--------+-----
     10 |  2
     11 |  1
     12 |  2
     13 |  3
     14 |  4
     15 |  5
     16 |  6
     17 |  7
     18 |  8
     19 |  9
     20 |  3
     21 |  2
     22 |  4
     23 |  6
     24 |  8
     25 | 10
     26 | 12
     27 | 14
     28 | 16
     29 | 18
     30 |  4
     31 |  3
     32 |  6
     33 |  9
     34 | 12
     35 | 15
     36 | 18
     37 | 21
     38 | 24
     39 | 27
     40 |  5
     41 |  4
     42 |  8
     43 | 12
     44 | 16
     45 | 20
     46 | 24
     47 | 28
     48 | 32
     49 | 36
     50 |  6
     51 |  5
     52 | 10
     53 | 15
     54 | 20
     55 | 25
     56 | 30
     57 | 35
     58 | 40
     59 | 45
     60 |  7
     61 |  6
     62 | 12
     63 | 18
     64 | 24
     65 | 30
     66 | 36
     67 | 42
     68 | 48
     69 | 54
     70 |  8
     71 |  7
     72 | 14
     73 | 21
     74 | 28
     75 | 35
     76 | 42
     77 | 49
     78 | 56
     79 | 63
     80 |  9
     81 |  8
     82 | 16
     83 | 24
     84 | 32
     85 | 40
     86 | 48
     87 | 56
     88 | 64
     89 | 72
     90 | 10
     91 |  9
     92 | 18
     93 | 27
     94 | 36
     95 | 45
     96 | 54
     97 | 63
     98 | 72
     99 | 81
    100 |  3

このリストは、ここで 10000に展開されています

得点

これはであるため、バイト単位の最短有効コードが優先されます。Tiebreakerは、最初に最終バイトカウントに達した送信に進みます。勝者は10月19日の次の月曜日に選ばれます。

編集:勝者の@isaacgにおめでとう。Pythを14バイト使用しました

回答:


10

Pyth、15 14バイト

.uu|*GHhGjNT1Q

デニスのおかげで1バイト

テストスイート

この課題は、Pythのリデュース機能のために作成されたように感じます。1つは数字を減らし、もう1つは値の変化が止まるまで減らします。


2
|*GHhGを1バイト節約します?H*GHhG
デニス

4

PowerShell、92 91 90 88 87バイト

($n=$args);while($n-gt9){$x=1;[char[]]"$n"|%{$x=if($y=$_-48){$x*$y}else{$x+1}};($n=$x)}

1
これは非常に滑らかで、(...)自動出力を活用するために使用します...将来、それを覚えておく必要があります。
AdmBorkBork

3

ピップ28 25 23バイト

Tt>Pa{Y1FdaYy*d|y+1a:y}

コマンドライン引数として数値を受け取り、連続する行にシーケンスを出力します。

説明:

                         a is cmdline arg; t is 10 (implicit)
Tt>Pa{                }  Loop till a<10, printing it each time the test is made:
      Y1                   Yank 1 into variable y
        Fda                For each digit d in a:
           Yy*d|y+1          If y*d is truthy (nonzero), yank it; otherwise, yank y+1
                   a:y     Assign value of y back to a

P数リビジョン前にステートメントから演算子に変更できてうれしいです。Paa値を評価するが出力する式なので、をa使用して10未満かどうかを印刷して同時にテストできますt>Pa


3

CJam、26 25 24 22バイト

riA,{_pAb{_2$*@)?}*j}j

または

ri{_pAb{_2$*@)?}*_9>}g

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

使い方

どちらのプログラムも基本的に同じことを行います。最初は再帰的アプローチであり、2番目は反復アプローチです。最初のものについて説明しますが、これはもっと興味深いと思います。

ri                     Read an integer from STDIN and push it on the stack.
  A,{               }j Initialize a memoized, recursive function j with the array
                       [0 ... 9] as "base cases". If j is called on an integer
                       below 10, it returns the element at that index of the base
                       cases (which is same integer) and does not execute the code
                       block. The base case array is filled with new values as j is
                       called again and again, but we do not use this feature.
     _p                Copy and print the integer on the stack.
       Ab              Convert it into its base-10 digits.
         {       }*    Fold; push the first digit, for each remaining digit:
          _2$*         Multiply copies of the accumulator and the current digit.
              @)       Increment the original accumulator.
                ?      Select the product if the digit is non-zero, else the sum.
                   j   Call j on the result.
                       If the result was less than 10, it is retrieved from the
                       base cases and pushed on the stack. CJam prints it before
                       exiting the program.

2

Minkolang 0.752の 46バイト

ndN((d25*%1R25*:)r11(x2~gd4&x1+!*I1-)dNd9`,?).

Woohooネストループ!

説明

ndN     Takes integer input and outputs it
(       Starts overall loop

 (        Starts loop that separates top of stack into digits
  d25*%   Modulus by 10
  1R      Rotates stack 1 unit to the right
  25*:    Divides by 10
 )

 r11   Reverses stack and pushes two 1s; 1 for the dump and 1 for the multiply
 (     Starts the multiply/add loop
  x    Dumps top value

      -This top-of-stack dump is because
       while loops end when the stack is
       empty or the top of stack is 0. The
       top of stack is *not* popped for
       this conditional check, so if the loop
       continues, I need to dump the left-over
       from the previous iteration.

  2~gd    Gets next-to-last stack value and duplicates for the conditional
  4&      Jumps 4 spaces if top of stack is positive
   x1+!   Dumps the 0 leftover, adds 1 to top of stack, and jumps the multiply
   *      Multiplies the top two elements of stack
  I1-     Pushes length of stack - 1
 )        Exits the loop if top of stack is 0 (i.e., len(stack)=1)
 dN       Outputs as integer
 d9`,?    Jumps out of the loop if top of stack <=9
)
.    Stop.


2

Python 3、74、76バイト

ここには既にPythonの回答があり、reduceがありました。そこで、それなしでやりたいと思いました。intで呼び出す必要があります。

def j(n,m=1):
 print(n)
 if n>9:
  for d in str(n):m=m*int(d)or m+1
  j(m)

2

Python、85 80バイト

def g(n):y=reduce(lambda i,x:i*int(x)or i+1,`n`,1);return[n]+(g(y)if n>9else[])

これにより、最初の値だけではなく、リスト全体が適切に出力されるようになりました。


名前のないラムダを使用して、つまり省略して、2バイトを保存できますg=
アレックスA.

1

K5、24バイト

(1{(x*y;x+1)@~y}/.:'$:)\

固定点まで反復しながらアイテムのリストを収集することは、まさにスキャンオペレーター\が行うことです。各反復で、最初に数値を文字列にキャストしてから、各文字(.:'$:)を評価し、数値をその数字に分解します。次に/、1から始まりlambdaを使用してreduction()を実行し{(x*y;x+1)@~y}ます。この場合x、減少値でありy、シーケンスの各連続項です。

動作中:

  f: (1{(x*y;x+1)@~y}/.:'$:)\

  f'77 90 249 806 1337 9999 10000 8685309 9999999 1234567890
(77 49 36 18 8
 90 10 2
 249 72 14 4
 806 54 20 3
 1337 63 18 8
 9999 6561 180 9
 10000 5
 8685309 51849 1440 17 7
 9999999 4782969 217728 1568 240 9
 1234567890 362881 2304 28 16 6)

1

ジュリア、93 89 88 86 83 77バイト

f(n)=(println(n);if(d=n>9)for i=reverse(digits(n)) i<1?d+=1:d*=i end;f(d)end)

これによりf、シーケンス要素を別々の行に出力する再帰関数が作成されます。

ゴルフをしていない:

function f(n::Int)
    println(n)
    if (d = n > 9)
        for i in reverse(digits(n))
            i < 1 ? d += 1 : d *= i
        end
        f(d)
    end
end

オンラインで試す

デニスのおかげで6バイト節約されました!


n>92番目の例に従う必要があります。また、f(n)=(println(n);if(d=n>9)for i=reverse(digits(n)) i<1?d+=1:d*=i end;f(d)end)少し短くなります。
デニス

@Dennis素晴らしいアイデア、ありがとう!
アレックスA.

1

Ruby 83、72バイト

関数として宣言された

def f(d)loop{p d;break if d<10;d=d.to_s.bytes.inject(1){|r,i|i>48?r*(i-48):r+1}}end

私は使用しようとしましたEnumerator.newが、非常に多くのバイトを使用します:-(

再帰を使用して改善

def f(d)p d;f(d.to_s.bytes.inject(1){|r,i|i>48?r*(i-48):r+1})if d>10 end

0

C#およびLINQ、165 146バイト

void j(int a){r.Add(a);var l=a.ToString().Select(d=>int.Parse(d.ToString()));int n=1;foreach(int i in l)n=i==0?n+1:n*i;if(n>9)j(n);else r.Add(n);}

j(jarvisの場合)は再帰関数です。rは結果のintのリストです。

LINQPADでテスト済み:

void Main()
{
    j(806);
    r.Dump();
}
List<int> r = new List<int>();

void j(int a){r.Add(a);var l=a.ToString().Select(d=>int.Parse(d.ToString()));int n=1;foreach(int i in l)n=i==0?n+1:n*i;if(n>9)j(n);else r.Add(n);}

あなたは演算子を囲むスペースを除去することによって、いくつかのバイトを保存することができ、例えば、int n = 1可能int n=1など、
アレックス・A.

良いキャッチ@AlexA。146に減少しました。
noisyass2

a.tostring()の代わりにa + ""を実行することで少し節約することもできます:)
Alex Carlsen

0

Haskell、71バイト

x!'0'=x+1
x!c=x*read[c]
g x|h>9=x:g h|1<2=[x,h]where h=foldl(!)1$show x

使用法:g 8675309-> [8675309,45369,3240,25,10,2]



0

Java 8、148バイト

String f(int j){String s="";Function r=i->(""+i).chars().map(x->x-48).reduce(1,(x,y)->y>0?x*y:x+1);while((j=(int)r.apply(j))>9)s+=j+" ";return s+j;}

フォーマット済み

String f(int j) {
    String s = "";
    Function r = i -> ("" + i).chars().map(x -> x - 48).reduce(1, (x, y) -> y>0 ? x*y : x+1);
    while ((j = (int)r.apply(j)) > 9) s += j+" ";
    return s+j;
}

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