Skip to content
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

Pop count #1420

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -1039,6 +1039,14 @@
"difficulty": 3,
"topics": ["classes", "conditionals"]
},
{
"slug": "pop-count",
"name": "Eliud's Eggs",
"uuid": "2cdf947c-34ed-4ecf-92dd-4c4e6c21f9f3",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
Expand Down
8 changes: 8 additions & 0 deletions exercises/practice/pop-count/.docs/instructions.md
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.
47 changes: 47 additions & 0 deletions exercises/practice/pop-count/.docs/introduction.md
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
```
13 changes: 13 additions & 0 deletions exercises/practice/pop-count/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
!.meta

# Protected or generated
.git
.vscode

# When using npm
node_modules/*

# Configuration files
.eslintrc.cjs
babel.config.cjs
jest.config.cjs
38 changes: 38 additions & 0 deletions exercises/practice/pop-count/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = {
root: true,
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
overrides: [
// Student provided files
{
files: ['*.ts'],
excludedFiles: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
extends: '@exercism/eslint-config-typescript',
},
// Exercism given tests
{
files: ['*.test.ts'],
excludedFiles: ['custom.test.ts'],
env: {
jest: true,
},
extends: '@exercism/eslint-config-typescript/maintainers',
},
// Student provided tests
{
files: ['custom.test.ts'],
env: {
jest: true,
},
extends: '@exercism/eslint-config-typescript',
},
// Exercism provided files
{
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
excludedFiles: ['custom.test.ts'],
extends: '@exercism/eslint-config-typescript/maintainers',
},
],
}
19 changes: 19 additions & 0 deletions exercises/practice/pop-count/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"therealowenrees"
],
"files": {
"solution": [
"pop-count.ts"
],
"test": [
"pop-count.test.ts"
],
"example": [
".meta/proof.ci.ts"
]
},
"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"
}
10 changes: 10 additions & 0 deletions exercises/practice/pop-count/.meta/proof.ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const eggCount = (displayValue: number): number => {
let count = 0

while (displayValue !== 0) {
count += displayValue & 1
displayValue >>= 1
}

return count
}
22 changes: 22 additions & 0 deletions exercises/practice/pop-count/.meta/tests.toml
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"
874 changes: 874 additions & 0 deletions exercises/practice/pop-count/.yarn/releases/yarn-3.6.4.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions exercises/practice/pop-count/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-3.6.4.cjs
4 changes: 4 additions & 0 deletions exercises/practice/pop-count/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: ['@exercism/babel-preset-typescript'],
plugins: [],
}
19 changes: 19 additions & 0 deletions exercises/practice/pop-count/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
}
32 changes: 32 additions & 0 deletions exercises/practice/pop-count/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@exercism/typescript-pop-count",
"version": "1.0.0",
"description": "Exercism exercises in Typescript.",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/typescript"
},
"type": "module",
"engines": {
"node": "^18.16.0 || >=20.0.0"
},
"devDependencies": {
"@exercism/babel-preset-typescript": "^0.4.0",
"@exercism/eslint-config-typescript": "^0.5.0",
"@types/jest": "^29.5.2",
"@types/node": "~18.16.16",
"babel-jest": "^29.5.0",
"core-js": "~3.30.2",
"eslint": "^8.42.0",
"jest": "^29.5.0",
"typescript": "~5.0.4"
},
"scripts": {
"test": "yarn lint:types && jest --no-cache",
"lint": "yarn lint:types && yarn lint:ci",
"lint:types": "yarn tsc --noEmit -p .",
"lint:ci": "eslint . --ext .tsx,.ts"
},
"packageManager": "yarn@3.6.4"
}
27 changes: 27 additions & 0 deletions exercises/practice/pop-count/pop-count.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { eggCount } from './pop-count'

describe('PopCount', () => {
it('0 eggs', () => {
const expected = 0
const actual = eggCount(0)
expect(actual).toEqual(expected)
})

xit('1 egg', () => {
const expected = 1
const actual = eggCount(16)
expect(actual).toEqual(expected)
})

xit('4 eggs', () => {
const expected = 4
const actual = eggCount(89)
expect(actual).toEqual(expected)
})

xit('13 eggs', () => {
const expected = 13
const actual = eggCount(2000000000)
expect(actual).toEqual(expected)
})
})
8 changes: 8 additions & 0 deletions exercises/practice/pop-count/pop-count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// This is only a SKELETON file for the 'Pop Count' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const eggCount = (displayValue: unknown): unknown => {
throw new Error('Remove this statement and implement this function')
}
28 changes: 28 additions & 0 deletions exercises/practice/pop-count/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"display": "Configuration for Exercism TypeScript Exercises",
"compilerOptions": {
// Allows you to use the newest syntax, and have access to console.log
// https://www.typescriptlang.org/tsconfig#lib
"lib": ["ESNEXT", "dom"],
// Make sure typescript is configured to output ESM
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
"module": "ES2020",
// Since this project is using babel, TypeScript may target something very
// high, and babel will make sure it runs on your local Node version.
// https://babeljs.io/docs/en/
"target": "ESNext", // ESLint doesn't support this yet: "es2022",

"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,

// Because we'll be using babel: ensure that Babel can safely transpile
// files in the TypeScript project.
//
// https://babeljs.io/docs/en/babel-plugin-transform-typescript/#caveats
"isolatedModules": true
},
"include": ["*.ts", "*.tsx", ".meta/*.ts", ".meta/*.tsx"],
"exclude": ["node_modules"]
}
Loading