降順の数字列


16

前書き

例として、番号を見てみましょう7。次に、これを複製し、間に7つのスペースを配置します。これを取得します。

7_______7

その後、スペースがなくなるまで数を減らします。数7については次のようになります。

7_______7    
 6543210

次に、2つをマージするだけです。

7_______7    
 6543210  becomes

765432107

これは、N = 7の場合に出力されます。

簡単そうですね。N = 12を取ります。再び2つの数字の間に12個のスペースを挿入します。

12____________12

次に、減分を開始します。

12____________12
  111098765432

そして、これは最終的に私たちに与えます:

1211109876543212

ご覧のとおり、下降部分は0 ではなく 2で終わります

仕事

1より大きい整数を指定すると、上記のように降順が出力されます。

テストケース

Input   Output

2       2102
3       32103
4       432104
5       5432105
6       65432106
7       765432107
8       8765432108
9       98765432109
10      10987654321010
11      111098765432111
12      1211109876543212
13      13121110987654313
14      141312111098765414
15      1514131211109876515
20      201918171615141312111020
99      9998979695949392919089888786858483828180797877767574737271706968676665646362616059585756555453525150499
100     1009998979695949392919089888786858483828180797877767574737271706968676665646362616059585756555453525150100

これはであるため、バイト数が最も少ない提出が勝ちです!


内部空間は整数で埋める必要がありますか、必要に応じて数値を切り刻む必要がありますか?それに関するテストケースはありません(たとえば99)
-edc65

@ edc65必要に応じて、数字を切り刻む必要があります。テストケースとして99を追加しました。
アドナン

回答:


8

CJam、11 10バイト

q4*~,W%s<\

オンラインでお試しください。入力に末尾の改行があると仮定します。(バイトを保存してくれた@ jimmy23013に感謝します。)

説明

各行の最後には、スタックがその時点でどのように見えるかが示されています(4例として使用)。

q4*  e# Push input x 4 times, separated by newlines. ["4\n4\n4\n4\n"]
~    e# Evaluate, separating the 4's and converting them to numbers. [4 4 4 4]
,W%  e# Take the range of x and reverse it. [4 4 4 [3 2 1 0]]
s<   e# Cast to string and take the first x characters. [4 4 "3210"]
\    e# Swap the top two to get the final result. [4 "3210" 4]

9

ジュリア、30バイト

n->"$n"join(n-1:-1:0)[1:n]"$n"

これは、整数を受け入れて文字列を返す匿名関数です。呼び出すには、変数に割り当てます。

n -1から0 までの降順を構築して結合し、結果の文字列から最初のn文字を取得します。入力として文字列としてこれを先頭に追加します。

すべてのテストケースをオンラインで検証する


5

Haskell、44バイト

s=show
f n=s n++take n(s=<<[n-1,n-2..])++s n

使用例:f 14-> "141312111098765414"


5

JavaScript(ES6)、55 52バイト

n=>n+[...Array(m=n)].map(_=>--m).join``.slice(0,n)+n

編集:@WashingtonGuedesのおかげで3バイト保存されました。


@WashingtonGuedes Bah、私は使用することはないようです.keys()
ニール

.keys()のようなもの.reduceです。仕事に適したツールですが、その特定のケースでより良い結果が得られるものを常に見つけます
-edc65


3

Pyth、11バイト

++Q<jk_UQQQ

2つの代替バージョンは、いずれも11バイトです(ため息):

s[Q<jk_UQQQ
pQp<jk_UQQQ
  Q           the input
       UQ     [0, 1, ..., input-2, input-1]
      _       reverse
    jk        join on empty string
   <     Q    first (input) characters
          Q   the input again
++            concatenate everything so it prints on one line

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



3

ゼリー、10バイト

Ȯ’r0DFḣ³Ḍ³

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

使い方

Ȯ’r0DFḣ³Ḍ³  Main link. Input: n

Ȯ           Print n.
 ’          Decrement to yield n - 1.
  r0        Create a range from n - 1 to 0.
    D       Convert each integer to base 10 (array of decimal digits).
     F      Flatten the resulting array.
      ḣ³    Keep the first n elements.
        Ḍ   Convert from base 10 to integer.
         ³  Print the integer and set the return value to n.
            (implicit) Print the return value.


2

Vitsy、35バイト

Vitsyは数字から文字列を作成する方法を認識していないため、2行目の小数点以下の桁数を見つける方法を実装しました。

V0VVNHVv[XDN1mv$-DvD);]VN
1a/+aL_1+

説明:

V0VVNHVv[XDN1mv$-DvD);]VN
V                          Save the input as a global final variable.
 0V                        Push 0, push input.
   VN                      Output the input.
     H                     Push the range 0...intput.
      Vv                   Push the input, then save it as a temp variable.
        [             ]    Do the stuff in brackets infinitely or until exited.
         X                 Remove the top item of the stack.
          DN               Duplicate, then pop as output.
            1m             Calls the first line index, retrieving length.
              v            Pop the temp var and push it to the stack.
               $           Switch the top two items of the stack. 
                -          Subtract them.
                 Dv        Duplicate, then pop one as a temp var.
                   D);     If it's zero, exit the loop.
                       VN  Output the global var.

