Greetings! Snippets of Code is where I keep track of my programming language studies and explorations. It's not about learning new techniques - my primary goal is to create a learning diary that chronicles my journey. So, whether you're a seasoned programmer or starting, I'm excited to have you here and hope you find something inspiring.
Why now? It's simple. In continuous learning and growth, improving my craft is essential. Understanding programming languages is a way to keep my knowledge current and relevant. I plan to publish an issue weekly on my learnings for that week.
Code
In this exercise, you'll write code to help a freelancer communicate with a project manager by providing a few utilities to quickly calculate daily and monthly rates, optionally, with a discount.
We first establish a few rules between the freelancer and the project manager:
The daily rate is eight times the hourly rate
A month has 22 billable days.
The freelancer is offering to apply a discount if the project manager chooses to let the freelancer bill per month, which can come in handy if there is a specific budget the project manager has to work with. Discounts are modeled as fractional numbers representing percentages, for example, `25.0` (25%).
defmodule FreelancerRates do
def daily_rate(hourly_rate) do
hourly_rate * 8.0
end
def apply_discount(before_discount, discount) do
before_discount - ((before_discount * discount) / 100)
end
def monthly_rate(hourly_rate, discount) do
Kernel.ceil(apply_discount(daily_rate(hourly_rate) * 22, discount))
end
def days_in_budget(budget, hourly_rate, discount) do
Float.floor(budget / apply_discount(daily_rate(hourly_rate), discount), 1)
end
end
daily_rate
: This function takes an hourly rate as an argument and returns the equivalent daily rate by multiplying the hourly rate by 8.0.
apply_discount
: This function takes a rate before the discount and a discount percentage and returns the rate after applying the discount. It does this by subtracting the discounted amount (calculated as the product of the before discount rate and the discount percentage divided by 100) from the before discount rate.
monthly_rate
: This function takes an hourly rate and a discount percentage and returns the equivalent monthly rate after applying the discount. It does this by converting the hourly rate to a daily rate using the daily_rate
function, multiplying the daily rate by 22 to get the monthly rate before the discount, and applying the discount using the apply_discount
function. The result is then rounded to the nearest integer using the `ceil` function from the Kernel
module.
days_in_budget
: This function takes a budget, an hourly rate, and a discount percentage and returns the number of days that can be worked based on the budget, rate, and discount. It does this by first converting the hourly rate to a daily rate with a discount using the apply_discount
daily_rate
functions, dividing the budget by the daily rate with a discount and rounding down to the nearest integer using the floor
function from the Float
module.
Resources
[^1]: Elixir Is not Ruby is Erlang
Elixir is not Ruby. The familiar syntax has helped the language win the hearts of the broader developer community. Yet, under the hood, Elixir is all about Erlang. The Erlang that everyone tells stories about, as if it were some mythical creature, but no one dares to touch.