primeiro commit com exercicios de Exercism realizados até esta data

This commit is contained in:
2026-10-17 22:10:27 +01:00
commit 58fccbe27a
708 changed files with 35038 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
{
"authors": [
"DavyJ0nes",
"kahgoh"
],
"contributors": [
"tehsphinx",
"andrerfcsantos"
],
"files": {
"solution": [
"cars_assemble.go"
],
"test": [
"cars_assemble_test.go"
],
"exemplar": [
".meta/exemplar.go"
],
"invalidator": [
"go.mod"
]
},
"blurb": "Learn about numbers and type conversion by analyzing an assembly line in a car factory.",
"custom": {
"taskIdsEnabled": true
}
}
+1
View File
@@ -0,0 +1 @@
{"track":"go","exercise":"cars-assemble","id":"4fa36f30c78743439e2f50df93aeb7bb","url":"https://exercism.org/tracks/go/exercises/cars-assemble","handle":"sanctushck","is_requester":true,"auto_approve":false}
+41
View File
@@ -0,0 +1,41 @@
# Help
## Running the tests
To run the tests run the command `go test` from within the exercise directory.
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
flags:
go test -v --bench . --benchmem
Keep in mind that each reviewer will run benchmarks on a different machine, with
different specs, so the results from these benchmark tests may vary.
## Submitting your solution
You can submit your solution using the `exercism submit cars_assemble.go` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Go track's documentation](https://exercism.org/docs/tracks/go)
- The [Go track's programming category on the forum](https://forum.exercism.org/c/programming/go)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [How to Write Go Code](https://golang.org/doc/code.html)
- [Effective Go](https://golang.org/doc/effective_go.html)
- [Go Resources](http://golang.org/help)
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)
+32
View File
@@ -0,0 +1,32 @@
# Hints
## General
- To learn more about types in Go, check [Tour of Go: Basic Types][basic types].
- To learn more about type conversions in Go, check [Tour of Go: Type Conversions][type conversions].
## 1. Calculate the number of working cars produced per hour
- The percentage (passed as an argument) is a number between 0-100. To make this percentage a bit easier to work with, start by dividing it by 100.
- To compute the number of cars produced successfully, multiply the percentage (divided by 100) by the number of cars produced.
- When multiplying two numbers together, they both need to be of the same type.
Use [type conversions][type conversions] if needed.
## 2. Calculate the number of working cars produced per minute
- Start by calculating the production of successful cars per hour.
For this, you can use the `CalculateProductionRatePerHour` function you made from the previous step.
- Knowing the production per hour of cars, you can get the production per minute by dividing the production per hour by 60 (the number of minutes in an hour).
- Remember to cast the result to an `int`.
## 3. Calculate the cost of production
- Start by working out how many groups of 10 cars there are.
You can do this by dividing the number of cars by 10.
- Then work out how many cars are remaining (the [modulo operator][modulo operator] is useful for this).
- To arrive at the cost, multiply the number of groups by the cost to produce 10 cars and then multiply the number of cars remaining by the cost to produce each individual car.
Then sum the results of the multiplication together.
[basic types]: https://tour.golang.org/basics/11
[type conversions]: https://tour.golang.org/basics/13
[modulo operator]: https://golangbyexamples.com/remainder-modulus-go-golang/
+138
View File
@@ -0,0 +1,138 @@
# Cars Assemble
Welcome to Cars Assemble on Exercism's Go Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
## Numbers
Go contains basic numeric types that can represent sets of either integer or floating-point values.
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).
For the sake of this exercise you will only be dealing with:
- `int`: e.g. `0`, `255`, `2147483647`.
A signed integer that is at least 32 bits in size (value range of: -2147483648 through 2147483647).
But this will depend on the systems architecture.
Most modern computers are 64 bit, therefore `int` will be 64 bits in size (value rate of: -9223372036854775808 through 9223372036854775807).
- `float64`: e.g. `0.0`, `3.14`.
Contains the set of all 64-bit floating-point numbers.
- `uint`: e.g. `0`, `255`.
An unsigned integer that is the same size as `int` (value range of: 0 through 4294967295 for 32 bits and 0 through 18446744073709551615 for 64 bits)
Numbers can be converted to other numeric types through Type Conversion.
## Arithmetic Operators
Go supports many standard arithmetic operators:
| Operator | Example |
| -------- | -------------- |
| `+` | `4 + 6 == 10` |
| `-` | `15 - 10 == 5` |
| `*` | `2 * 3 == 6` |
| `/` | `13 / 3 == 4` |
| `%` | `13 % 3 == 1` |
For integer division, the remainder is dropped (e.g. `5 / 2 == 2`).
Go has shorthand assignment for the operators above (e.g. `a += 5` is short for `a = a + 5`).
Go also supports the increment and decrement statements `++` and `--` (e.g. `a++`).
## Converting between types
Converting between types is done via a function with the name of the type to convert to.
For example:
```go
var x int = 42 // x has type int
f := float64(x) // f has type float64 (ie. 42.0)
var y float64 = 11.9 // y has type float64
i := int(y) // i has type int (ie. 11)
```
## Arithmetic operations on different types
In many languages you can perform arithmetic operations on different types of variables, but in Go this gives an error.
For example:
```go
var x int = 42
// this line produces an error
value := float32(2.0) * x // invalid operation: mismatched types float32 and int
// you must convert int type to float32 before performing arithmetic operation
value := float32(2.0) * float32(x)
```
## Instructions
In this exercise you'll be writing code to analyze the production in a car factory.
## 1. Calculate the number of working cars produced per hour
The cars are produced on an assembly line.
The assembly line has a certain speed, that can be changed.
The faster the assembly line speed is, the more cars are produced.
However, changing the speed of the assembly line also changes the number of cars that are produced successfully, that is cars without any errors in their production.
Implement a function that takes in the number of cars produced per hour and the success rate and calculates the number of successful cars made per hour.
The success rate is given as a percentage, from `0` to `100`:
```go
CalculateWorkingCarsPerHour(1547, 90)
// => 1392.3
```
**Note:** the return value should be a `float64`.
## 2. Calculate the number of working cars produced per minute
Implement a function that takes in the number of cars produced per hour and the success rate and calculates how many cars are successfully produced each minute:
```go
CalculateWorkingCarsPerMinute(1105, 90)
// => 16
```
**Note:** the return value should be an `int`.
## 3. Calculate the cost of production
Each car normally costs $10,000 to produce individually, regardless of whether it is successful or not.
But with a bit of planning, 10 cars can be produced together for $95,000.
For example, 37 cars can be produced in the following way:
37 = 3 x groups of ten + 7 individual cars
So the cost for 37 cars is:
3\*95,000+7\*10,000=355,000
Implement the function `CalculateCost` that calculates the cost of producing a number of cars, regardless of whether they are successful:
```go
CalculateCost(37)
// => 355000
CalculateCost(21)
// => 200000
```
**Note:** the return value should be an `uint`.
## Source
### Created by
- @DavyJ0nes
- @kahgoh
### Contributed to by
- @tehsphinx
- @andrerfcsantos
+19
View File
@@ -0,0 +1,19 @@
package cars
// CalculateWorkingCarsPerHour calculates how many working cars are
// produced by the assembly line every hour.
func CalculateWorkingCarsPerHour(productionRate int, successRate float64) float64 {
return float64(productionRate) * successRate/100.0
}
// CalculateWorkingCarsPerMinute calculates how many working cars are
// produced by the assembly line every minute.
func CalculateWorkingCarsPerMinute(productionRate int, successRate float64) int {
return int(float64(productionRate)/60 * successRate/100)
}
// CalculateCost works out the cost of producing the given number of cars.
func CalculateCost(carsCount int) uint {
var numberOfBundles uint = uint(carsCount/10)
return (numberOfBundles*95000) + (uint(carsCount)-(numberOfBundles*10))*10000
}
+197
View File
@@ -0,0 +1,197 @@
package cars
import (
"math"
"testing"
)
const floatEqualityThreshold = 1e-9
func floatingPointEquals(got, want float64) bool {
absoluteDifferenceBelowThreshold := math.Abs(got-want) <= floatEqualityThreshold
relativeDifferenceBelowThreshold := math.Abs(got-want)/(math.Abs(got)+math.Abs(want)) <= floatEqualityThreshold
return absoluteDifferenceBelowThreshold || relativeDifferenceBelowThreshold
}
func TestCalculateWorkingCarsPerHour(t *testing.T) {
tests := []struct {
name string
productionRate int
successRate float64
want float64
}{
{
name: "calculate working cars per hour for production rate 0",
productionRate: 0,
successRate: 100,
want: 0.0,
},
{
name: "calculate working cars per hour for 100% success rate",
productionRate: 221,
successRate: 100,
want: 221.0,
},
{
name: "calculate working cars per hour for 80% success rate",
productionRate: 426,
successRate: 80,
want: 340.8,
},
{
name: "calculate working cars per hour for 20.5% success rate",
productionRate: 6824,
successRate: 20.5,
want: 1398.92,
},
{
name: "calculate working cars per hour for 0% success rate",
productionRate: 8000,
successRate: 0,
want: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := CalculateWorkingCarsPerHour(tc.productionRate, tc.successRate)
if !floatingPointEquals(got, tc.want) {
t.Errorf(
"CalculateWorkingCarsPerHour(%d, %f) = %f, want %f",
tc.productionRate,
tc.successRate,
got,
tc.want,
)
}
})
}
}
func TestCalculateWorkingCarsPerMinute(t *testing.T) {
tests := []struct {
name string
productionRate int
successRate float64
want int
}{
{
name: "calculate working cars per minute for production rate 0",
productionRate: 0,
successRate: 100,
want: 0,
},
{
name: "calculate working cars per minute for 100% success rate",
productionRate: 221,
successRate: 100,
want: 3,
},
{
name: "calculate working cars per minute for 80% success rate",
productionRate: 426,
successRate: 80,
want: 5,
},
{
name: "calculate working cars per minute for 20.5% success rate",
productionRate: 6824,
successRate: 20.5,
want: 23,
},
{
name: "calculate working cars per minute for 0% success rate",
productionRate: 8000,
successRate: 0,
want: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := CalculateWorkingCarsPerMinute(tc.productionRate, tc.successRate)
if got != tc.want {
t.Errorf(
"CalculateWorkingCarsPerMinute(%d, %f) = %d, want %d",
tc.productionRate,
tc.successRate,
got,
tc.want,
)
}
})
}
}
func TestCalculateCost(t *testing.T) {
tests := []struct {
name string
carsCount int
want uint
}{
{
name: "calculate cost to produce 0 cars",
carsCount: 0,
want: 0,
},
{
name: "calculate the cost of materials to produce 1 car",
carsCount: 1,
want: 10000,
},
{
name: "calculate cost to produce 2 cars",
carsCount: 2,
want: 20000,
},
{
name: "calculate cost to produce 9 cars",
carsCount: 9,
want: 90000,
},
{
name: "calculate cost to produce 10 cars",
carsCount: 10,
want: 95000,
},
{
name: "calculate cost to produce 100 cars",
carsCount: 100,
want: 950000,
},
{
name: "calculate cost to produce 21 cars",
carsCount: 21,
want: 200000,
},
{
name: "calculate cost to produce 37 cars",
carsCount: 37,
want: 355000,
},
{
name: "calculate cost to produce 56 cars",
carsCount: 56,
want: 535000,
},
{
name: "calculate cost to produce 148 cars",
carsCount: 148,
want: 1410000,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := CalculateCost(tc.carsCount)
if got != tc.want {
t.Errorf(
"CalculateCost(%d) = %d, want %d",
tc.carsCount,
got,
tc.want,
)
}
})
}
}
+3
View File
@@ -0,0 +1,3 @@
module cars
go 1.26