From 0556f2694a1a8ab24f6640e33c2d510ad48e3b10 Mon Sep 17 00:00:00 2001 From: shayan javani <25373436+massivefermion@users.noreply.github.com> Date: Wed, 11 Oct 2023 07:46:39 +0330 Subject: [PATCH] added the book-store exercise --- config.json | 150 +++++++---------- .../practice/book-store/.docs/instructions.md | 61 +++++++ exercises/practice/book-store/.gitignore | 4 + .../practice/book-store/.meta/config.json | 23 +++ .../practice/book-store/.meta/example.gleam | 40 +++++ .../practice/book-store/.meta/tests.toml | 64 ++++++++ exercises/practice/book-store/gleam.toml | 11 ++ exercises/practice/book-store/manifest.toml | 25 +++ .../practice/book-store/src/book_store.gleam | 3 + .../book-store/test/book_store_test.gleam | 151 ++++++++++++++++++ 10 files changed, 438 insertions(+), 94 deletions(-) create mode 100644 exercises/practice/book-store/.docs/instructions.md create mode 100644 exercises/practice/book-store/.gitignore create mode 100644 exercises/practice/book-store/.meta/config.json create mode 100644 exercises/practice/book-store/.meta/example.gleam create mode 100644 exercises/practice/book-store/.meta/tests.toml create mode 100644 exercises/practice/book-store/gleam.toml create mode 100644 exercises/practice/book-store/manifest.toml create mode 100644 exercises/practice/book-store/src/book_store.gleam create mode 100644 exercises/practice/book-store/test/book_store_test.gleam diff --git a/config.json b/config.json index 0c6833a89..0b8d57459 100644 --- a/config.json +++ b/config.json @@ -16,7 +16,7 @@ "highlightjs_language": "gleam" }, "test_runner": { - "average_run_time": 3 + "average_run_time": 3.0 }, "files": { "solution": [ @@ -560,8 +560,7 @@ "name": "Forth", "uuid": "8176ac78-d4fd-475f-ac8b-e205ca7b3a77", "difficulty": 9, - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "strings", @@ -645,8 +644,7 @@ "slug": "protein-translation", "name": "Protein Translation", "uuid": "612395a5-238e-4be0-8ce0-4ac66f57056e", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1012,8 +1010,7 @@ "slug": "alphametics", "name": "Alphametics", "uuid": "5aef7d9d-3939-4014-8102-a7262fdde754", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "sets", @@ -1062,8 +1059,7 @@ "slug": "acronym", "name": "Acronym", "uuid": "21946b94-c304-4a10-a6b6-b9fd5bbe9966", - "practices": [ - ], + "practices": [], "prerequisites": [ "labelled-arguments", "regular-expressions", @@ -1204,8 +1200,7 @@ "slug": "prime-factors", "name": "Prime Factors", "uuid": "00d1a428-99c4-4175-bee2-e05fc034528f", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "lists", @@ -1218,8 +1213,7 @@ "slug": "run-length-encoding", "name": "Run-Length Encoding", "uuid": "22298714-cc36-49b7-8e70-cfb91ff42112", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "strings", @@ -1233,8 +1227,7 @@ "slug": "grains", "name": "Grains", "uuid": "b5b9be18-9141-4176-8f8c-3dd14d14bed5", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "case-expressions", @@ -1247,8 +1240,7 @@ "slug": "nucleotide-count", "name": "Nucleotide count", "uuid": "3b7e1bbf-08b2-4e0e-ba5d-5a887deb4817", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "results", @@ -1262,8 +1254,7 @@ "slug": "hamming", "name": "Hamming", "uuid": "64a40995-c380-4233-a7af-98498dbc5c2d", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "strings", @@ -1277,8 +1268,7 @@ "slug": "proverb", "name": "Proverb", "uuid": "d2288ce0-3ef4-4a79-8e21-bf5624b5ab27", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "strings", @@ -1290,8 +1280,7 @@ "slug": "high-scores", "name": "High Scores", "uuid": "af720091-1352-4e07-96d2-0868588601f2", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "ints", @@ -1303,8 +1292,7 @@ "slug": "allergies", "name": "Allergies", "uuid": "f829a1af-0eaf-4880-b1d8-cd2cb3753fb0", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "lists", @@ -1318,8 +1306,7 @@ "name": "Twelve Days", "uuid": "583b74dc-2c5c-48f8-85c2-c90623507682", "difficulty": 3, - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1331,8 +1318,7 @@ "slug": "etl", "name": "ETL", "uuid": "1655d9f6-6199-4462-85bd-a5da5f25d30a", - "practices": [ - ], + "practices": [], "prerequisites": [ "maps", "lists", @@ -1345,8 +1331,7 @@ "slug": "queen-attack", "name": "Queen Attack", "uuid": "9caa195e-2273-4096-9aee-26ab5658e7f7", - "practices": [ - ], + "practices": [], "prerequisites": [ "custom-types", "case-expressions", @@ -1359,8 +1344,7 @@ "slug": "sublist", "name": "Sublist", "uuid": "6d4933cc-0845-4992-93ee-54f79a79fc5a", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "case-expressions", @@ -1374,8 +1358,7 @@ "slug": "rotational-cipher", "name": "Rotational Cipher", "uuid": "2e7fdb4d-3e75-4dc2-9fb7-ee1a9e1950f9", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "ints", @@ -1387,8 +1370,7 @@ "slug": "change", "name": "Change", "uuid": "8dc8aceb-fe74-415c-bf4b-956840155272", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "results", @@ -1401,8 +1383,7 @@ "slug": "flatten-array", "name": "Flatten Array", "uuid": "efdf70a3-57e6-474a-9fc2-09f26bb609b6", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "custom-types", @@ -1415,8 +1396,7 @@ "slug": "all-your-base", "name": "All Your Base", "uuid": "e9332e6f-a7e5-4f30-825c-64af0a278a70", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "lists", @@ -1430,8 +1410,7 @@ "slug": "knapsack", "name": "Knapsack", "uuid": "6e889724-656e-4f0c-aba1-08f103490e9d", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "custom-types", @@ -1460,8 +1439,7 @@ "slug": "dnd-character", "name": "D&D Character", "uuid": "65d80b55-02f0-4d40-a530-79843553252f", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "custom-types", @@ -1473,8 +1451,7 @@ "slug": "isbn-verifier", "name": "ISBN Verifier", "uuid": "d12ac283-8ca4-4b40-919a-9588e1e52d50", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "ints", @@ -1504,8 +1481,7 @@ "slug": "atbash-cipher", "name": "Atbash Cipher", "uuid": "3c850415-92fa-4019-b776-aaab30675680", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1518,8 +1494,7 @@ "slug": "square-root", "name": "Square Root", "uuid": "700f44e5-13a3-4e97-aad5-d383e25f9f9b", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "bools", @@ -1547,8 +1522,7 @@ "slug": "simple-cipher", "name": "Simple Cipher", "uuid": "37dd415b-1d67-434d-8705-19295d1438b8", - "practices": [ - ], + "practices": [], "prerequisites": [ "iterators", "strings", @@ -1562,8 +1536,7 @@ "slug": "crypto-square", "name": "Crypto Square", "uuid": "5bf01e81-b30a-4210-b1ed-1edbf3d28ab1", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "lists", @@ -1576,8 +1549,7 @@ "slug": "affine-cipher", "name": "Affine Cipher", "uuid": "594b6c1a-0a4c-4463-9535-62056c71404e", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1593,8 +1565,7 @@ "slug": "house", "name": "House", "uuid": "21421905-a33c-4040-9f09-78726b5d1167", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1607,8 +1578,7 @@ "slug": "connect", "name": "Connect", "uuid": "89c6bf7a-5cd8-4254-b3e0-1457a5390b46", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "maps", @@ -1624,8 +1594,7 @@ "slug": "clock", "name": "Clock", "uuid": "ad9cc1bd-d99b-4f43-941c-8e09b43801af", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "strings", @@ -1638,8 +1607,7 @@ "slug": "killer-sudoku-helper", "name": "Killer Sudoku Helper", "uuid": "4c6ed017-a02b-4826-a111-ef9733b7d315", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "ints", @@ -1651,8 +1619,7 @@ "slug": "luhn", "name": "Luhn", "uuid": "a66bbefd-db5a-44d0-ab4b-73d871756f08", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1667,8 +1634,7 @@ "slug": "wordy", "name": "Wordy", "uuid": "5031033b-bd21-4563-ae18-59ad02f4c489", - "practices": [ - ], + "practices": [], "prerequisites": [ "results", "strings", @@ -1684,8 +1650,7 @@ "slug": "largest-series-product", "name": "Largest Series Product", "uuid": "34293b12-1d94-43d9-a9cb-27b8bff92d67", - "practices": [ - ], + "practices": [], "prerequisites": [ "case-expressions", "ints", @@ -1698,8 +1663,7 @@ "slug": "minesweeper", "name": "Minesweeper", "uuid": "abbeeb8f-6ec1-4f57-86fc-2e18b3a62e29", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1713,8 +1677,7 @@ "slug": "say", "name": "Say", "uuid": "753fff04-5a64-4c3f-afd2-beee7e27efe8", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "case-expressions", @@ -1728,8 +1691,7 @@ "slug": "saddle-points", "name": "Saddle Points", "uuid": "e1103441-9391-4760-829b-1ec412762fab", - "practices": [ - ], + "practices": [], "prerequisites": [ "custom-types", "lists", @@ -1742,18 +1704,15 @@ "slug": "secret-handshake", "name": "Secret Handshake", "uuid": "22461f35-684d-4f75-9399-d07a4f483bdd", - "practices": [ - ], - "prerequisites": [ - ], + "practices": [], + "prerequisites": [], "difficulty": 2 }, { "slug": "matrix", "name": "Matrix", "uuid": "6fc59193-bc31-40af-902a-b13ad926bbb3", - "practices": [ - ], + "practices": [], "prerequisites": [ "lists", "strings", @@ -1766,8 +1725,7 @@ "slug": "grade-school", "name": "Grade School", "uuid": "836dddea-2a02-434f-a886-c8e1c429268f", - "practices": [ - ], + "practices": [], "prerequisites": [ "maps", "results", @@ -1783,8 +1741,7 @@ "slug": "variable-length-quantity", "name": "Variable Length Quantity", "uuid": "f7e33664-ea30-4885-8961-4aa984a1341d", - "practices": [ - ], + "practices": [], "prerequisites": [ "bit-strings", "case-expressions", @@ -1798,8 +1755,7 @@ "slug": "ocr-numbers", "name": "OCR Numbers", "uuid": "0ac6ba02-b2c0-4c81-8ca0-31c49a340e23", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "lists", @@ -1813,8 +1769,7 @@ "slug": "pig-latin", "name": "Pig Latin", "uuid": "dbc455de-1845-49f9-b5b5-082350e12811", - "practices": [ - ], + "practices": [], "prerequisites": [ "strings", "case-expressions", @@ -1826,14 +1781,21 @@ "slug": "nth-prime", "name": "Nth Prime", "uuid": "fda0001e-51b5-4041-a08a-2cb7dfef0f1c", - "practices": [ - ], + "practices": [], "prerequisites": [ "ints", "iterators", "recursion" ], "difficulty": 3 + }, + { + "slug": "book-store", + "name": "TODO", + "uuid": "c251e29f-de8a-4e98-b005-f600fe3ae4f9", + "practices": [], + "prerequisites": [], + "difficulty": 5 } ] }, @@ -2068,4 +2030,4 @@ "used_for/scripts", "used_for/web_development" ] -} +} \ No newline at end of file diff --git a/exercises/practice/book-store/.docs/instructions.md b/exercises/practice/book-store/.docs/instructions.md new file mode 100644 index 000000000..54403f17b --- /dev/null +++ b/exercises/practice/book-store/.docs/instructions.md @@ -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. diff --git a/exercises/practice/book-store/.gitignore b/exercises/practice/book-store/.gitignore new file mode 100644 index 000000000..170cca981 --- /dev/null +++ b/exercises/practice/book-store/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +build +erl_crash.dump diff --git a/exercises/practice/book-store/.meta/config.json b/exercises/practice/book-store/.meta/config.json new file mode 100644 index 000000000..4c5cff5ec --- /dev/null +++ b/exercises/practice/book-store/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "massivefermion" + ], + "files": { + "solution": [ + "src/book_store.gleam" + ], + "test": [ + "test/book_store_test.gleam" + ], + "example": [ + ".meta/example.gleam" + ], + "invalidator": [ + "gleam.toml", + "manifest.toml" + ] + }, + "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" +} diff --git a/exercises/practice/book-store/.meta/example.gleam b/exercises/practice/book-store/.meta/example.gleam new file mode 100644 index 000000000..af83f7715 --- /dev/null +++ b/exercises/practice/book-store/.meta/example.gleam @@ -0,0 +1,40 @@ +import gleam/int +import gleam/list +import gleam/float + +pub fn lowest_price(books: List(Int)) -> Float { + books + |> list.fold( + [#(1, 0), #(2, 0), #(3, 0), #(4, 0), #(5, 0)], + fn(acc, book) { + let assert Ok(current) = list.key_find(acc, book) + list.key_set(acc, book, current + 1) + }, + ) + |> list.map(fn(item) { item.1 }) + |> list.sort(int.compare) + |> calculate(0.0) +} + +fn calculate(counts: List(Int), acc: Float) -> Float { + case counts { + [0, 0, 0, 0, a] -> acc +. int.to_float(a) *. 800.0 + + [0, 0, 0, a, b] -> calculate([0, 0, 0, a - 1, b - 1], acc +. 2.0 *. 760.0) + + [0, 0, a, b, c] -> + calculate([0, 0, a - 1, b - 1, c - 1], acc +. 3.0 *. 720.0) + + [0, a, b, c, d] -> + calculate([0, a - 1, b - 1, c - 1, d - 1], acc +. 4.0 *. 640.0) + + [a, b, c, d, e] -> + float.min( + calculate([a - 1, b - 1, c - 1, d - 1, e - 1], acc +. 5.0 *. 600.0), + calculate( + list.sort([a, b - 1, c - 1, d - 1, e - 1], int.compare), + acc +. 4.0 *. 640.0, + ), + ) + } +} diff --git a/exercises/practice/book-store/.meta/tests.toml b/exercises/practice/book-store/.meta/tests.toml new file mode 100644 index 000000000..4b7ce98be --- /dev/null +++ b/exercises/practice/book-store/.meta/tests.toml @@ -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" diff --git a/exercises/practice/book-store/gleam.toml b/exercises/practice/book-store/gleam.toml new file mode 100644 index 000000000..dc13bdb0c --- /dev/null +++ b/exercises/practice/book-store/gleam.toml @@ -0,0 +1,11 @@ +name = "book_store" +version = "0.1.0" + +[dependencies] +gleam_bitwise = "~> 1.3" +gleam_otp = "~> 0.7" +gleam_stdlib = "~> 0.31" +simplifile = "~> 0.1" + +[dev-dependencies] +exercism_test_runner = "~> 1.4" diff --git a/exercises/practice/book-store/manifest.toml b/exercises/practice/book-store/manifest.toml new file mode 100644 index 000000000..6000d0db9 --- /dev/null +++ b/exercises/practice/book-store/manifest.toml @@ -0,0 +1,25 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "exercism_test_runner", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "simplifile", "gleam_erlang", "gleam_json", "gap", "glance", "gleam_stdlib"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "336FBF790841C2DC25EB77B35E76A09EFDB9771D7D813E0FDBC71A50CB79711D" }, + { name = "gap", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_ansi"], otp_app = "gap", source = "hex", outer_checksum = "AF290C27B3FAE5FE64E1B7E9C70A9E29AA0F42429C0592D375770C1C51B79D36" }, + { name = "glance", version = "0.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "B646A08970990D9D7A103443C5CD46F9D4297BF05F188767777FCC14ADF395EA" }, + { name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" }, + { name = "gleam_community_ansi", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "6E4E0CF2B207C1A7FCD3C21AA43514D67BC7004F21F82045CDCCE6C727A14862" }, + { name = "gleam_community_colour", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "D27CE357ECB343929A8CEC3FBA0B499943A47F0EE1F589EE16AFC2DC21C61E5B" }, + { name = "gleam_erlang", version = "0.22.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "367D8B41A7A86809928ED1E7E55BFD0D46D7C4CF473440190F324AFA347109B4" }, + { name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" }, + { name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" }, + { name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" }, + { name = "glexer", version = "0.6.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "703D2347F5180B2BCEA4D258549B0D91DACD0905010892BAC46D04D913B84D1F" }, + { name = "simplifile", version = "0.1.14", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "10EA0207796F20488A3A166C50A189C9385333F3C9FAC187729DE7B9CE4ADDBC" }, + { name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" }, +] + +[requirements] +exercism_test_runner = { version = "~> 1.4" } +gleam_bitwise = { version = "~> 1.3" } +gleam_otp = { version = "~> 0.7" } +gleam_stdlib = { version = "~> 0.31" } +simplifile = { version = "~> 0.1" } diff --git a/exercises/practice/book-store/src/book_store.gleam b/exercises/practice/book-store/src/book_store.gleam new file mode 100644 index 000000000..d3198f19d --- /dev/null +++ b/exercises/practice/book-store/src/book_store.gleam @@ -0,0 +1,3 @@ +pub fn lowest_price(basket: List(Int)) { + todo +} diff --git a/exercises/practice/book-store/test/book_store_test.gleam b/exercises/practice/book-store/test/book_store_test.gleam new file mode 100644 index 000000000..cb1c329f3 --- /dev/null +++ b/exercises/practice/book-store/test/book_store_test.gleam @@ -0,0 +1,151 @@ +import exercism/test_runner +import exercism/should +import book_store + +pub fn main() { + test_runner.main() +} + +pub fn only_a_single_book_test() { + let input = [1] + let output = book_store.lowest_price(input) + let expected = 800.0 + output + |> should.equal(expected) +} + +pub fn two_of_the_same_book_test() { + let input = [2, 2] + let output = book_store.lowest_price(input) + let expected = 1600.0 + output + |> should.equal(expected) +} + +pub fn empty_basket_test() { + let input = [] + let output = book_store.lowest_price(input) + let expected = 0.0 + output + |> should.equal(expected) +} + +pub fn two_different_books_test() { + let input = [1, 2] + let output = book_store.lowest_price(input) + let expected = 1520.0 + output + |> should.equal(expected) +} + +pub fn three_different_books_test() { + let input = [1, 2, 3] + let output = book_store.lowest_price(input) + let expected = 2160.0 + output + |> should.equal(expected) +} + +pub fn four_different_books_test() { + let input = [1, 2, 3, 4] + let output = book_store.lowest_price(input) + let expected = 2560.0 + output + |> should.equal(expected) +} + +pub fn five_different_books_test() { + let input = [1, 2, 3, 4, 5] + let output = book_store.lowest_price(input) + let expected = 3000.0 + output + |> should.equal(expected) +} + +pub fn two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three_test() { + let input = [1, 1, 2, 2, 3, 3, 4, 5] + let output = book_store.lowest_price(input) + let expected = 5120.0 + output + |> should.equal(expected) +} + +pub fn two_groups_of_four_is_cheaper_than_groups_of_five_and_three_test() { + let input = [1, 1, 2, 3, 4, 4, 5, 5] + let output = book_store.lowest_price(input) + let expected = 5120.0 + output + |> should.equal(expected) +} + +pub fn group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three_test() { + let input = [1, 1, 2, 2, 3, 4] + let output = book_store.lowest_price(input) + let expected = 4080.0 + output + |> should.equal(expected) +} + +pub fn two_each_of_first_four_books_and_one_copy_each_of_rest_test() { + let input = [1, 1, 2, 2, 3, 3, 4, 4, 5] + let output = book_store.lowest_price(input) + let expected = 5560.0 + output + |> should.equal(expected) +} + +pub fn two_copies_of_each_book_test() { + let input = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] + let output = book_store.lowest_price(input) + let expected = 6000.0 + output + |> should.equal(expected) +} + +pub fn three_copies_of_first_book_and_two_each_of_remaining_test() { + let input = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1] + let output = book_store.lowest_price(input) + let expected = 6800.0 + output + |> should.equal(expected) +} + +pub fn three_each_of_first_two_books_and_two_each_of_remaining_books_test() { + let input = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2] + let output = book_store.lowest_price(input) + let expected = 7520.0 + output + |> should.equal(expected) +} + +pub fn four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three_test() { + let input = [1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5] + let output = book_store.lowest_price(input) + let expected = 10_240.0 + output + |> should.equal(expected) +} + +pub fn check_that_groups_of_four_are_created_properly_even_when_there_are_more_groups_of_three_than_groups_of_five_test() { + let input = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5] + let output = book_store.lowest_price(input) + let expected = 14_560.0 + output + |> should.equal(expected) +} + +pub fn one_group_of_one_and_four_is_cheaper_than_one_group_of_two_and_three_test() { + let input = [1, 1, 2, 3, 4] + let output = book_store.lowest_price(input) + let expected = 3360.0 + output + |> should.equal(expected) +} + +pub fn one_group_of_one_and_two_plus_three_groups_of_four_is_cheaper_than_one_group_of_each_size_test() { + let input = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5] + let output = book_store.lowest_price(input) + let expected = 10_000.0 + output + |> should.equal(expected) +}