1a/+aL_1+
1a/+       Add .1. This makes sure we don't throw errors on input 0.
    a      Push ten.
     L     Pop the top item as n, push the log base n of second to top.
      _    Make it an int.
       1+  Add 1.

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

LOLの詳細モード:

save top as permanent variable;
push 0;
save top as permanent variable;
save top as permanent variable;
output top as number;
push all ints between second to top and top;
save top as permanent variable;
save top as temporary variable;
begin recursive area;
remove top;
duplicate top item;
output top as number;
push 1;
goto top method;
save top as temporary variable;
switch the top two items;
subtract top two;
duplicate top item;
save top as temporary variable;
duplicate top item;
if (int) top is not 0;
generic exit;
end recursive area;
save top as permanent variable;
output top as number;
:push 1;
push 10;
divide top two;
add top two;
push 10;
push ln(top);
replace top with int(top);
push 1;
add top two;

の定義では冗長モードが間違っているように見えますがL、今では修正されています(ただし、質問は更新されません)。
アディソンクランプ

好奇心が強いのですが、プログラムの最後でメソッドが実行されないようにするにはどうすればよいですか?改行文字はプログラムを返す/終了するためのシグナルですか?
LegionMammal978

@ LegionMammal978すべてのVitsyプログラムの最初の行が「メイン」メソッドであり、他のすべての行がpublic static voidメソッドであると想像してください。メインは、プログラムが終了するとプログラムを終了します。これを行う方法については、命令はtype ArrayList<ArrayList<String[]>>に保持されString[]ます。各行はです。すべてのメソッドは、ファイルのロード方法によって改行で分割されるため、メインメソッドは他のすべてのメソッドから分離されます。
アディソンクランプ

これが、3つのレベルが必要な理由を説明しています。だからStringsが指示され、String[]sが方法(最初のものはmainメソッド)であり、そしてArrayList<String[]>、正しいのは、クラス(最初のものはメインクラスである)ですか?
LegionMammal978

@ LegionMammal978それはすべて正しいです。:)
アディソンクランプ

2

純粋なバッシュ、49

eval printf -va %s {$[$1-1]..0}
echo $1${a::$1}$1

または:

Bash + coreutils、48

echo $1$(seq $[$1-1] -1 0|tr -d \\n|head -c$1)$1

これらが範囲を適切に評価しているかどうかはわかりません。両方をテストすると、範囲の半分のみが印刷されます。すなわち、$ 1 = 90の場合、範囲は45までです。私の努力は「for i in $(eval echo {$ 1..0}); do echo -n $ i; done; echo $ 1」
rcjohnson

@rcjohnsonこれは必須の動作だと思います。N = 90の出力はどうなると思いますか?
デジタル外傷

@rcjohnsonたとえばN = 12の場合、出力は12、次に最初の12文字11..0(または111098765432)、最後に12
Digital Trauma

説明を読み直したところ、あなたは正しいことがわかりました。問題は、整数ではなく「スペース」を示しています。
rcjohnson

@rcjohnsonはい、「スペース」の部分は中間ステップにのみ適用されると思います。最終的な出力は、単なる数字の文字列でなければなりません。
デジタル外傷

2

網膜、63バイト

.+
$0,y$0$*y$0$*x
x
$'_
(x)*_
$#1
+`(y+)y(.)
$2$1
,(\d+).*
$1$`

ゴルフの余地はまだまだあります...

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


ええと、前のトークンが数字ではないリテラルである場合(同様$0に)を$0$*オプションにすることを検討していますy...これを見て、実際にそれを実装するかもしれません。
マーティンエンダー

@MartinBüttnerこれは新しい機能だと思っていましたが、実際にはそうではありませんでした。:)
randomra

いいえ、現在は置換の開始時にのみ機能します。そうは言っても、最初と最後の番号の役割を切り替えて、それを利用することはできますか?
マーティンエンダー

2

MATL、15バイト

VG:qPVXvG:)GVhh

編集(2016年5月20日)言語の最近の変更により、リンク内のコードはのXz代わりにを使用Xvします。

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

V                 % input n. Convert to string
 G:               % range [1,2,...,n]
   qP             % convert into [n-1,n-2,...,0]
     VXv          % convert to string, no spaces
        G:)       % take first n characters only
           GV     % push input as a string, again
             hh   % concat horizontally twice    


1

Ruby、41バイト

->n{[n]*2*(r=0...n).to_a.reverse.join[r]}

1

天の川1.6.527の 25バイト

I'::%{K£BCH=}<ΩHG<+<;+!

説明

