部分積を出力する


17

長い乗算、数を乗算した後、あなたが出力それらの部分積を意志この挑戦に、部分積が残されています。

長い乗算は長いため、コードを補正するにはできるだけ短くする必要があります。

34, 53
102, 1700

48, 38 
384, 1440

361, 674
1444, 25270, 216600

0, 0
0

1, 8
8

仕様書

  • 入力/出力は、配列、コンマ区切りの文字列(または数字ではないその他の区切り文字)、リスト、関数の引数など、妥当な形式にすることができます。
  • 部分的な製品は昇順でなければなりません。
  • 部分的な製品がの場合、0出力するかどうかを選択できます。

これはので、バイト単位の最短コードが勝ちです!


数字は文字列にできると仮定していますよね?
ママファンロール

その0,0テストケースはそれをずっと難しくしています。
xnor

予想される結果は12, 102何ですか?ほとんどの答えが返されるようです24, 0, 1200
デニス

@デニス24, 0, 1200は大丈夫です。ポストで指定します
Downgoat

回答:


4

ゼリー、10 バイト

DU×µLR’⁵*×

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

使い方

DU×µLR’⁵*×  Left argument: multiplier -- Right argument: multiplicant

D           Convert the multiplier to base 10 (array of digits).
 U          Reverse the array.
  ×         Multiply each digit by the multiplicant.
   µ        Begin a new, monadic chain. Argument: A(array of products)
    L       Get the length of A.
     R      Turn length l into [1, ..., l].
      ’     Decrement to yield [0, ..., l-1].
       ⁵*   Compute 10**i for each i in that range.
         ×  Hook; multiply the powers of ten by the corresponding elements of A.

3
この言語の名前は、誰もがゼリーを感じるようになるという事実に由来すると推測しています。
geokavel

7

Pyth、12バイト

.e**Qsb^Tk_w

テストスイート

入力改行を区切ります。たとえば

361
674

説明:

.e**Qsb^Tk_w
                Implicit: Q = eval(input()),T = 10
           w    Input the second number as a string.
          _     Reverse it.
.e              Enumerated map, where b is the character and k is the index.
     sb         Convert the character to an int.
   *Q           Multiply by Q.
  *    ^Tk      Multiply by T ^ k. (10 ^ index)

4

JavaScript(ES7)、48バイト

(a,b)=>[...b+""].reverse().map((d,i)=>10**i*a*d)

ES6(56バイト)

(a,b)=>[...b+""].reverse().map((d,i)=>a*d+"0".repeat(i))

説明

部分積の配列を数値として返します。

(a,b)=>
  [...b+""]    // convert the multiplier to an array of digits
  .reverse()   // reverse the digits of the multiplier so the output is in the right order
  .map((d,i)=> // for each digit d of the multiplier
    10**i      // get the power of ten of the digit
      *a*d     // raise the product of the digit to it
  )

テスト

テストではMath.powなく**、標準のブラウザーで動作するようにします。



3

APL、21バイト

{⍺×x×10*1-⍨⍳≢x←⊖⍎¨⍕⍵}

これは、左右の整数を受け入れて配列を返すダイアディック関数です。呼び出すには、変数に割り当てます。

