21
\$\begingroup\$

Your task is to sort an array containing the strings "quarter", "dime", "nickel", and "penny" any number of times in no specific order and sort them so that they are in this order: quarter dime nickel penny (in other words, greatest to least monetary value).


Rules

  1. Your program must take an array as input containing the names of U.S coins and sort them from greatest to least by monetary value.
    • For those who are not from the U.S or don't use change, the values of U.S coins, from greatest to least, are:
      • Quarter: 25 cents
      • Dime: 10 cents
      • Nickel: 5 cents
      • Penny: 1 cent
  2. You may sort this array in any way you wish, as long as the output is ordered by the monetary values shown above.
  3. Input can be taken in any way, be it command-line arguments or STDIN.
  4. An input array would be all lowercase strings, something like this:
    • quarter dime nickel nickel quarter dime penny penny
  5. The actual format of input and output is up to you.

Test Cases

"penny nickel dime quarter" 
-> "quarter dime nickel penny"

"nickel penny penny quarter quarter quarter dime dime dime dime"
-> "quarter quarter quarter dime dime dime dime nickel penny penny"

"quarter dime nickel nickel quarter dime penny penny"
-> "quarter quarter dime dime nickel nickel penny penny"

This is , so standard rules & loopholes apply.

\$\endgroup\$
  • 1
    \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ – ckjbgames Feb 3 '17 at 19:24
  • 1
    \$\begingroup\$ All test cases should include output. In the mobile the second and third cases are shown in two lines, so it looks as if the second line is the output \$\endgroup\$ – Luis Mendo Feb 3 '17 at 19:33
  • 4
    \$\begingroup\$ i'm canadian, can i assume the input has no pennies? ;) \$\endgroup\$ – undergroundmonorail Feb 3 '17 at 21:48
  • 1
    \$\begingroup\$ @undergroundmonorail Sadly, no. \$\endgroup\$ – ckjbgames Feb 5 '17 at 13:46
  • 1
    \$\begingroup\$ What happened to loonies and half-dollars? \$\endgroup\$ – Adám Feb 6 '17 at 6:29

32 Answers 32

0
\$\begingroup\$

Smalltalk, 42 bytes

a sort:[:s :t|s allButFirst>t allButFirst]

Explanation

a              input array
sort:          sort message, criterion in the block
[:s :t |       block arguments, represent entries of the input array
allButFirst    copy the receiving string without its first character
>              alphabetic comparison
\$\endgroup\$
0
\$\begingroup\$

Perl 6 (19 bytes)

Takes a list of strings.

*.sort(*.comb[5%*])

In action:

my &f = *.sort(*.comb[5%*]);
say f(<quarter dime nickel nickel quarter dime penny penny>);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.