I                        ` empty the stack
 '::                     ` push 3 copies of the input
    %{K£BCH=}            ` dump digits of reversed range(n) as strings [n-1...0]
             <ΩHG<+<;+   ` select the first nth digits and pad them with n
                      !  ` output

使用法

$ ./mw <path-to-code> -i <input-integer>

天の川はどのエンコードを使用していますか?
アドナン

Uhhh .. UTF-8、ハハだと思います。@AandN
ザックゲイツ

私が得たこのエラーをこれを実行しようとしているとき:(Pはい、私は、Windowsのscumbagです)。これを貼り付けI'::%{K£BCH=}<OHG<+<;+!ました。UTF-8エンコードファイルに貼り付けましたが、機能しません。
アドナン

ここに私が使用しているファイルへのリンクがあります。@AandN
ザックゲイツ

1

Perl 6バイト

{$_~([R~] ^$_).substr(0,$_)~$_}
{
  $_ # input
  ~  # string concatenated with
  ([R~] ^$_)    # all numbers up to and excluding the input concatenated in reverse
  .substr(0,$_) # but use only up to the input number of characters
  ~
  $_
}

使用法:

for 2,3,7,12,100 {
  say {$_~([R~] ^$_).substr(0,$_)~$_}( $_ )
}
2102
32103
765432107
1211109876543212
1009998979695949392919089888786858483828180797877767574737271706968676665646362616059585756555453525150100

1

Perl、43 + 2 = 45バイト

私は使用reverseしなかったことと、どちらも幸せではありませんsubstr

"@{[1-$_..0]}"=~s.\D..gr=~/.{$_}/;$_.=$&.$_

-plフラグが必要です。

$ perl -ple'"@{[1-$_..0]}"=~s.\D..gr=~/.{$_}/;$_.=$&.$_' <<< 12
1211109876543212

使い方:

                                            # '-p' read first line into `$_` and
                                            # auto print at the end
"@{[1-$_..0]}"                              # Create a list from -1-n..0 and
                                            # join it on space. This becomes:
                                            #   "-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0"
              =~s.\D..gr                    # Remove all but digits:
                                            #   "11109876543210"
                        =~/.{$_}/;          # Match the n first characters from
                                            # the generated string
                                  $_.=$&.$_ # Append the match and the input

1

C、130の 125バイト

#define p(x) printf("%i",x);
i,y,h;f(x){for(i=y=x;(i-=h)>=0;){p(y--)h=floor(log10(y))+1;}if(i+=h)p(h=floor(y/pow(10,i)))p(x)}

非ゴルフバージョン(説明付き):

#define p(x) printf("%i",x);     // alias to print an integer
i,y,h;                           // helper variables
f(x){                            // function takes an integer x as arg
    for(i=y=x;(i-=h)>=0;){       // i -> the remaining space
                                 // y -> the current descending number
        p(y--)                   // print y (at first y==x)
        h=floor(log10(y))+1;     // h -> the number of digits in y-1
    }                            // do it until there is no more empty space
    if(i+=h)                     // if needs to chop the last number
        p(h=floor(y/pow(10,i)))  // chop and print (implicitly cast of double to int)
    p(x)                         // print x at the end
}                                // end function

doubleからintへの暗黙的なキャストh=floor(...)により、#define p(x)5バイトの節約が可能になりました。

イデオンでテストします。


1

R、67バイト(関数として)

# usage example : f(7)
f=function(i)cat(i,substr(paste((i-1):0,collapse=''),1,i),i,sep='')

R、63バイト(STDINからの入力)

i=scan();cat(i,substr(paste((i-1):0,collapse=''),1,i),i,sep='')

1

Brainfuck、265バイト

これは、10未満の数値でのみ機能します

ここでゴルフバージョンをお試しください:

>,------------------------------------------------[->+>+<<]>>[-<<+>>]<[[->+>+<<]>>[-<<+>>]<-]<[<]>[>[>]>+<<[<]>-]>[++++++++++++++++++++++++++++++++++++++++++++++++.>]++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++.

非ゴルフ。ここで試してみてください

>
,
---------- Convert to base 10
----------
----------
----------
-------- 


[->+>+<<]>>[-<<+>>]<

Fill up the grid
[
[->+>+<<]>>[-<<+>>] //duplicate number like [5][0] -> [5][5]
<-
]

<[<]> Go to cell 1
[

>[>] Scan for zero
> Move one more
+ Add one
<< Move two back
[<] Scan for zero
> Move one forward
- Subtract One
]

> Move one forward into actual numbers
[
++++++++++ Convert to ascii
++++++++++
++++++++++
++++++++++
++++++++
.
>
]
++++++++++ Convert to ascii
++++++++++
++++++++++
++++++++++
++++++++
.
>
++++++++++ Convert to ascii
++++++++++
++++++++++
++++++++++
++++++++
.

,>>++++++[<++++++++>-]<[-<->]<これにより、短いコード長で48を差し引くことができます
Leaky Nun


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