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\$
  • \$\begingroup\$ Can we take the input one by one? Or separated by something other than a newline? \$\endgroup\$ – mınxomaτ Sep 21 '15 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 Sep 21 '15 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 Sep 21 '15 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 Sep 21 '15 at 15:05
  • 6
    \$\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\$ – Martin Ender Sep 21 '15 at 15:45

13 Answers 13

13
\$\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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]>[-]>[-]>[-]>[-]>>>>[<<<<<+[>+<<<<+>>>-]<<<[>>>+<<<-]+>>>>----------[<<<<->
>>>[-]]<<<<[>>+>[-]<<<-]>>[>>+<<<<+>>-]<<[>>+<<-]+>>>>----------[<<<<->>>>[-]]<<<<[>+>[-
]<<-]>>>>>>>>-]<<<<<<<[<++++++++[>++++++>++++++<<-]>.>.[-]<[-]]>[<<++++++++[>>++++++<<-]
>>.[-]]<<++++++++[>>>++++++<<<-]>>>.[-]<<<++++++++++.[-]>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<
<->-]<[>[-]+<-]>[<+<->>-]<<]
\$\endgroup\$
  • \$\begingroup\$ You'll get a vote for ambition, wish I could give more than one. :) \$\endgroup\$ – ciscoheat Sep 21 '15 at 19:30
  • 8
    \$\begingroup\$ I think that gif needs an epilepsy warning. \$\endgroup\$ – Alex A. Sep 21 '15 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\$
11
\$\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\$
  • \$\begingroup\$ To fix the Pyth version I think you can do meS,-*hJ/-d2K5K%J2KQ \$\endgroup\$ – FryAmTheEggman Sep 21 '15 at 20:04
  • 2
    \$\begingroup\$ or 2 bytes shorter: m-|*K5hJ/-d2K6%J2Q \$\endgroup\$ – Jakube Sep 21 '15 at 20:14
  • 1
    \$\begingroup\$ @Jakube That's clever, how about you post it? \$\endgroup\$ – xnor Sep 21 '15 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 Sep 21 '15 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\$
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\$
5
\$\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\$
  • \$\begingroup\$ Seems like your program only takes a single integer? \$\endgroup\$ – daniero Sep 23 '15 at 17:33
  • \$\begingroup\$ @daniero correct, fixed now to accept list. \$\endgroup\$ – Todd Sep 23 '15 at 18:33
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\$
  • \$\begingroup\$ eQ is the same thing as %Q10, also you can inline the assignment: I<KeQ6+-QK5;E+-QK9 \$\endgroup\$ – Jakube Sep 21 '15 at 15:10
  • \$\begingroup\$ Cool, I didn't know about that! \$\endgroup\$ – RK. Sep 21 '15 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 Sep 21 '15 at 15:17
  • \$\begingroup\$ Ah, I'll work on it. \$\endgroup\$ – RK. Sep 21 '15 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\$
  • \$\begingroup\$ Quite clever. You can handle 1 with only 2 bytes though. mt|t+d@jC"²a<"6ed6Q \$\endgroup\$ – Jakube Sep 21 '15 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\$
  • \$\begingroup\$ Sorry about the link being broken, we were doing some database work. Works now. \$\endgroup\$ – Jacob Misirian Sep 21 '15 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 Oct 1 '15 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\$
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\$

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\$

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.