19
\$\begingroup\$

I'm sure you know about the $9.99 price scheme, instead of using $10. Well, in your new job as a sys admin at a large retail store, they want prices to adhere to a similar scheme:

  • All prices are in whole dollars, no cents.
  • All prices should end with 5 or 9, rounding to the closest but up if the last digit is right between 5 and 9. (Applies to last digit 2 and 7)
  • Lowest input is $1, and the lowest outputted price should be $5.

Your input is a list of integers:

12
8
41
27
144
99
3

And the output should a list of the new prices. In the above case:

15
9
39
29
145
99
5
\$\endgroup\$
10
  • \$\begingroup\$ Can we take the input one by one? Or separated by something other than a newline? \$\endgroup\$
    – user42643
    Commented Sep 21, 2015 at 13:53
  • \$\begingroup\$ For simplicity I think the specified format is best, so you can focus on solving the actual problem instead of handling formatting as well. But of course a single integer on one row is allowed. :) \$\endgroup\$
    – ciscoheat
    Commented Sep 21, 2015 at 14:11
  • 1
    \$\begingroup\$ Yes a language-specific list is probably easier to handle. But how long can I keep changing specifications? This is one of the reasons I've hesitated to post anything in code golf. \$\endgroup\$
    – ciscoheat
    Commented Sep 21, 2015 at 15:00
  • 2
    \$\begingroup\$ Newline is now dropped as a requirement, specifying only "a list". I hope it will make things better, not worse...! \$\endgroup\$
    – ciscoheat
    Commented Sep 21, 2015 at 15:05
  • 7
    \$\begingroup\$ "But how long can I keep changing specifications? This is one of the reasons I've hesitated to post anything in code golf." In case you're not aware of it, you can post challenge ideas in the sandbox where you can get community feedback before the challenge goes live, such that things like this can (hopefully) be pointed out and fixed before changes invalidate existing answers. \$\endgroup\$ Commented Sep 21, 2015 at 15:45

17 Answers 17

15
\$\begingroup\$

Brainfuck, 4428 bytes (invalid)

Once I knew the algorithm worked, I lost interest and didn't finish the input handler. That's why this solution technically solves the problem, but is very hard to use. When you start the program in an interactive interpreter (faster is better), you can enter your "number". It has to be entered in Base256 if your interpreter doesn't support number conversion (mine does). The maximum price you can enter is therefore 255.

It then performs a looping modulo if the number is greater than 9 to split off all digits except the last one. The division results are saved, while the last digit is rounded to 5 or 9. Then they are added and printed. Then the program cleans all used registers (probably overkill) and asks for the next number.

It handles all special cases ($1, $20/$21 -> $19 etc.). Watch it run for the number 4 here (about 3 minutes, video shortened):

video demo

Code

>+<+[>>>>>>>>>>>>>>>>>>[<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<<<[>[-]+<-]>
[<+>>>>>>>>>>>[-],>>[-]<[-]<[>+<<<<<<<<<<+>>>>>>>>>-]<<<<<<<<<[>>>>>>>>>+<<<<<<<<<-]>>>>
>>>>[-]>[<+<<<<<<<<+>>>>>>>>>-]<<<<<<<<<[>>>>>>>>>+<<<<<<<<<-]>>>>>>>[-]+++++++++>[<<<<+
>>>>-]<[<<<<<<+>+<<+>>>>>>>-]<<<<<<<[>>>>>>>+<<<<<<<-]>>[>>[<+<<<+>>>>-]<<<<[>>>>+<<<<-]
+>>>[<<->>>-<<<<->>>[-]]<<<[>>[-]+<<-]>>-]>>[>>>>-<<<<[-]]<<<[-]<->>>>>>>>[<<<<<<<<->>>>
>>>>-]<<<<<<<<[>>>>>>>>+<<<<<<<<-]>>>>>>>>[>>>>>+<<<<<<<<<<<<<<<->>>>>>>>>>[-]]<<<<<<<<<
-]>>>>>>>>>>>>>>>>>>>>[<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<<<<<<
<[>[-]+<-]>[<+>>>>>>>>>>>>[-]<[>+<<<<<<<<<<+>>>>>>>>>-]<<<<<<<<<[>>>>>>>>>+<<<<<<<<<-]>>
>>>>>[-]++++++++++>>>[<<<<<<<+>>>>>>>-]<<<<<<<[>>>>[<<<<<+<<+>>>>>>>-]<<<<<<<[>>>>>>>+<<
<<<<<-]>>[>-[<<+<+>>>-]<<<[>>>+<<<-]+>[<->[-]]<[>>-[>>>>>>>>-<<<<<<<<[-]]+<<[-]]>>-]>>>>
>>>>+<<<<<<<]>>>>>[-]>>[<<+<<<<<<<<+>>>>>>>>>>-]<<<<<<<<<<[>>>>>>>>>>+<<<<<<<<<<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>>>>>>>>>+
<<<<<<<<<<<-]>[-]>[-]>[-]>[-]>>>>[<<<<<+[>+<<<<+>>>-]<<<[>>>+<<<-]+>>>>----------[<<<<->
>>>[-]]<<<<[>>+>[-]<<<-]>>[>>+<<<<+>>-]<<[>>+<<-]+>>>>----------[<<<<->>>>[-]]<<<<[>+>[-
]<<-]>>>>>>>>-]<<<<<<<[<++++++++[>++++++>++++++<<-]>.>.[-]<[-]]>[<<++++++++[>>++++++<<-]
>>.[-]]<<++++++++[>>>++++++<<<-]>>>.[-]<<<++++++++++.[-]>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<
<->-]<[>[-]+<-]>[<+<->>-]<<]
\$\endgroup\$
2
  • \$\begingroup\$ You'll get a vote for ambition, wish I could give more than one. :) \$\endgroup\$
    – ciscoheat
    Commented Sep 21, 2015 at 19:30
  • 9
    \$\begingroup\$ I think that gif needs an epilepsy warning. \$\endgroup\$
    – Alex A.
    Commented Sep 21, 2015 at 20:02
12
\$\begingroup\$

CJam, 19 17 bytes

q~{2-Ab)4>59s=N}/

Test it here.

Takes input as a CJam-style list and returns output newline separated.

Explanation

qN/{   e# Run this block for each line of the input...
  ~    e#   Evaluate the current line to get the integer.
  2-   e#   Subtract 2 to get all but the last digit right.
  Ab)  e#   Convert to base 10 (discarding a potential minus sign) and split off
       e#   the last digit.
  4>   e#   Test if it's greater than 4.
  59s= e#   Select the correct digit from the string "59" based on this result.
  N    e#   Push a line feed.
}/
\$\endgroup\$
0
12
\$\begingroup\$

