loading...

Daily Challenge #239 - Graceful Tipping

thepracticaldev profile image dev.to staff ・1 min read

Adding tip to a restaurant bill in a graceful way can be tricky, that's why you need make a function for it. The function will receive the restaurant bill (always a positive number) as an argument.

You need to:
1) add at least 15% in tip
2) round that number up to an elegant value and
3) return it.

What is an elegant number? It depends on the magnitude of the number to be rounded. Numbers below 10 should simply be rounded to whole numbers. Numbers 10 and above should be rounded like this:
10 - 99.99... ---> Round to number divisible by 5
100 - 999.99... ---> Round to number divisible by 50
1000 - 9999.99... ---> Round to number divisible by 500
And so on...

Examples
1 --> 2
7 --> 9
12 --> 15
86 --> 100

Tests
99 -->
114 -->
1149 -->

Good luck!


This challenge comes from viktorostlund on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

markdown guide
 

nim:

import math

proc gracefulTipping*(rest_bill:float):float=
    result = ceil(rest_bill*1.15)

    if result < 10:
        return result

    let exp = floor(log10(result))-1

    let mod_op = 5 * pow(10.0,exp)

    if floorMod(result,mod_op)>0:
        result += (mod_op - floorMod(result,mod_op))

 

Quick question: shouldn't the round-up from 86 become 90 instead of 100?

Anyway, nice challenge, will give it a try sometime!