電子貯金箱


11

貯金箱は、中に収集コインを使用した容器である。この課題は、4枚の米国のコイン使用の場合:四半期、ダイム、ニッケル、およびペニーを。

チャレンジ

あなたの課題は、電子貯金箱を作成することです。プログラム(または関数)を作成して、実行(または呼び出し)したときに、所有している各コインの数と、コインの合計金額を出力(または返す)します。

入力

貯金箱に入れるコインの文字列、配列など(お好み)(大文字と小文字は区別されません)。

 Q - Quarter(Value of 25)
 D - Dime(Value of 10)
 N - Nickel(Value of 5)
 P - Penny(Value of 1)

出力

選択した整数以外の区切り文字で区切られた、入力からのコインの数と合計金額。(コインの合計を出力する順序は重要ではありませんが、コインの合計値(合計)は最後の要素でなければなりません)

例

 Input          ->       Output

 P              ->       1,0,0,0,1 or 0,0,0,1,1 or 0,0,1,0,1 or 1,1
 N              ->       0,1,0,0,5
 D              ->       0,0,1,0,10 
 Q              ->       0,0,0,1,25
 QQQQ           ->       0,0,0,4,100
 QNDPQNDPQNDP   ->       3,3,3,3,123
 PPPPPPPPPP     ->       10,0,0,0,10
 PNNDNNQPDNPQND ->       3,6,3,2,113

ルール

標準の抜け穴は許可されていません。

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


1
@Shaggyはいすることができます
— DevelopingDeveloper

1
合計でのコインの順序。入力の出現順にそれを与えることができますか?
— アダム

1
したがって、入力から順序を推測できる場合、0を省略できますか?各数字がどの文字を指しているかはまだ明らかです。
— アダム

1
この出力形式は遠すぎますか?有効かどうかわからない19バイトの回答:オンラインで試してみてください!
— 魔法のタコ

1
[money]タグが必要だと思います。
— 12Me21

回答:




4

APL(Dyalog Unicode)、28 27バイト

1 5 10 25(⊢,+.×)'PNDQ'+.=¨⊂

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

暗黙関数。入力を形式のベクトルとして取得します,'<input>'。

1バイトのngnに感謝します!

どうやって?

1 5 10 25(⊢,+.×)'PNDQ'+.=¨⊂ ⍝ Main function, tacit.
                          ⊂ ⍝ Enclose
                         ¨  ⍝ Each character of the input
                      +.=   ⍝ Sum the number of matched characters
                'PNDQ'      ⍝ From this string
1 5 10 25(  +.×)            ⍝ Multiply the values with the left argument, then sum them.
          ⊢,                ⍝ And append to the original vector of coins.

1⊥∘.=∘'PNDQ'->'PNDQ'+.=¨⊂
— ngn

4

R、70 69バイト

function(n)c(x<-table(factor(n,c("P","N","Q","D"))),x%*%c(1,5,25,10))

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

入力を個々の文字のベクトルとして受け取ります。変換し、それらをfactorsし、tabulateそれらをよ、その後、内積を使用して値を計算します。

テストを簡単にするために、上記のテストケースから関数が期待する入力に変換する方法を追加しました。

これは、コイン名をvectorとして保存することをやっと打ち負かしnamesます。つまり、より多くのコインタイプがあれば、以下のアプローチはおそらくゴルファーになります。

R、71 70バイト

function(n)c(x<-table(factor(n,names(v<-c(P=1,N=5,Q=25,D=10)))),x%*%v)

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


3

ゼリー、19バイト

ċЀ“PNDQ”µæ.“¢¦½ı‘ṭ

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

使い方

ċЀ“PNDQ”µæ.“¢¦½ı‘ṭ    Main link. Arguments: s (string)
 Ѐ“PNDQ”              For each char in "PNDQ":
ċ                        Count the occurrences of the char in s.
                       Collect the results in an array. Call this a.
         µ             Start a new monadic chain. Argument: a
          æ.           Take the dot product of a with
            “¢¦½ı‘       [1, 5, 10, 25].
                  ṭ    Tack this onto the end of a.

