DEV Community

dev.to staff
dev.to staff

Posted on

8 1

Daily Challenge #239 - Graceful Tipping

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!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (2)

Collapse
 
bb4l profile image
bb4L

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))

Collapse
 
lucasqueiroz profile image
Lucas Queiroz

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

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay