Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "book-store",
"name": "Book Store",
"uuid": "8fe40b8b-d6f4-46a7-83cc-29bac3494858",
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "meetup",
"name": "Meetup",
Expand Down
23 changes: 23 additions & 0 deletions exercises/practice/book-store/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Instruction append

## WebAssembly-specific Notes

The function signature for the WebAssembly export `total` is as follows:

```wasm
(func (export "total")
(param $basketOffset i32)
(param $basketLength i32)
(result i32)
)
```

The two parameters `$basketOffset` and `$basketLength` express the base offset and length of an array of 32-bit integers. The length parameter is sized in number of elements in the array, not bytes. Prior to calling this function, the caller writes this array into the WebAssembly linear memory beginning at offset `$basketOffset`. WebAssembly linear memory is always expressed in little-endian.

For example, the caller would encode the basket `[1,2]` as the following eight byte sequence.

```
| 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
| --- basket[0] --- | --- basket[1] --- |
,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
```
61 changes: 61 additions & 0 deletions exercises/practice/book-store/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Instructions

To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts on multiple book purchases.

One copy of any of the five books costs $8.

If, however, you buy two different books, you get a 5% discount on those two books.

If you buy 3 different books, you get a 10% discount.

If you buy 4 different books, you get a 20% discount.

If you buy all 5, you get a 25% discount.

Note that if you buy four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs $8.

Your mission is to write code to calculate the price of any conceivable shopping basket (containing only books of the same series), giving as big a discount as possible.

For example, how much does this basket of books cost?

- 2 copies of the first book
- 2 copies of the second book
- 2 copies of the third book
- 1 copy of the fourth book
- 1 copy of the fifth book

One way of grouping these 8 books is:

- 1 group of 5 (1st, 2nd,3rd, 4th, 5th)
- 1 group of 3 (1st, 2nd, 3rd)

This would give a total of:

- 5 books at a 25% discount
- 3 books at a 10% discount

Resulting in:

- 5 × (100% - 25%) × $8 = 5 × $6.00 = $30.00, plus
- 3 × (100% - 10%) × $8 = 3 × $7.20 = $21.60

Which equals $51.60.

However, a different way to group these 8 books is:

- 1 group of 4 books (1st, 2nd, 3rd, 4th)
- 1 group of 4 books (1st, 2nd, 3rd, 5th)

This would give a total of:

- 4 books at a 20% discount
- 4 books at a 20% discount

Resulting in:

- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60, plus
- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60

Which equals $51.20.

