Skip to content

Commit d0b8f30

Browse files
author
Aadit Kamat
authored
Add solution to cars assemble in go
1 parent 1ea89e3 commit d0b8f30

File tree

8 files changed

+518
-0
lines changed

8 files changed

+518
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"blurb": "Learn about numbers and type conversion by analyzing an assembly line in a car factory.",
3+
"authors": [
4+
"DavyJ0nes"
5+
],
6+
"contributors": [
7+
"tehsphinx"
8+
],
9+
"files": {
10+
"solution": [
11+
"cars_assemble.go"
12+
],
13+
"test": [
14+
"cars_assemble_test.go"
15+
],
16+
"exemplar": [
17+
".meta/exemplar.go"
18+
]
19+
}
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"go","exercise":"cars-assemble","id":"b0dcffe0dc9c47a0bda26bc1a5735174","url":"https://exercism.org/tracks/go/exercises/cars-assemble","handle":"aaditkamat","is_requester":true,"auto_approve":false}

Exercism/go/cars-assemble/HELP.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
To run the tests run the command `go test` from within the exercise directory.
6+
7+
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
8+
flags:
9+
10+
go test -v --bench . --benchmem
11+
12+
Keep in mind that each reviewer will run benchmarks on a different machine, with
13+
different specs, so the results from these benchmark tests may vary.
14+
15+
## Submitting your solution
16+
17+
You can submit your solution using the `exercism submit cars_assemble.go` command.
18+
This command will upload your solution to the Exercism website and print the solution page's URL.
19+
20+
It's possible to submit an incomplete solution which allows you to:
21+
22+
- See how others have completed the exercise
23+
- Request help from a mentor
24+
25+
## Need to get help?
26+
27+
If you'd like help solving the exercise, check the following pages:
28+
29+
- The [Go track's documentation](https://exercism.org/docs/tracks/go)
30+
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
31+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
32+
33+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
34+
35+
To get help if you're having trouble, you can use one of the following resources:
36+
37+
- [How to Write Go Code](https://golang.org/doc/code.html)
38+
- [Effective Go](https://golang.org/doc/effective_go.html)
39+
- [Go Resources](http://golang.org/help)
40+
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)

Exercism/go/cars-assemble/HINTS.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Hints
2+
3+
## General
4+
5+
- [Basic types][basic types] and [Type conversions][type conversions] tutorials.
6+
7+
## 1. Calculate the success rate
8+
9+
- Use if conditionals to return the values in the table.
10+
11+
## 2. Calculate the production rate per hour
12+
13+
- Use the `successRate` method coupled with the base rate (221 times the speed).
14+
- When multiplying two numbers by one another, they both need to be of the same type.
15+
16+
## 3. Calculate the number of working items produced per minute
17+
18+
- Use the `CalculateProductionRatePerHour` function.
19+
- Remember to cast the result to an `int`.
20+
21+
## 4. Calculate the artificially-limited production rate
22+
23+
- Use the `CalculateProductionRatePerHour` function.
24+
- Use an initializer statement for the if condition.
25+
26+
[basic types]: https://tour.golang.org/basics/11
27+
[type conversions]: https://tour.golang.org/basics/13

Exercism/go/cars-assemble/README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cars
2+
3+
// SuccessRate is used to calculate the ratio of an item being created without
4+
// error for a given speed
5+
func SuccessRate(speed int) float64 {
6+
if (speed == 0) {
7+
return 0.0;
8+
}
9+
if (speed >= 1 && speed <= 4) {
10+
return 1.0;
11+
}
12+
if (speed >= 5 && speed <= 8) {
13+
return 0.9;
14+
}
15+
return 0.77;
16+
}
17+
18+
// CalculateProductionRatePerHour for the assembly line, taking into account
19+
// its success rate
20+
func CalculateProductionRatePerHour(speed int) float64 {
21+
const CarsProducedEachHour = 221;
22+
return float64(CarsProducedEachHour * speed) * SuccessRate(speed);
23+
}
24+
25+
// CalculateProductionRatePerMinute describes how many working items are
26+
// produced by the assembly line every minute
27+
func CalculateProductionRatePerMinute(speed int) int {
28+
return int(CalculateProductionRatePerHour(speed) / 60);
29+
}
30+
31+
// CalculateLimitedProductionRatePerHour describes how many working items are
32+
// produced per hour with an upper limit on how many can be produced per hour
33+
func CalculateLimitedProductionRatePerHour(speed int, limit float64) float64 {
34+
rate := CalculateProductionRatePerHour(speed);
35+
if (rate > limit) {
36+
return limit;
37+
}
38+
return rate;
39+
}

0 commit comments

Comments
 (0)