Skip to content
loading...

Daily Challenge #138 - Do I get a Bonus?

dev.to staff on December 16, 2019

It's bonus time in the big city! The fatcats are rubbing their paws in anticipation... but who is going to make the most money? Build a function t... [Read Full]
markdown guide
 

Since this exercise was too simplistic I decided to do it in GolfScript

{\.'0'+\if}:bonus;

You can call it like this (with 0 being false and 1 being true):

1000 0 bonus
1000

1000 1 bonus
10000

Explanation:

  • {} defines a block of code
  • :bonus assigns this block to a variable named bonus
  • ; pops the block off the stack since it's assigned to a variable anyway

The "function body":

  • \ swaps the top 2 stack arguments, so e.g. 1000 1 becomes 1 1000
  • . duplicates the top of the stack, so 1 1000 becomes 1 1000 1000
  • '0' pushes '0' on the stack, so 1 1000 1000 becomes 1 1000 1000 '0'
  • + concatenates the top two stack argument, automatically coercing to string if necessary. The stack is now 1 1000 10000
  • \ swap the top 2 stack arguments again, so 1 10000 1000
  • if pops 3 elements of the stack, if true (1) outputs the second (10000) otherwise the third (1000)
  • No explicit output is needed since a GolfScript program automatically prints the remaining stack when it finishes

In a more traditional stack-based language like Forth one could define the word bonus simply as :

: bonus if 10 * . else . then  ;

1000 true bonus
10000 
 
 

That’s a pretty apt summary of GolfScript. I like it for solving small problems like this, fun mental exercise.

 

Here's an F# version of the function without obvious args, and really without any multiplication;

let payDay = function | true -> sprintf "€%d0" | _ -> sprintf "€%d"

But it works...

let pay = payDay true 100
// pay = "€1000"
 

QBasic!

DECLARE FUNCTION CALC$ (salary!, bonus!)
CONST False = 0
CONST True = 1
CLS
PRINT CALC(32, True)
PRINT CALC(32, False)

FUNCTION CALC$(salary, bonus)
    CALC$ = "$" + LTRIM$(STR$(salary * (bonus * 9 + 1)))
END FUNCTION
 

Ruby:

def total_salary(base_salary, getting_bonus)
    "$#{getting_bonus ? (base_salary * 10).to_s : base_salary.to_s}"
end
 

String interpolation calls to_s by itself, no need to explicitly do it. :-)

"$#{getting_bonus ? base_salary * 10 : base_salary}"

But since multiplying by 10 is just adding a 0, you can also do this (not recommended though, more clever than useful):

"$#{base_salary}#{0 if getting_bonus}"
 

lol, haven't used Ruby in a while so I wanted to be sure (though, now that you mention it, implicitly converting to string in interpolation makes sense :)

Clever on adding a zero!

 
 

In Go.


import "fmt"

func finalSalary(salary int, bonus bool) string {
    if bonus {
        return fmt.Sprintf("\u00A3 %d", salary*10)
    }
    return fmt.Sprintf("\u00A3 %d", salary)
}
 

JS Quick and dirty :)

function salary(salary, bonus) {
    return ${bonus? salary * 10 : salary}`;
}
 

Untested PHP attempt:

function get_salary( $salary, $bonus )  {
    return '$' . ( $bonus ? $salary * 10 : $salary );
}
 

Python:

def get_salary(salary, bonus):
    return f'${salary * 10**bonus}'
code of conduct - report abuse