3

JavaScript(ES6)、63 61バイト

シャギーのおかげで2バイト節約

入力を文字の配列として受け取ります。出力P,N,D,Q,total。ovsのPython answerに
触発されました。

a=>eval(a.join`++,`+`++,[P,N,D,Q,P+N*5+D*10+Q*25]`,P=N=D=Q=0)

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


元の回答、73バイト

入力を文字の配列として受け取ります。出力Q,D,N,P,total。

a=>a.map(c=>o[o[4]+='521'[i='QDNP'.search(c)]*5||1,i]++,o=[0,0,0,0,0])&&o

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


とてもうまくできました!いくつかのものをシャッフルすることで2バイトをノックすることができます。
— シャギー

3

MATL、22 20バイト

!'PNDQ'=Xst[l5X25]*s

オンラインでお試しください!または、すべてのテストケースを確認します。

例付きの説明

'PNNDNNQPDNPQND'例として入力を検討してください。スタックの内容は上下逆に表示されます。つまり、上部の要素が下に表示されます。

!        % Implicit input: string (row vector of chars). Transpose into a
         % column vector of chars
         % STACK: ['P';
                   'N';
                   'N';
                   'D';
                   'N';
                   'N';
                   'Q';
                   'P';
                   'D';
                   'N';
                   'P';
                   'Q';
                   'N';
                   'D']
'PNDQ'   % Push this string (row vector of chars)
         % STACK: ['P';
                   'N';
                   'N';
                   'D';
                   'N';
                   'N';
                   'Q';
                   'P';
                   'D';
                   'N';
                   'P';
                   'Q';
                   'N';
                   'D'],
                  'PNDQ'
=        % Implicit input. Test for equality, element-wise with broadcast
         % STACK: [1 0 0 0;
                   0 1 0 0;
                   0 1 0 0;
                   0 0 1 0;
                   0 1 0 0;
                   0 1 0 0;
                   0 0 0 1;
                   1 0 0 0;
                   0 0 1 0;
                   0 1 0 0;
                   1 0 0 0;
                   0 0 0 1;
                   0 1 0 0;
                   0 0 1 0]
Xs       % Sum of each column
         % STACK: [3 6 3 2]
t        % Duplicate
         % STACK: [3 6 3 2],
                  [3 6 3 2]
[l5X25]  % Push array [1 5 10 25]
         % STACK: [3 6 3 2],
                  [3 6 3 2],
                  [1 5 10 25]
*        % Multiply, element-wise
         % STACK: [3 6 3 2],
                  [3 30 30 50]
s        % Sum
         % STACK: [3 6 3 2],
                  113
         % Implicitly display

!'PNDQ'=Xst[l5X25]!Y*21バイトです。私はそれをテストしていないと認めていますが。
— ジュゼッペ

@Giuseppeのおかげで、しかし課題は、「言う私は唯一の区切り文字が許可されていると仮定して、お好みの非整数の区切り」
— ルイスMendo

私はそれに気づかなかった。まぁ。
— ジュゼッペ

1
21バイトのソリューションは大丈夫だと思います... OPはその効果について何かコメントしました
— ジュゼッペ

@Giuseppeすごい!その後、20バイトまで。ヘッズアップをありがとう
— ルイスメンドー

3

Japt、25 22バイト

@Shaggyのおかげで3バイト節約

`p˜q`¬£èX
pUí*38#éìH)x

オンラインでテストしてください!小文字で入力します

説明

`p˜q`¬         Split the compressed string "pndq" into chars, giving ["p", "n", "d", "q"].
      £        Map each char X to
       èX      the number of occurrences of X in the input.
<newline>      Set variable U to the resulting array.
 Uí*           Multiply each item in U by the corresponding item in
    38#é         38233
        ìH       converted to base-32, giving [1, 5, 10, 25].
           x   Take the sum.
