Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
"prerequisites": [],
"status": "wip"
},
{
"uuid": "88baa5ab-dacb-4fab-8f16-4f3ed726b27b",
"slug": "freelancer-rates",
"name": "Freelancer Rates",
"concepts": ["numbers"],
"prerequisites": [
"basics",
"includes"
],
"status": "wip"
},
{
"slug": "last-will",
"name": "Last Will",
Expand Down
24 changes: 24 additions & 0 deletions exercises/concept/freelancer-rates/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hints

## General

- Browse the [`cmath` reference][cmath-reference] to learn about common mathematical operations and transformations that work with `doubles`.

## 1. Calculate the daily rate given an hourly rate

- Basic arithmetic operations where one argument is an `int`, and the other is a `double`, will return a `double`.

## 2. Calculate a discounted price

- Basic arithmetic operations where one argument is an `int`, and the other is a `double`, will return a `double`.

## 3. Calculate the monthly rate, given an hourly rate and a discount

- There is a [function in the `cmath` header][cmath-ceil] for rounding up.

## 4. Calculate the number of workdays given a budget, hourly rate, and discount

- Casting a `double` to an `int` will truncate the number at the decimal point.

[cmath-reference]: https://en.cppreference.com/w/cpp/header/cmath
[cmath-ceil]: https://en.cppreference.com/w/cpp/numeric/math/ceil
56 changes: 56 additions & 0 deletions exercises/concept/freelancer-rates/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Instructions
In this exercise, you'll be writing code to help a freelancer communicate with a project manager.
Your task is to provide a few utilities to quickly calculate daily and monthly rates, optionally with a given discount.

We first establish a few rules between the freelancer and the project manager:

- The daily rate is 8 times the hourly rate.
- A month has 22 billable days.

Sometimes, the freelancer is offering to apply a discount on their daily rate (for example for their most loyal customers or not-for-profit customers).

Discounts are modeled as fractional numbers representing percentages, for example, `25.0` (25%).

## 1. Calculate the daily rate given an hourly rate

Implement a function to calculate the daily rate given an hourly rate:

```cpp
daily_rate(60)
// => 480.0
```

The returned daily rate should be of type `double`.

## 2. Calculate a discounted price

Implement a function to calculate the price after a discount.

```cpp
apply_discount(150, 10)
// => 135.0
```

The returned value should always be of type `double`, not rounded in any way.

## 3. Calculate the monthly rate, given an hourly rate and a discount

Implement a function to calculate the monthly rate, and apply a discount.

```cpp
monthly_rate(77, 10.5)
// => 12130
```

The returned monthly rate should be rounded up (take the ceiling) to the nearest integer.

## 4. Calculate the number of complete workdays given a budget, hourly rate, and discount

Implement a function that takes a budget, an hourly rate, and a discount, and calculates how many complete days of work that covers.

```cpp
days_in_budget(20'000, 80, 11.0)
// => 35
```

The returned number of days should be rounded down (take the floor) to the next integer.
46 changes: 46 additions & 0 deletions exercises/concept/freelancer-rates/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Introduction

## Numbers
The built-in number types in C++ can be divided into integers and floating points.
Integers are whole numbers like `0`, `691`, or `-2`.
Floating point numbers are numbers with a decimal point like `6.02214076`, `0.1`, or `-1.616`.

### Integers
The following example shows the declaration and initialization of four different variables

```cpp
int m_morales{9241}; // base 10: 0-9
int a_apaec{0x24CD}; // base 16: 0-9 and A-F
int m_gargan{0b10010000011001}; // base 2: 0-1
int b_reilly{022031}; // base 8: 0-7
// Leading with a 0 not the letter o.
```
When you assign a value to an `int` variable, you can do so directly with a literal.
A literal is a hard-coded number like `9241`.
There are different integer literals for several bases of the representation.
Decimal integer literals are the most common and use the digits `0` to `9`.
By adding a special prefix, like `0x`, it is possible to use other bases.
The example above shows the number `9421` in its four representations and prefixes.
All variables are initialized to the same value.

For more details on the different representation systems, take a look at [a small tutorial][cpp_numerical_bases].

You can use an apostrophe to separate digits for easier readability.
`9'241` is the same as `0b0100'100'0001'1001` or `92'4'1`.

### Floating-Point Numbers

The floating-point literals come in two flavors.
In addition to the intuitive `0.0024` it is possible to use its scientific notation `2.4e-3`.
The most common floating-point type is `double`.

### Arithmetic

C++ supports `+`, `-`, `*`, `/`, `(` and `)` and `%` to form expressions.
The result from the operation between two integers is also an integer.
`5 / 2` will return `2`.
When one of the involved types is a floating-point type, the result will also be of a floating-point.
`5.0 / 2` and `5 / 2.0` will return `2.5`.
`%` is the remainder operator and will return the remainder of an integer division: `5%3` is `2`.

[cpp_numerical_bases]: https://cplusplus.com/doc/hex/
3 changes: 3 additions & 0 deletions exercises/concept/freelancer-rates/.docs/introduction.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

%{concept:numbers}
21 changes: 21 additions & 0 deletions exercises/concept/freelancer-rates/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"vaeng"
],
"files": {
"solution": [
"freelancer_rates.cpp"
],
"test": [
"freelancer_rates_test.cpp"
],
"exemplar": [
".meta/exemplar.cpp"
]
},
"forked_from": [
"elixir/freelancer-rates"
],
"icon": "freelancer-rates",
"blurb": "Learn about integers and floating point numbers by helping a freelancer communicate with a project manager about billing."
}
30 changes: 30 additions & 0 deletions exercises/concept/freelancer-rates/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Design

