diff --git a/config.json b/config.json index 7965fb8e60..93cf00ec18 100644 --- a/config.json +++ b/config.json @@ -2535,6 +2535,14 @@ "strings" ], "difficulty": 7 + }, + { + "slug": "pop-count", + "name": "Eliud's Eggs", + "uuid": "2a3ecf62-fb5d-4dac-8369-72e7976a8f57", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/leap/.docs/introduction.md b/exercises/practice/leap/.docs/introduction.md index 4ffd2da594..8017122d0d 100644 --- a/exercises/practice/leap/.docs/introduction.md +++ b/exercises/practice/leap/.docs/introduction.md @@ -11,6 +11,6 @@ Some examples: - 1900 was not a leap year as it's not divisible by 400. - 2000 was a leap year! -~~~~exercism/note +```exercism/note For a delightful, four-minute explanation of the whole phenomenon of leap years, check out [this YouTube video](https://www.youtube.com/watch?v=xX96xng7sAE). -~~~~ +``` diff --git a/exercises/practice/pop-count/.docs/instructions.md b/exercises/practice/pop-count/.docs/instructions.md new file mode 100644 index 0000000000..b0c2df593c --- /dev/null +++ b/exercises/practice/pop-count/.docs/instructions.md @@ -0,0 +1,8 @@ +# Instructions + +Your task is to count the number of 1 bits in the binary representation of a number. + +## Restrictions + +Keep your hands off that bit-count functionality provided by your standard library! +Solve this one yourself using other basic tools instead. diff --git a/exercises/practice/pop-count/.docs/introduction.md b/exercises/practice/pop-count/.docs/introduction.md new file mode 100644 index 0000000000..49eaffd8bc --- /dev/null +++ b/exercises/practice/pop-count/.docs/introduction.md @@ -0,0 +1,47 @@ +# Introduction + +Your friend Eliud inherited a farm from her grandma Tigist. +Her granny was an inventor and had a tendency to build things in an overly complicated manner. +The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up. + +Eliud is asking you to write a program that shows the actual number of eggs in the coop. + +The position information encoding is calculated as follows: + +1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot. +2. Convert the number from binary to decimal. +3. Show the result on the display. + +Example 1: + +```text +Chicken Coop: + _ _ _ _ _ _ _ +|E| |E|E| | |E| + +Resulting Binary: + 1 0 1 1 0 0 1 + +Decimal number on the display: +89 + +Actual eggs in the coop: +4 +``` + +Example 2: + +```text +Chicken Coop: + _ _ _ _ _ _ _ _ +| | | |E| | | | | + +Resulting Binary: + 0 0 0 1 0 0 0 0 + +Decimal number on the display: +16 + +Actual eggs in the coop: +1 +``` diff --git a/exercises/practice/pop-count/.eslintrc b/exercises/practice/pop-count/.eslintrc new file mode 100644 index 0000000000..1d4446029c --- /dev/null +++ b/exercises/practice/pop-count/.eslintrc @@ -0,0 +1,14 @@ +{ + "root": true, + "extends": "@exercism/eslint-config-javascript", + "env": { + "jest": true + }, + "overrides": [ + { + "files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"], + "excludedFiles": ["custom.spec.js"], + "extends": "@exercism/eslint-config-javascript/maintainers" + } + ] +} diff --git a/exercises/practice/pop-count/.gitignore b/exercises/practice/pop-count/.gitignore new file mode 100644 index 0000000000..31c57dd53a --- /dev/null +++ b/exercises/practice/pop-count/.gitignore @@ -0,0 +1,5 @@ +/node_modules +/bin/configlet +/bin/configlet.exe +/pnpm-lock.yaml +/yarn.lock diff --git a/exercises/practice/pop-count/.meta/config.json b/exercises/practice/pop-count/.meta/config.json new file mode 100644 index 0000000000..5f80e7bf45 --- /dev/null +++ b/exercises/practice/pop-count/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "Cool-Katt" + ], + "files": { + "solution": [ + "pop-count.js" + ], + "test": [ + "pop-count.spec.js" + ], + "example": [ + ".meta/proof.ci.js" + ] + }, + "blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.", + "source": "Christian Willner, Eric Willigers", + "source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5" +} diff --git a/exercises/practice/pop-count/.meta/proof.ci.js b/exercises/practice/pop-count/.meta/proof.ci.js new file mode 100644 index 0000000000..ad767f867f --- /dev/null +++ b/exercises/practice/pop-count/.meta/proof.ci.js @@ -0,0 +1,5 @@ +export const eggCount = (displayValue) => + [...displayValue.toString(2)].reduce( + (acc, curr) => (curr === '1' ? (acc += 1) : acc), + 0, + ); diff --git a/exercises/practice/pop-count/.meta/tests.toml b/exercises/practice/pop-count/.meta/tests.toml new file mode 100644 index 0000000000..e11683c2ef --- /dev/null +++ b/exercises/practice/pop-count/.meta/tests.toml @@ -0,0 +1,22 @@ +# 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. + +[559e789d-07d1-4422-9004-3b699f83bca3] +description = "0 eggs" + +[97223282-f71e-490c-92f0-b3ec9e275aba] +description = "1 egg" + +[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5] +description = "4 eggs" + +[0c18be92-a498-4ef2-bcbb-28ac4b06cb81] +description = "13 eggs" diff --git a/exercises/practice/pop-count/.npmrc b/exercises/practice/pop-count/.npmrc new file mode 100644 index 0000000000..d26df800bb --- /dev/null +++ b/exercises/practice/pop-count/.npmrc @@ -0,0 +1 @@ +audit=false diff --git a/exercises/practice/pop-count/LICENSE b/exercises/practice/pop-count/LICENSE new file mode 100644 index 0000000000..90e73be03b --- /dev/null +++ b/exercises/practice/pop-count/LICENSE @@ -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. diff --git a/exercises/practice/pop-count/babel.config.js b/exercises/practice/pop-count/babel.config.js new file mode 100644 index 0000000000..b781d5a667 --- /dev/null +++ b/exercises/practice/pop-count/babel.config.js @@ -0,0 +1,4 @@ +module.exports = { + presets: ['@exercism/babel-preset-javascript'], + plugins: [], +}; diff --git a/exercises/practice/pop-count/package.json b/exercises/practice/pop-count/package.json new file mode 100644 index 0000000000..3330397e03 --- /dev/null +++ b/exercises/practice/pop-count/package.json @@ -0,0 +1,33 @@ +{ + "name": "@exercism/javascript-pop-count", + "description": "Exercism practice exercise on pop-count", + "author": "Katrina Owen", + "contributors": [ + "Derk-Jan Karrenbeld (https://derk-jan.com)", + "Tejas Bubane (https://tejasbubane.github.io/)" + ], + "private": true, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/exercism/javascript", + "directory": "exercises/practice/pop-count" + }, + "devDependencies": { + "@babel/core": "^7.23.0", + "@exercism/babel-preset-javascript": "^0.2.1", + "@exercism/eslint-config-javascript": "^0.6.0", + "@types/jest": "^29.5.4", + "@types/node": "^20.5.6", + "babel-jest": "^29.6.4", + "core-js": "~3.32.2", + "eslint": "^8.49.0", + "jest": "^29.7.0" + }, + "dependencies": {}, + "scripts": { + "test": "jest ./*", + "watch": "jest --watch ./*", + "lint": "eslint ." + } +} diff --git a/exercises/practice/pop-count/pop-count.js b/exercises/practice/pop-count/pop-count.js new file mode 100644 index 0000000000..dba85fb0bc --- /dev/null +++ b/exercises/practice/pop-count/pop-count.js @@ -0,0 +1,8 @@ +// +// This is only a SKELETON file for the 'Poker' exercise. It's been provided as a +// convenience to get you started writing code faster. +// + +export const eggCount = (displayValue) => { + throw new Error('Remove this statement and implement this function'); +}; diff --git a/exercises/practice/pop-count/pop-count.spec.js b/exercises/practice/pop-count/pop-count.spec.js new file mode 100644 index 0000000000..b8019c20da --- /dev/null +++ b/exercises/practice/pop-count/pop-count.spec.js @@ -0,0 +1,27 @@ +import { eggCount } from './pop-count'; + +describe('PopCount', () => { + test('0 eggs', () => { + const expected = 0; + const actual = eggCount(0); + expect(actual).toEqual(expected); + }); + + xtest('1 egg', () => { + const expected = 1; + const actual = eggCount(16); + expect(actual).toEqual(expected); + }); + + xtest('4 eggs', () => { + const expected = 4; + const actual = eggCount(89); + expect(actual).toEqual(expected); + }); + + xtest('13 eggs', () => { + const expected = 13; + const actual = eggCount(2000000000); + expect(actual).toEqual(expected); + }); +});