And $51.20 is the price with the biggest discount.
18 changes: 18 additions & 0 deletions exercises/practice/book-store/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [
"*.spec.js"
],
"excludedFiles": [
"custom.spec.js"
],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
28 changes: 28 additions & 0 deletions exercises/practice/book-store/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"book-store.wat"
],
"test": [
"book-store.spec.js"
],
"example": [
".meta/proof.ci.wat"
],
"invalidator": [
"package.json"
]
},
"blurb": "To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts of multiple-book purchases.",
"source": "Inspired by the harry potter kata from Cyber-Dojo.",
"source_url": "https://cyber-dojo.org",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
143 changes: 143 additions & 0 deletions exercises/practice/book-store/.meta/proof.ci.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
(module
(memory (export "mem") 1)

(global $tableOffset i32 (i32.const 0))

;;
;; Calculate the price of shopping basket of books
;;
;; @param {i32} basketOffset - offset of input u32[] array
;; @param {i32} basketLength - length of input u32[] array in elements
;;
;; @return {i32} - price of shopping basket
;;
(func (export "total") (param $basketOffset i32) (param $basketLength i32) (result i32)
(local $stop i32)
(local $inputPtr i32)
(local $tablePtr i32)
(local $i i32)
(local $j i32)
(local $tableI i32)
(local $tableJ i32)
(local $one i32)
(local $two i32)
(local $three i32)
(local $four i32)
(local $five i32)
(local $adjustment i32)

(local.set $stop (i32.add (local.get $basketOffset)
(i32.mul (local.get $basketLength)
(i32.const 4))))
(local.set $inputPtr (local.get $basketOffset))

(local.set $i (i32.const 0))
(loop $zero
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use memory.fill to zero out the array?

(if (i32.lt_u (local.get $i) (i32.const 20)) (then
(i32.store (i32.add (global.get $tableOffset)
(local.get $i)) (i32.const 0))
(local.set $i (i32.add (local.get $i)
(i32.const 4)))
(br $zero)
))
)

(loop $read
(if (i32.ne (local.get $inputPtr) (local.get $stop)) (then
(local.set $tablePtr (i32.add (global.get $tableOffset)
(i32.mul (i32.sub (i32.load (local.get $inputPtr))
(i32.const 1))
(i32.const 4))))
(i32.store (local.get $tablePtr) (i32.add (i32.load (local.get $tablePtr))
(i32.const 1)))
(local.set $inputPtr (i32.add (local.get $inputPtr)
(i32.const 4)))
(br $read)
))
)

(local.set $i (i32.const 4))
(loop $sortOuter
(if (i32.lt_u (local.get $i) (i32.const 20)) (then
(local.set $tableI (i32.load (i32.add (global.get $tableOffset)
(local.get $i))))
(local.set $j (local.get $i))

(loop $sortInner
(local.set $j (i32.sub (local.get $j)
(i32.const 4)))
(if (i32.ge_s (local.get $j) (i32.const 0)) (then
(local.set $tableJ (i32.load (i32.add (global.get $tableOffset)
(local.get $j))))
(if (i32.ge_u (local.get $tableJ) (local.get $tableI)) (then
(i32.store (i32.add (global.get $tableOffset)
(i32.add (local.get $j)
(i32.const 4)))
(local.get $tableJ))
(br $sortInner)
))
))
)

(i32.store (i32.add (global.get $tableOffset)
(i32.add (local.get $j)
(i32.const 4)))
(local.get $tableI))

(local.set $i (i32.add (local.get $i)
(i32.const 4)))
(br $sortOuter)
))
)

(local.set $five (i32.load (i32.add (global.get $tableOffset)
(i32.const 0))))
(local.set $four (i32.load (i32.add (global.get $tableOffset)
(i32.const 4))))
(local.set $three (i32.load (i32.add (global.get $tableOffset)
(i32.const 8))))
(local.set $two (i32.load (i32.add (global.get $tableOffset)
(i32.const 12))))
(local.set $one (i32.load (i32.add (global.get $tableOffset)
(i32.const 16))))

(local.set $one (i32.sub (local.get $one)
(local.get $two)))
(local.set $two (i32.sub (local.get $two)
(local.get $three)))
(local.set $three (i32.sub (local.get $three)
(local.get $four)))
(local.set $four (i32.sub (local.get $four)
(local.get $five)))

(local.set $adjustment (local.get $three))
(if (i32.lt_u (local.get $five) (local.get $three)) (then
(local.set $adjustment (local.get $five))
))

(local.set $three (i32.sub (local.get $three)
(local.get $adjustment)))
(local.set $five (i32.sub (local.get $five)
(local.get $adjustment)))
(local.set $four (i32.add (local.get $four)
(i32.mul (local.get $adjustment)
(i32.const 2))))

(local.set $one (i32.mul (local.get $one)
(i32.const 800)))
(local.set $two (i32.mul (local.get $two)
(i32.const 1520)))
(local.set $three (i32.mul (local.get $three)
(i32.const 2160)))
(local.set $four (i32.mul (local.get $four)
(i32.const 2560)))
(local.set $five (i32.mul (local.get $five)
(i32.const 3000)))

(return (i32.add (local.get $one)
(i32.add (i32.add (local.get $two)
(local.get $three))
(i32.add (local.get $four)
(local.get $five)))))
)
)
64 changes: 64 additions & 0 deletions exercises/practice/book-store/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[17146bd5-2e80-4557-ab4c-05632b6b0d01]
description = "Only a single book"

[cc2de9ac-ff2a-4efd-b7c7-bfe0f43271ce]
description = "Two of the same book"

[5a86eac0-45d2-46aa-bbf0-266b94393a1a]
description = "Empty basket"

[158bd19a-3db4-4468-ae85-e0638a688990]
description = "Two different books"

[f3833f6b-9332-4a1f-ad98-6c3f8e30e163]
description = "Three different books"

[1951a1db-2fb6-4cd1-a69a-f691b6dd30a2]
description = "Four different books"

[d70f6682-3019-4c3f-aede-83c6a8c647a3]
description = "Five different books"

[78cacb57-911a-45f1-be52-2a5bd428c634]
description = "Two groups of four is cheaper than group of five plus group of three"

[f808b5a4-e01f-4c0d-881f-f7b90d9739da]
description = "Two groups of four is cheaper than groups of five and three"

[fe96401c-5268-4be2-9d9e-19b76478007c]
description = "Group of four plus group of two is cheaper than two groups of three"

[68ea9b78-10ad-420e-a766-836a501d3633]
description = "Two each of first four books and one copy each of rest"

[c0a779d5-a40c-47ae-9828-a340e936b866]
description = "Two copies of each book"

[18fd86fe-08f1-4b68-969b-392b8af20513]
description = "Three copies of first book and two each of remaining"

[0b19a24d-e4cf-4ec8-9db2-8899a41af0da]
description = "Three each of first two books and two each of remaining books"

[bb376344-4fb2-49ab-ab85-e38d8354a58d]
description = "Four groups of four are cheaper than two groups each of five and three"

[5260ddde-2703-4915-b45a-e54dbbac4303]
description = "Check that groups of four are created properly even when there are more groups of three than groups of five"

[b0478278-c551-4747-b0fc-7e0be3158b1f]
description = "One group of one and four is cheaper than one group of two and three"

[cf868453-6484-4ae1-9dfc-f8ee85bbde01]
description = "One group of one and two plus three groups of four is cheaper than one group of each size"
1 change: 1 addition & 0 deletions exercises/practice/book-store/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/book-store/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/book-store/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
presets: ["@exercism/babel-preset-javascript"],
plugins: [],
};
Loading