-
-
Notifications
You must be signed in to change notification settings - Fork 654
[New practice exercise] Pop count #2373
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /node_modules | ||
| /bin/configlet | ||
| /bin/configlet.exe | ||
| /pnpm-lock.yaml | ||
| /yarn.lock | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const eggCount = (displayValue) => | ||
| [...displayValue.toString(2)].reduce( | ||
| (acc, curr) => (curr === '1' ? (acc += 1) : acc), | ||
| 0, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| audit=false | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also defined in the root and I suspect isn't needed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another auto-generated file. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Licensing is done on a repo level, not an exercise level.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yet another auto-generated file. |
||
|
|
||
| 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. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module.exports = { | ||
| presets: ['@exercism/babel-preset-javascript'], | ||
| plugins: [], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "@exercism/javascript-pop-count", | ||
| "description": "Exercism practice exercise on pop-count", | ||
| "author": "Katrina Owen", | ||
| "contributors": [ | ||
| "Derk-Jan Karrenbeld <derk-jan+git@karrenbeld.info> (https://derk-jan.com)", | ||
| "Tejas Bubane (https://tejasbubane.github.io/)" | ||
| ], | ||
|
Comment on lines
+4
to
+8
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Cool-Katt Do you want to update these?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean to add myself as a contributor? I don't know if I properly understand what you mean.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Or as the author. The only person that worked on this exercise is you so I think it ought to be reflected in the author/contributors docs. |
||
| "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 ." | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| }); | ||
| }); |
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.
Was this file auto-created? It looks ... wrong. These ought to be in the root ignore file.
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.
This was an auto-generated file.