p              Append this to the end of U.
               Implicit: output result of last expression

これまであまり運がなかったので、カンマを離れてゴルフをする方法を見つけようとしています。
— シャギー


そして、小文字で入力を取り、別のバイトを保存します。
— シャギー


@Shaggy Impressive、ありがとう!
— ETHproductions

3

Excel(ポーランド語版)、150バイト

入力はA1です。数式はセル内にありますB1- F1:

cell  formula
------------------------------
B1    =DŁ(A1)-DŁ(PODSTAW(A1;"Q";""))
C1    =DŁ(A1)-DŁ(PODSTAW(A1;"D";""))
D1    =DŁ(A1)-DŁ(PODSTAW(A1;"N";""))
E1    =DŁ(A1)-DŁ(PODSTAW(A1;"P";""))
F1    =B1*25+C1*10+D1*10+E1

四半期、ダイム、硬貨、pennys細胞における合計の数の出力をもたらすB1、C1、D1、E1およびF1それぞれ。

英語版(162バイト):

cell  formula
------------------------------
B1    =LEN(A1)-LEN(SUBSTITUTE(A1;"Q";""))
C1    =LEN(A1)-LEN(SUBSTITUTE(A1;"D";""))
D1    =LEN(A1)-LEN(SUBSTITUTE(A1;"N";""))
E1    =LEN(A1)-LEN(SUBSTITUTE(A1;"P";""))
F1    =B1*25+C1*10+D1*10+E1

3

APL + WIN、33 27バイト

Adamのおかげで5バイト節約

コイン文字列の画面入力のプロンプト。

n,+/1 5 10 25×n←+⌿⎕∘.='PNDQ'

5バイトを節約:n,+/1 5 10 25×n←+⌿⎕∘.='PNDQ'
— 18年

@ngnありがとう。古い時代には:(私の良くなっている
— グラハム

2

05AB1E、30 26 22 21 19バイト

X5T25)s.•50†•S¢=*O=

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


X                   # Push 1.
 5                  # Push 5.
  T                 # Push 10.
   25               # Push 25.
     )s             # Wrap stack to array, swap with input.
       .•50†•       # Push 'pndq'.
             S      # Push ['p','n','d','q'] (split).
              ¢     # Count (vectorized).
               =    # Print counts, without popping.
                *   # Multiply counts by [1,2,10,25]
                 O  # Sum.
                  = # Print.

投げ捨てる:

Full program: X5T25)s.•50†•S¢=*O=
current >> X  ||  stack: []
current >> 5  ||  stack: [1]
current >> T  ||  stack: [1, '5']
current >> 2  ||  stack: [1, '5', 10]
current >> )  ||  stack: [1, '5', 10, '25']
current >> s  ||  stack: [[1, '5', 10, '25']]
current >> .  ||  stack: [[1, '5', 10, '25'], 'pnndnnqpdnpq']
current >> S  ||  stack: [[1, '5', 10, '25'], 'pnndnnqpdnpq', 'pndq']
current >> ¢  ||  stack: [[1, '5', 10, '25'], 'pnndnnqpdnpq', ['p', 'n', 'd', 'q']]
current >> =  ||  stack: [[1, '5', 10, '25'], [3, 5, 2, 2]]
[3, 5, 2, 2]
current >> *  ||  stack: [[1, '5', 10, '25'], [3, 5, 2, 2]]
current >> O  ||  stack: [[3, 25, 20, 50]]
current >> =  ||  stack: [98]
98
stack > [98]

印刷出力:

[3, 25, 20, 50]\n98 or [P, N, D, Q]\n<Sum>

何かが印刷されたため、終了スタックは無視されます。


2

J、29バイト

1 5 10 25(],1#.*)1#.'PNDQ'=/]

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

説明:

'PNDQ'=/] 等式テーブルを作成します

   'PNDQ' =/ 'PNNDNNQPDNPQND'
