-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
517 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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 gross_store.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) | ||
- [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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- [Go by example map][gobyexample-map] | ||
- [Go maps in action][goblog-map] | ||
|
||
## 1. Store the unit of measurement in your program | ||
|
||
- To store the measurement in your program, you can use map literal, see [go blog about map][goblog-map] | ||
|
||
## 2. Create a new bill | ||
|
||
- To create a new bill, all you need to do is reinitialize the customer, see [go blog about map][goblog-map] | ||
|
||
## 3. Add item to the customer bill | ||
|
||
- To check whether the given unit of measurement is correct, you can test your measurement map for a key without retrieving a value, see [go blog about map][goblog-map] | ||
|
||
## 4. Remove item from the customer bill | ||
|
||
- To check whether the given item is in customer bill, you can test your measurement map for a key without retrieving a value, see [go blog about map][goblog-map] | ||
|
||
- To check whether the given unit of measurement is correct, you can test your measurement map for a key without retrieving a value, see [go blog about map][goblog-map] | ||
|
||
## 5. Return the number of specific item that is in the customer bill | ||
|
||
- To check whether the given item is in customer bill, you can test your measurement map for a key without retrieving a value, see [go blog about map][goblog-map] | ||
|
||
[gobyexample-map]: https://gobyexample.com/maps | ||
[goblog-map]: https://blog.golang.org/maps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# Gross Store | ||
|
||
Welcome to Gross Store 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 | ||
|
||
In Go, `map` is a built-in data type that maps keys to values. In other programming languages, you might be familiar with the concept of `map` as a dictionary, hash table, key/value store or an associative array. | ||
|
||
Syntactically, `map` looks like this: | ||
|
||
```go | ||
map[KeyType]ElementType | ||
``` | ||
|
||
It is also important to know that each key is unique, meaning that assigning the same key twice will overwrite the value of the corresponding key. | ||
|
||
To create a map, you can do: | ||
|
||
```go | ||
// With map literal | ||
foo := map[string]int{} | ||
``` | ||
|
||
or | ||
|
||
```go | ||
// or with make function | ||
foo := make(map[string]int) | ||
``` | ||
|
||
Here are some operations that you can do with a map | ||
|
||
```go | ||
// Add a value in a map with the `=` operator: | ||
foo["bar"] = 42 | ||
// Here we update the element of `bar` | ||
foo["bar"] = 73 | ||
// To retrieve a map value, you can use | ||
baz := foo["bar"] | ||
// To delete an item from a map, you can use | ||
delete(foo, "bar") | ||
``` | ||
|
||
If you try to retrieve the value for a key which does not exist in the map, it will return the zero value of the value type. | ||
This can confuse you, especially if the default value of your `ElementType` (for example, 0 for an int), is a valid value. | ||
To check whether a key exists in your map, you can use | ||
|
||
```go | ||
value, exists := foo["baz"] | ||
// If the key "baz" does not exist, | ||
// value: 0; exists: false | ||
``` | ||
|
||
## Instructions | ||
|
||
A friend of yours has an old wholesale store called **Gross Store**. | ||
The name comes from the quantity of the item that the store sell: it's all in [gross unit][gross-unit]. | ||
Your friend asked you to implement a point of sale (POS) system for his store. | ||
**First, you want to build a prototype for it.** | ||
**In your prototype, your system will only record the quantity.** | ||
Your friend gave you a list of measurements to help you: | ||
|
||
| Unit | Score | | ||
| ------------------ | ----- | | ||
| quarter_of_a_dozen | 3 | | ||
| half_of_a_dozen | 6 | | ||
| dozen | 12 | | ||
| small_gross | 120 | | ||
| gross | 144 | | ||
| great_gross | 1728 | | ||
|
||
## 1. Store the unit of measurement in your program | ||
|
||
In order to use the measurement, you need to store the measurement in your program. | ||
|
||
```go | ||
units := Units() | ||
fmt.Println(units) | ||
// Output: map[...] with entries like ("dozen": 12) | ||
``` | ||
|
||
## 2. Create a new customer bill | ||
|
||
You need to implement a function that create a new (empty) bill for the customer. | ||
|
||
```go | ||
bill := NewBill() | ||
fmt.Println(bill) | ||
// Output: map[] | ||
``` | ||
|
||
## 3. Add an item to the customer bill | ||
|
||
To implement this, you'll need to: | ||
|
||
- Return `false` if the given `unit` is not in the `units` map. | ||
- Otherwise add the item to the customer `bill`, indexed by the item name, then return `true`. | ||
- If the item is already present in the bill, increase its quantity by the amount that belongs to the provided `unit`. | ||
|
||
```go | ||
bill := NewBill() | ||
units := Units() | ||
ok := AddItem(bill, units, "carrot", "dozen") | ||
fmt.Println(ok) | ||
// Output: true (since dozen is a valid unit) | ||
``` | ||
|
||
> Note that the returned value is type `bool`. | ||
## 4. Remove an item from the customer bill | ||
|
||
To implement this, you'll need to: | ||
|
||
- Return `false` if the given item is **not** in the bill | ||
- Return `false` if the given `unit` is not in the `units` map. | ||
- Return `false` if the new quantity would be less than 0. | ||
- If the new quantity is 0, completely remove the item from the `bill` then return `true`. | ||
- Otherwise, reduce the quantity of the item and return `true`. | ||
|
||
```go | ||
bill := NewBill() | ||
units := Units() | ||
ok := RemoveItem(bill, units, "carrot", "dozen") | ||
fmt.Println(ok) | ||
// Output: false (because there are no carrots in the bill) | ||
``` | ||
|
||
> Note that the returned value is type `bool`. | ||
## 5. Return the quantity of a specific item that is in the customer bill | ||
|
||
To implement this, you'll need to: | ||
|
||
- Return `0` and `false` if the `item` is not in the bill. | ||
- Otherwise, return the quantity of the item in the `bill` and `true`. | ||
|
||
```go | ||
bill := map[string]int{"carrot": 12, "grapes": 3} | ||
qty, ok := GetItem(bill, "carrot") | ||
fmt.Println(qty) | ||
// Output: 12 | ||
fmt.Println(ok) | ||
// Output: true | ||
``` | ||
|
||
> Note that the returned value are types `int` and `bool`. | ||
[gross-unit]: https://en.wikipedia.org/wiki/Gross_(unit) | ||
|
||
## Source | ||
|
||
### Created by | ||
|
||
- @chocopowwwa | ||
|
||
### Contributed to by | ||
|
||
- @MiroslavGatsanoga |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module gross | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package gross | ||
|
||
// Units stores the Gross Store unit measurements. | ||
func Units() map[string]int { | ||
panic("Please implement the Units() function") | ||
} | ||
|
||
// NewBill creates a new bill. | ||
func NewBill() map[string]int { | ||
panic("Please implement the NewBill() function") | ||
} | ||
|
||
// AddItem adds an item to customer bill. | ||
func AddItem(bill, units map[string]int, item, unit string) bool { | ||
panic("Please implement the AddItem() function") | ||
} | ||
|
||
// RemoveItem removes an item from customer bill. | ||
func RemoveItem(bill, units map[string]int, item, unit string) bool { | ||
panic("Please implement the RemoveItem() function") | ||
} | ||
|
||
// GetItem returns the quantity of an item that the customer has in his/her bill. | ||
func GetItem(bill map[string]int, item string) (int, bool) { | ||
panic("Please implement the GetItem() function") | ||
} |
Oops, something went wrong.