Python 2, 47

lambda l:[max(5,(n+3)/5*5-(n-2)/5%2)for n in l]

If we look at the sequence of rounded values, we see that they come in blocks of 5.

... 25, 29, 29, 29, 29, 29, 35, 35, 35, 35, 35, 39, ...

We find what number block we're in with (n+3)/5 (call this value J). Then, we get the right multiple of 5 with J*5, and adjust things like 30 down to 29 by subtracting 1 whenever J is even.

To special-case 1 give 5 rather than -1, we pass the result to max(5,_).

\$\endgroup\$
4
  • \$\begingroup\$ To fix the Pyth version I think you can do meS,-*hJ/-d2K5K%J2KQ \$\endgroup\$ Commented Sep 21, 2015 at 20:04
  • 2
    \$\begingroup\$ or 2 bytes shorter: m-|*K5hJ/-d2K6%J2Q \$\endgroup\$
    – Jakube
    Commented Sep 21, 2015 at 20:14
  • 1
    \$\begingroup\$ @Jakube That's clever, how about you post it? \$\endgroup\$
    – xnor
    Commented Sep 21, 2015 at 21:40
  • \$\begingroup\$ O.k. I will. But you can take the idea for your Python answer. It saves one byte: lambda l:[((n+3)/5*5or 6)-(n-2)/5%2for n in l] \$\endgroup\$
    – Jakube
    Commented Sep 21, 2015 at 21:49
8
\$\begingroup\$

Retina, 32 bytes

Accepts input in a comma separated list. There must be a trailing comma. Outputs in the same format.

T`d`aa555559`.,
T+`da`ad`\da
a
5

Explanation:

T`               #Transliteration mode.
  d`aa555559`    #Map the digits 0-9 to aa55555999
             .,  #Map only the trailing digits.
T+`              #Do until input does not change.
   da`ad`        #Map a to 9, 0 to a, and 1-9 to 0-8
         \da     #Only do this to each a and the character before each a.
a                #Match all leftover a's. This only happens when the input contains the integer 1.
5                #Replace them with 5.
\$\endgroup\$
0
6
\$\begingroup\$

Python 3, 74 82 bytes

a=eval(input())
for i in a:print(round(i,-1)+[5,-1][max(4,i-2)%10>4])

I struggled for brevity on values less than 11 and the requirement for 1 to evaluate to 5.

\$\endgroup\$
2
  • \$\begingroup\$ Seems like your program only takes a single integer? \$\endgroup\$
    – daniero
    Commented Sep 23, 2015 at 17:33
  • \$\begingroup\$ @daniero correct, fixed now to accept list. \$\endgroup\$
    – Todd
    Commented Sep 23, 2015 at 18:33
5
\$\begingroup\$

R, 51 49 47 43 bytes

(f=((n=scan()-2)%/%5+1+(n<0))*5)-(f%%10==0)

There should be room to improve this, but I thinking a different strategy might be better. Takes a vector of integers from scan and outputs a vector of integers. Essentially this uses integer division to round the number down, adds 1 and multiples it by five. Anything divisible by 10 has 1 taken away. If n = 1 then it increments the integer division by 1.

Test run

> (f=((n=scan()-2)%/%5+1+(n<0))*5)-(f%%10==0)
1: 1
2: 12
3: 8
4: 41
5: 27
6: 144
7: 99
8: 3
9: 
Read 8 items
[1]   5  15   9  39  29 145  99   5
> 
\$\endgroup\$
4
\$\begingroup\$

Pyth, 21 18 29 28 bytes

