电子存钱罐


11

一个储蓄罐是用于在收集硬币的容器对于这个挑战使用四个美国硬币:季毛钱,镍和一分钱

挑战

您的挑战是创建一个电子存钱罐。编写一个程序(或函数),当运行(或调用)该程序(或函数)时,输出(或返回)您拥有的每个硬币的计数以及硬币的总金额。

输入值

一串,一串等(您选择的)硬币放入您的存钱罐(不区分大小写)。

 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个字节的答案:在线尝试!
魔术八达通Ur

1
我觉得应该有[money]标签。
18Me21年

Answers:


5

Python 2,73个字节

-3字节归功于@Rod

C=map(input().count,'QDNP')
print C+[sum(map(int.__mul__,C,[25,10,5,1]))]

在线尝试!


4
您可以替换a*b for a,b in zip(C,[25,10,5,1])map(int.__mul__,C,[25,10,5,1])以节省一些字节
Rod


4

APL(Dyalog Unicode)28 27字节

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

在线尝试!

隐性功能。将输入作为格式的向量,'<input>'

感谢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

R70 69字节

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

在线尝试!

将输入作为单个字符的向量。将它们转换为factorstabulates ^它们,然后用点积计算值。

为了便于测试,我添加了一种方法,可以将上述测试用例转换为功能期望的输入。

这几乎不存在将硬币名称存储为vector的情况names,这意味着如果我们有更多的硬币类型,则下面的方法可能更容易实现:

R71 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字节

由于Shaggy节省了2个字节

将输入作为字符数组。输出P,N,D,Q,total
ovs的Python答案启发。

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

MATL22 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谢谢,但是挑战说“ 您选择非整数定界符”,所以我假设只允许使用一个定界符
Luis

我没有注意到。呃,好吧。
朱塞佩

1
我认为21字节的解决方案是可以的... OP对此表示评论
朱塞佩

@Giuseppe太好了!然后减少到20个字节。感谢您的单挑
Luis Mendo

3

Japt25 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令人印象深刻,谢谢!
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数目和细胞的总和的输出B1C1D1E1F1分别。

英文版(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字节

多亏了亚当,节省了5个字节

提示屏幕输入硬币串。

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

保存5个字节:n,+/1 5 10 25×n←+⌿⎕∘.='PNDQ'
亚当

@ngn谢谢。老年使我变得更好:(
Graham

2

05AB1E30 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字节

感谢@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很好的解决方案!请问为什么您将输入切换到通用列表而不是数组?可以在不使用列表的情况下遍历字符串的字符。
伊恩·

这是早期版本的残余,因此我可以使用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提交短一个字节,但是嘿,短一点就是: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,节省了一个字节。输出为[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++]));}

在线尝试!

现在,输出序列为P,Q,D,N,总值
为小写和大写输入均可。

说明:

"AYJE"或者{64+1,64+25,64+10,64+5}是。64+硬币价值。
d=(c|c/2)&3(用作索引)分别具有输入值1,2,3,0q,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_

为每个字符插入一个额外的副本,以便每个字符至少匹配一个。

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没有关联数组,因此我伪造了它(非常低效,从内存角度来讲!),然后使用查找数组再次循环以添加硬币。它不会计算外币,但:-)


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.