1 0 0 0 0 0 0 1 0 0 1 0 0 0
0 1 1 0 1 1 0 0 0 1 0 0 1 0
0 0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0 0 1 0 0

1#. テーブルの各行の合計、つまり各値の出現回数を見つける

   1#. 'PNDQ' =/ 'PNNDNNQPDNPQND'
3 6 3 2

1#.* 左右の引数の内積を求めます

    1 5 10 25(1#.*)3 6 3 2
113

], 値のリストにドット積を追加します

   1 5 10 25(],1#.*)1#.'PNDQ'=/] 'PNNDNNQPDNPQND'
3 6 3 2 113

2

C#(.NET Core)、163 136バイト

たくさんのバイトを保存してくれた@raznagulに感謝します!

n=>{var m="";int c=0,i=0,k=0;for(var v=new[]{1,5,10,25};i<4;m+=k+",",c+=k*v[i++],k=0)foreach(var x in n)k+=x=="PNDQ"[i]?1:0;return m+c;}

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


古いバージョン:

n=>{var v=new[]{1,5,10,25};string l="PNDQ",m="";int c=0,i,j,k;for(i=0;i<4;i++){for(j=0,k=0;j<n.Length;j++)k+=n[j]==l[i]?1:0;m+=k+",";c+=k*v[i];k=0;}m+=c;return m;}

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


1
私はあなたの魂を136バイトまでゴルフすることができました。リストへの多くの変更。
— raznagul

@raznagul素晴らしい解決策!入力を配列ではなく汎用リストに切り替えた理由を尋ねてもいいですか?知っている限り、リストを使用せずに文字列の文字を反復処理できます。
— イアンH.

それは以前のバージョンの名残であるため、のn.Count代わりに使用できますn.Length。完全に削除されたので、string今すぐ使用できます。
— raznagul



1

05AB1E、19バイト

"PNDQ"S¢D•Ωт•₂в*O)˜

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

説明

"PNDQ"                # push this string
      S               # split to list of chars
       ¢              # count the occurrences of each in input
        D             # duplicate
         •Ωт•         # push 21241
             ₂в       # convert to a list of base 26 digits
               *      # element-wise multiplication
                O     # sum
                 )˜   # wrap in a flattened list

1

Java(OpenJDK 8)、148バイト

c->{int q=0,d=0,n=0,p=0;for(char w:c){if(w=='Q')q++;if(w=='D')d++;if(w=='N')n++;if(w=='P')p++;}return ""+q+","+d+","+n+","+p+","+(q*25+d*10+n*5+p);}

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

まあ、それは他の Javaの提出よりも1バイト短いだけですが、ちょっと短いです:D

説明:

int q=0,d=0,n=0,p=0;    //Initialize too many integers
for(char w:c){    //Loop through each coin
  if(w=='Q')q++;if(w=='D')d++;if(w=='N')n++;if(w=='P')p++;    //Increment the correct coin
}return ""+q+","+d+","+n+","+p+","+(q*25+d*10+n*5+p);    //Return each coin count and the total monetary value 


1

Gol> <>、47バイト

5R0TiE!vD;
5+@P@@t>b%m$.
a+$P$t
PrPrt
9s+r$P$rt

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

出力形式は[P Q N D Value]です。

使い方

5R0TiE!vD;
       >b%m$.

5R0            Repeat command '0' (push 0) 5 times
   T           Set teleport location for later
    i          Input a char
     E         Pop if the last input was EOF; skip next otherwise

               If the last is EOF, the following is run:
      ! D;     Skip 'v', print the contents of the stack from bottom to top, then exit

               Otherwise the following is run:
       v
       >b%m$.  Take the top (input) modulo 11, and jump to (-1, input % 11)
               P%11 = 3, N%11 = 1, D%11 = 2, Q%11 = 4

5+@P@@t        Runs if the input is N
5+             Add 5 to top
  @            Rotate top 3 (the 3rd comes to the top)
   P           Increment the top
  @P@@         Increment the 3rd from top
      t        Teleport to the last 'T'

