-
-
Notifications
You must be signed in to change notification settings - Fork 22
Add book-store #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add book-store #213
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
exercises/practice/book-store/.docs/instructions.append.md
This file contains hidden or 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,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, | ||
| ``` |
This file contains hidden or 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,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. |
This file contains hidden or 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,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" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,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 | ||
| } | ||
| } |
This file contains hidden or 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,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 | ||
| (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))))) | ||
| ) | ||
| ) | ||
This file contains hidden or 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,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" |
This file contains hidden or 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 @@ | ||
| audit=false |
This file contains hidden or 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,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. |
This file contains hidden or 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,4 @@ | ||
| export default { | ||
| presets: ["@exercism/babel-preset-javascript"], | ||
| plugins: [], | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?