|
| 1 | +# Cars Assemble |
| 2 | + |
| 3 | +Welcome to Cars Assemble on Exercism's Go Track. |
| 4 | +If you need help running the tests or submitting your code, check out `HELP.md`. |
| 5 | +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +## Numbers |
| 10 | + |
| 11 | +Go contains basic numeric types that can represent sets of either integer or floating-point values. |
| 12 | +There are different types depending on the size of value you require and the architecture of the computer where the application is running (e.g. 32-bit and 64-bit). |
| 13 | + |
| 14 | +For the sake of this exercise you will only be dealing with: |
| 15 | + |
| 16 | +- `int`: e.g. `0`, `255`, `2147483647`. A signed integer that is at least 32 bits in size (value range of: -2147483648 through 2147483647). |
| 17 | + But this will depend on the systems architecture. |
| 18 | + Most modern computers are 64 bit, therefore `int` will be 64 bits in size (value rate of: -9223372036854775808 through 9223372036854775807). |
| 19 | + |
| 20 | +- `float64`: e.g. `0.0`, `3.14`. Contains the set of all 64-bit floating-point numbers. |
| 21 | + |
| 22 | +## Arithmetic Operators |
| 23 | + |
| 24 | +Go supports many standard arithmetic operators: |
| 25 | + |
| 26 | +| Operator | Example | |
| 27 | +| -------- | -------------- | |
| 28 | +| `+` | `4 + 6 == 10` | |
| 29 | +| `-` | `15 - 10 == 5` | |
| 30 | +| `*` | `2 * 3 == 6` | |
| 31 | +| `/` | `13 / 3 == 4` | |
| 32 | +| `%` | `13 % 3 == 1` | |
| 33 | + |
| 34 | +For integer division, the remainder is dropped (eg. `5 / 2 == 2`). |
| 35 | + |
| 36 | +Go has shorthand assignment for the operators above (e.g. `a += 5` is short for `a = a + 5`). |
| 37 | +Go also supports the increment and decrement statements `++` and `--` (e.g. `a++`). |
| 38 | + |
| 39 | +## Converting between int and float64 |
| 40 | + |
| 41 | +Converting between types is done via a function with the name of the type to convert to. |
| 42 | +For example: |
| 43 | + |
| 44 | +```go |
| 45 | +var x int = 42 // x has type int |
| 46 | +f := float64(x) // f has type float64 (ie. 42.0) |
| 47 | +var y float64 = 11.9 // y has type float64 |
| 48 | +i := int(y) // i has type int (ie. 11) |
| 49 | +``` |
| 50 | +## Arithmetic operations on different types |
| 51 | + |
| 52 | +In many languages you can perform arithmetic operations on different types of variables, but in Go this gives an error. |
| 53 | +For example: |
| 54 | + |
| 55 | +```go |
| 56 | +var x int = 42 |
| 57 | + |
| 58 | +// this line produces an error |
| 59 | +value := float32(2.0) * x // invalid operation: mismatched types float32 and int |
| 60 | + |
| 61 | +// you must convert int type to float32 before performing arithmetic operation |
| 62 | +value := float32(2.0) * float32(x) |
| 63 | +``` |
| 64 | + |
| 65 | +## If Statements |
| 66 | + |
| 67 | +Conditionals in Go are similar to conditionals in other languages. |
| 68 | +The underlying type of any conditional operation is the `bool` type, which can have the value of `true` or `false`. |
| 69 | +Conditionals are often used as flow control mechanisms to check for various conditions. |
| 70 | + |
| 71 | +For checking a particular case an `if` statement can be used, which executes its code if the underlying condition is `true` like this: |
| 72 | + |
| 73 | +```go |
| 74 | +var value string |
| 75 | + |
| 76 | +if value == "val" { |
| 77 | + return "was val" |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +In scenarios involving more than one case many `if` statements can be chained together using the `else if` and `else` statements. |
| 82 | + |
| 83 | +```go |
| 84 | +var number int |
| 85 | +result := "This number is " |
| 86 | + |
| 87 | +if number > 0 { |
| 88 | + result += "positive" |
| 89 | +} else if number < 0 { |
| 90 | + result += "negative" |
| 91 | +} else { |
| 92 | + result += "zero" |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +If statements can also include a short initialization statement that can be used to initialize one or more variables for the if statement. |
| 97 | +For example: |
| 98 | + |
| 99 | +```go |
| 100 | +num := 7 |
| 101 | +if v := 2 * num; v > 10 { |
| 102 | + fmt.Println(v) |
| 103 | +} else { |
| 104 | + fmt.Println(num) |
| 105 | +} |
| 106 | +// Output: 14 |
| 107 | +``` |
| 108 | + |
| 109 | +> Note: any variables created in the initialization statement go out of scope after the end of the if statement. |
| 110 | +
|
| 111 | +## Instructions |
| 112 | + |
| 113 | +In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. |
| 114 | +The assembly line's speed can range from `0` (off) to `10` (maximum). |
| 115 | + |
| 116 | +At its default speed (`1`), `221` cars are produced each hour. |
| 117 | +In principle, the production increases linearly. |
| 118 | +So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. |
| 119 | +However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. |
| 120 | + |
| 121 | +Also, there are times when the assembly line has an artificially imposed limit on the throughput (meaning no more than the limit can be produced per hour). |
| 122 | + |
| 123 | +## 1. Calculate the success rate |
| 124 | + |
| 125 | +Implement a function (`SuccessRate`) to calculate the ratio of an item being created without error for a given speed. |
| 126 | +The following table shows how speed influences the success rate: |
| 127 | + |
| 128 | +- `0`: 0% success rate. |
| 129 | +- `1` - `4`: 100% success rate. |
| 130 | +- `5` - `8`: 90% success rate. |
| 131 | +- `9` - `10`: 77% success rate. |
| 132 | + |
| 133 | +```go |
| 134 | +rate := SuccessRate(6) |
| 135 | +fmt.Println(rate) |
| 136 | +// Output: 0.9 |
| 137 | +``` |
| 138 | + |
| 139 | +## 2. Calculate the production rate per hour |
| 140 | + |
| 141 | +Implement a function to calculate the assembly line's production rate per hour: |
| 142 | + |
| 143 | +```go |
| 144 | +rate := CalculateProductionRatePerHour(7) |
| 145 | +fmt.Println(rate) |
| 146 | +// Output: 1392.3 |
| 147 | +``` |
| 148 | + |
| 149 | +> Note that the value returned is of type `float64`. |
| 150 | +
|
| 151 | +## 3. Calculate the number of working items produced per minute |
| 152 | + |
| 153 | +Implement a function to calculate how many cars are produced each minute: |
| 154 | + |
| 155 | +```go |
| 156 | +rate := CalculateProductionRatePerMinute(5) |
| 157 | +fmt.Println(rate) |
| 158 | +// Output: 16 |
| 159 | +``` |
| 160 | + |
| 161 | +> Note that the value returned is of type `int`. |
| 162 | +
|
| 163 | +## 4. Calculate the artificially-limited production rate |
| 164 | + |
| 165 | +Implement a function to calculate the assembly line's production rate per hour: |
| 166 | + |
| 167 | +```go |
| 168 | +rate := CalculateLimitedProductionRatePerHour(2, 1000.0) |
| 169 | +fmt.Println(rate) |
| 170 | +// Output: 442.0 |
| 171 | +rate := CalculateLimitedProductionRatePerHour(7, 1000.0) |
| 172 | +fmt.Println(rate) |
| 173 | +// Output: 1000.0 |
| 174 | +``` |
| 175 | + |
| 176 | +> Note that the value returned is of type `float64`. |
| 177 | + This should call the `CalculateProductionRatePerHour` function exactly once. |
| 178 | + |
| 179 | +## Source |
| 180 | + |
| 181 | +### Created by |
| 182 | + |
| 183 | +- @DavyJ0nes |
| 184 | + |
| 185 | +### Contributed to by |
| 186 | + |
| 187 | +- @tehsphinx |
0 commit comments