説明:

             x←⊖⍎¨⍕⍵} ⍝ Define x to be the reversed digits of the right input
     10*1-⍨⍳≢         ⍝ Generate all 10^(1-i) for i from 1 to the number of digits
{⍺×x×                 ⍝ Multiply the right input by the digits and the powers of 10

1
⍎¨⍕かなり賢いです。
デニス

2

05AB1E、15バイト

コード:

VDgUSXFTNmY**=\

説明:

VDgUSXFTNmY**=\

V                 # Assign the input to Y
 D                # Duplicate of input, because the stack is empty
  g               # Pushes the length of the last item
   U              # Assign the length to X
    S             # Split the last item
     X            # Pushes X (length of the last item)
      F           # Creates a for loop: for N in range(0, X)
       TNm        # Pushes 10 ^ N
          Y       # Pushes Y (first input)
           *      # Multiplies the last two items
            *     # Multiplies the last two items
             =    # Output the last item
              \   # Discard the last item

2

Pyth、26バイト

DcGHKjHTFNJlK*G*@Kt-JN^TN

これは、引数をc受け入れるように関数を定義し、関数2は部分積を出力します。

DcGHKjHTFNJlK*G*@Kt-JN^TN
DCGH                      Define function c(G, H)
    KjHT                  Set K to the list of digits converting H to base 10
        FNJlK             Set J to the length of K and loop with variable N
                          (Implicit: print)
             *G*@Kt-JN    Calculates the partial product
                      ^TN Raising it to the appropriate power of 10

1

MATL、18バイト

ij48-tn:1-P10w^**P

コンパイラ(5.1.0)は、 MATLABでとオクターブで動作します。

各番号は別々の行に入力されます。

>> matl ij48-tn:1-P10w^**P
> 361
> 674
1444  25270 216600

説明

i           % input first number (say 361)
j           % input second number, interpreted as a string (say '674')
48-         % subtract '0' to obtain vector of figures (gives [6 7 4])
tn:1-P      % vector [n-1, ... 1, 0] where n is the number of figures (gives [2 1 0])
10w^        % 10 raised to that, element-wise (gives [100 10 1])
*           % multiply, element-wise (gives [600 70 4])
*           % multiply (gives 361*[600 70 4], or [216600 25270 1444])
P           % flip vector ([1444 25270 216600]). Implicitly display

1

Haskell、60 57 54バイト

g x=zipWith(\b->(x*10^b*).read.pure)[0..].reverse.show

.show2番目の数字を文字列として取得できる場合は、5バイト少なく(ドロップします)。

使用例:g 361 674-> [1444,25270,216600]

ywith の逆のすべての数字を乗算x10^i、whereでスケーリングしi = 0,1,2,...ます。

編集:@Maurisに3バイトありがとう!


あなたもすることができます(\b->(x*10^b*).read.pure)
リン

@モーリス:いいですね。どうもありがとう!
ニミ

1

ジュリア、50 49バイト

f(a,b)=[a*d*10^~-i for(i,d)=enumerate(digits(b))]

これは、2つの整数を受け入れ、整数配列を返す関数です。

このdigits関数は、入力整数の数字の配列を逆順で返します。インデックスを取得し、値のペアを使用enumerateして部分積を計算します。最初の入力に数字の10を掛け、数字のインデックス-1の累乗を掛けます。

デニスのおかげでバイトを節約できました!



1

CJam、19 17バイト

q~W%eef{~~A@#**}p

最初の項目が整数で、2番目の項目が文字列(例34 "53")である入力を受け取ります。提案は歓迎します。もっと短くできると確信しています。2バイトを節約してくれたDennisに感謝します。

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

説明

q~    e# Get input and evaluate it, x and "y"
W%    e# Reverse y so it will be properly sorted
ee    e# Enumerate through y with each character and its index
f{    e# For each digit in y...
  ~~  e# Convert the digit to an integer on the stack
  A@# e# Take 10 to the power of y's index
  **  e# Multiply all three together to get the final result
}
p     e# Print the array

1
~~A@#**数バイト節約します。
デニス

1

Haskell、37バイト

a%0=[]
a%b=b`mod`10*a:(a*10)%div b 10

文字列化せず、算術です。最小の部分積を残りの部分に再帰的に付加します。最後の桁bが切り捨てられ、乗数10が適用されます。演算子の優先順位はうまく機能します。


0

𝔼𝕊𝕄𝕚𝕟、11文字/ 23バイト(非競合)

ᴙíⓢⓜî*$*Ⅹⁿ_

Try it here (Firefox only).

この問題の解決策をコーディング中にバグを見つけました...

説明

          // Implicit: î = input 1, í = input 2
ᴙíⓢ      // reverse í and split it into an array
ⓜî*$*Ⅹⁿ_ // multiply î by each individual digit in í and put in previous array
          // implicit output

0

Japt、28バイト

I=[]Vs w m@IpApY+1 /A*U*X};I

説明:

I=[]Vs w m@IpApY+1 /A*U*X};I
I=[]                         //that's simple, init an array I
    Vs                       //take the second input and convert to string
       w                     //reverse it and...
         m@              }   //...map through the chars; X is a char, Y is counter
           Ip............    //push the following value into I...
             ApY+1 /A*U*X    //10^(Y+1)/10*U*X, U is the first input
                           I //output the resulting array

インタープリターのバグのため、(10 ^ 0)が100を与えるため、ApY+1 /10代わりにを使用する必要があります。Plzの修正、Eth。ApYAp0Ap0
ニカエル

0

Python 2、42バイト

f=lambda a,b:b*[0]and[b%10*a]+f(a*10,b/10)

文字列化せず、算術です。残りの部分に最小の部分積を再帰的に追加します。最後の桁bが切り捨てられ、乗数10が適用されます。

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