## Learning objectives

- Know how to write integer literals.
- Know how to write floating point literals.
- Know their underlying type (double precision).
- Know how to truncate floating point numbers (via cast to int or `cmath` header)
- Know how to convert floats to integers.

## Out of scope

- Parsing integers and floats from strings.
- Converting integers and floats to strings.

## Concepts

- `numbers`

## Prerequisites

- `basics`
- `includes`

## Analyzer

- assert that `monthly_rate` calls `apply_discount`
- assert that `monthly_rate` calls `daily_rate`
- assert that `days_in_budget` calls `apply_discount`
- assert that `days_in_budget` calls `daily_rate`
35 changes: 35 additions & 0 deletions exercises/concept/freelancer-rates/.meta/exemplar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// INFO: Headers from the standard library should be inserted at the top via
#include <cmath>

// daily_rate calculates the daily rate given an hourly rate
double daily_rate(double hourly_rate) {
double hours_per_day{8.0};
return hours_per_day * hourly_rate;
}

// apply_discount calculates the price after a discount
double apply_discount(double before_discount, double discount) {
return before_discount * (100.0 - discount) / 100.0;
}

// monthly_rate calculates the monthly rate, given an hourly rate and a discount
// The returned monthly rate is rounded up to the nearest integer.
int monthly_rate(double hourly_rate, double discount) {
double per_day{daily_rate(hourly_rate)};
int workdays_per_month{22};
double per_month{per_day * workdays_per_month};
double after_discount{apply_discount(per_month, discount)};
int rounded_up{std::ceil(after_discount)};

return rounded_up;
}

// days_in_budget calculates the number of workdays given a budget, hourly rate,
// and discount The returned number of days is rounded down (take the floor) to
// the next integer.
int days_in_budget(int budget, double hourly_rate, double discount) {
double discounted_per_hour{apply_discount(hourly_rate, discount)};
double discounted_daily{daily_rate(discounted_per_hour)};

return static_cast<int>(budget / discounted_daily);
}
64 changes: 64 additions & 0 deletions exercises/concept/freelancer-rates/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
32 changes: 32 additions & 0 deletions exercises/concept/freelancer-rates/freelancer_rates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// INFO: Headers from the standard library should be inserted at the top via
// #include <LIBRARY_NAME>

// daily_rate calculates the daily rate given an hourly rate
double daily_rate(double hourly_rate) {
// TODO: Implement a function to calculate the daily rate given an hourly
// rate
return 0.0;
}

// apply_discount calculates the price after a discount
double apply_discount(double before_discount, double discount) {
// TODO: Implement a function to calculate the price after a discount.
return 0.0;
}

// monthly_rate calculates the monthly rate, given an hourly rate and a discount
// The returned monthly rate is rounded up to the nearest integer.
int monthly_rate(double hourly_rate, double discount) {
// TODO: Implement a function to calculate the monthly rate, and apply a
// discount.
return 0;
}

// days_in_budget calculates the number of workdays given a budget, hourly rate,
// and discount The returned number of days is rounded down (take the floor) to
// the next integer.
int days_in_budget(int budget, double hourly_rate, double discount) {
// TODO: Implement a function that takes a budget, an hourly rate, and a
// discount, and calculates how many complete days of work that covers.
return 0;
}
Loading