Thanks to @Jakube for cutting 3 bytes!

KeQJ-QKI<K2tJ.q;I<K6+J5;E+J9

Try it here.

EDIT: Apparently it was invalid. I fixed it up at the cost of 11 bytes; I'll try to golf it more.

\$\endgroup\$
4
  • \$\begingroup\$ eQ is the same thing as %Q10, also you can inline the assignment: I<KeQ6+-QK5;E+-QK9 \$\endgroup\$
    – Jakube
    Commented Sep 21, 2015 at 15:10
  • \$\begingroup\$ Cool, I didn't know about that! \$\endgroup\$
    – RK.
    Commented Sep 21, 2015 at 15:12
  • \$\begingroup\$ No problem. Your approach is wrong though. You should round down to 9, if the last digit is 0 or 1 \$\endgroup\$
    – Jakube
    Commented Sep 21, 2015 at 15:17
  • \$\begingroup\$ Ah, I'll work on it. \$\endgroup\$
    – RK.
    Commented Sep 21, 2015 at 15:18
4
\$\begingroup\$

Pyth, 21 bytes

m?tdtt+d@jC"²a<"6ed5Q

Sadly I have to spend 4 bytes to correctly handle $1.

\$\endgroup\$
1
  • \$\begingroup\$ Quite clever. You can handle 1 with only 2 bytes though. mt|t+d@jC"²a<"6ed6Q \$\endgroup\$
    – Jakube
    Commented Sep 21, 2015 at 16:21
4
\$\begingroup\$

Pyth, 18 bytes

m-|*K5hJ/-d2K6%J2Q

Try it online: Demonstration or Test Suite

This answer is based on @xor's Python/Pyth solution. The main difference is, that I handle the special case 1 differently. The actual result for 1 would be 0 - 1 = -1. Using Python's or I can replace the 0 with a 6, resulting in 6 - 1 = 5. This saves the pain of taking the maximum of 5 and the result.

Explanation:

m-|*K5hJ/-d2K6%J2Q
m                Q   map each number d of the input list Q to:
    K5                  K = 5
       J/-d2K           J = (d - 2) / K
   *K hJ                   K * (J + 1)
  |                     or
             6             6 # if K*(J+1)==0
 -            %J2       minus (J mod 2)
\$\endgroup\$
3
\$\begingroup\$

Hassium, 133 Bytes

func main(){i=[12,8,41,27,144,99,3];foreach(e in i){f=e%10;if(!(e/10==0))print(e/10);if(f<5)r=5;else if(f>5)r=9;elser=f;println(r);}}

Run and see the expanded online: http://hassiumlang.com/Hassium/index.php?code=4f1c14f4d699b11da7a6392a74b720c4

\$\endgroup\$
2
  • \$\begingroup\$ Sorry about the link being broken, we were doing some database work. Works now. \$\endgroup\$ Commented Sep 21, 2015 at 22:04
  • \$\begingroup\$ As outlined in the code golf tag wiki, answers must be full programs or functions. Snippets that simply hardcode the input are not allowed, unless the question explicitly says otherwise. \$\endgroup\$
    – Dennis
    Commented Oct 1, 2015 at 5:39
3
\$\begingroup\$

TI-BASIC, 19 bytes

int(Ans/5+.6
max(5,5Ans-not(fPart(Ans/2

Uses xnor's algorithm. TI-BASIC gets vectorization and multiplication for free, but we spend a few more bytes because it doesn't have modulo.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 55 50 + 1 bytes

Run it with the n flag, like so: ruby -n prices.rb. Enter each price on a separate line.

x=$_.to_i
p x<7?5:(x-2).round(-1)+(~/[2-6]$/?5:-1)
\$\endgroup\$
2
\$\begingroup\$

JavaScript, 50

l=>l.map(n=>Math.max(5,((n+3)/5|0)*5-((n-2)/5&1)))

I use an &1 to save bytes instead of |0 and mod 2

\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 33 bytes

/.1$|[60]$/?$_--:$_++until/[59]$/

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Haskell, 114 bytes

g n
 |n>6=9-n
 |n>1=5-n
 |1>0=(-n-1)
f n=show$(read n)+(g$read$(:[])$last n)
main=interact(unlines.(map f).lines)

Explanation:

The function g returns 9-n if n>6 or else 5-n if n>1 or else -n-1. g is given the last digit and returns what should be added to the input number. f uses g to get the solution (plus lots of string manipulation). main outputs the result of f for each line of input.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, ja, 13 12 bytes

ƛ⇩ȧṫ4>59$iJṅ

Try it Online!

I simply ported the CJam answer.

Explained

ƛ⇩ȧṫ4>59$iJṅ
ƛ             # Over every number in the input,
 ⇩ȧ           #   abs(a - 2)
   ṫ4>        #   ↑[:-1], ↑[-1] > 4
      59$i    #   indexed into 59
          Jṅ  #   merged and joined on ""
\$\endgroup\$
0
\$\begingroup\$

Stax, 12 bytes

Ç«δv▀▼ÖP▄¶⌠"

Run and debug it

Yet another port of the CJam answer.

-1 more byte if a list of integers is taken as input.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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