a+$P$t         Runs if the input is D
a+             Add 10 to top
  $            Swap top two
  $P$          Increment the 2nd from top
     t         Teleport to the last 'T'

PrPrt          Runs if the input is P
P              Increment the top
 r             Reverse the stack
 rPr           Increment the bottom
    t          Teleport to the last 'T'

9s+r$P$rt      Runs if the input is Q
9s+            Add 25 to the top ('s': add 16 to the top)
   r$P$r       Increment the 2nd from bottom
        t      Teleport to the last 'T'


1

Pyth、23 27 26バイト

+Jm/Qd"PNDQ"s.b*NYJ[h05T25

@RKのおかげで1バイト節約できました。[P、N、D、Q、値]として出力します。
ここで試してみてください

説明

+Jm/Qd"PNDQ"s.b*NYJ[h05T25
 Jm/Qd"PNDQ"                Save the count of each coin (in PNDQ order) as J.
                   [h05T25  [1, 5, 10, 25].
             .b   J       For each pair of count and value...
               *NY          ... take the product...
            s               ... and get the sum.
+                          Stick that onto the list of counts.

あなたは、取得するにはJの定義とJの最初の使用を凝縮することができます+Jm/Qd"PNDQ"s.b*NYJ[h05T25
— RKを。

1

C(clang)、112バイト

f(char *i){int c,d,v[5]={0};for(;c=*i++;v[d=(c|c/2)&3]++,v[4]+="AYJE"[d]-64);for(c=0;c<5;printf("%d,",v[c++]));}

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

出力seqはP、Q、D、N、total-valueになりました
なりました。小文字と大文字の両方の入力で動作します。

説明:

"AYJE"または{64+1,64+25,64+10,64+5}です。64+コインの価値。
d=(c|c/2)&3(インデックスとして使用)は、大文字と小文字の両方で、それぞれ入力の値1,2,3,0を持ちますq,d,n,p。



c = 0を削除することは良いキャッチでした。ありがとう。
— GPS

0

C#(.NET Core)、156バイト

s=>{Func<char,int>f=i=>{return s.Split(i).Length-1;};var a=new[]{f('P'),f('N'),f('D'),f('Q')};return$"{string.Join(",",a)},{a[0]+a[1]*5+a[2]*10+a[3]*25}";};


0

網膜、50バイト

P
P_
N
N5*
D
D10*
Q
Q25*
^
PNDQ_
O`.
(.)(\1*)
$.2¶

オンラインでお試しください!出力は、D、N、P、Q、合計の順になります。説明:

P
P_
N
N5*
D
D10*
Q
Q25*

_各コインの値に対応するs を挿入して合計を計算します。

^
PNDQ_

一致する各文字が少なくとも1つあるように、各文字の余分なコピーを挿入します。

O`.

文字を順番に並べ替えます。

(.)(\1*)
$.2¶

最初の文字の後の各文字の数を数えます。


0

SmileBASIC、70バイト

INPUT C$P=N+D+Q
WHILE""<C$INC VAR(POP(C$))WEND?P,N,D,Q,P+N*5+D*10+Q*25

例:

? PNDNDNDQP
2   3   3   1   72

説明:

INPUT COINS$
P=N+D+Q 'create variables
WHILE COINS$>"" 'loop until the coin list is empty
 'pop a character from the coin list
 'and increment the variable with that name
 INC VAR(POP(COINS$))
WEND
PRINT P,N,D,Q,P+N*5+D*10+Q*25

0

C、149バイト

f(char*s){int a[81]={},b[]={1,5,10,25},c=0;char*t,*v="PNDQ";for(t=s;*t;a[*t++]++);for(t=v;*t;printf("%d,",a[*t++]))c+=a[*t]*b[t-v];printf("%d\n",c);}

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

Cには連想配列がないため、偽造し(非常に非効率的、メモリ的に!)、ルックアップ配列で再度ループ処理してコインを加算します。ただし、外貨は計算しません:-)


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