Skip to content

Commit 047aaaf

Browse files
Pop count (#1420)
* pop-count added * lint and format
1 parent 50cb885 commit 047aaaf

File tree

16 files changed

+1158
-0
lines changed

16 files changed

+1158
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,14 @@
10391039
"difficulty": 3,
10401040
"topics": ["classes", "conditionals"]
10411041
},
1042+
{
1043+
"slug": "pop-count",
1044+
"name": "Eliud's Eggs",
1045+
"uuid": "2cdf947c-34ed-4ecf-92dd-4c4e6c21f9f3",
1046+
"practices": [],
1047+
"prerequisites": [],
1048+
"difficulty": 3
1049+
},
10421050
{
10431051
"slug": "rotational-cipher",
10441052
"name": "Rotational Cipher",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Instructions
2+
3+
Your task is to count the number of 1 bits in the binary representation of a number.
4+
5+
## Restrictions
6+
7+
Keep your hands off that bit-count functionality provided by your standard library!
8+
Solve this one yourself using other basic tools instead.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Introduction
2+
3+
Your friend Eliud inherited a farm from her grandma Tigist.
4+
Her granny was an inventor and had a tendency to build things in an overly complicated manner.
5+
The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up.
6+
7+
Eliud is asking you to write a program that shows the actual number of eggs in the coop.
8+
9+
The position information encoding is calculated as follows:
10+
11+
1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
12+
2. Convert the number from binary to decimal.
13+
3. Show the result on the display.
14+
15+
Example 1:
16+
17+
```text
18+
Chicken Coop:
19+
_ _ _ _ _ _ _
20+
|E| |E|E| | |E|
21+
22+
Resulting Binary:
23+
1 0 1 1 0 0 1
24+
25+
Decimal number on the display:
26+
89
27+
28+
Actual eggs in the coop:
29+
4
30+
```
31+
32+
Example 2:
33+
34+
```text
35+
Chicken Coop:
36+
_ _ _ _ _ _ _ _
37+
| | | |E| | | | |
38+
39+
Resulting Binary:
40+
0 0 0 1 0 0 0 0
41+
42+
Decimal number on the display:
43+
16
44+
45+
Actual eggs in the coop:
46+
1
47+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
!.meta
2+
3+
# Protected or generated
4+
.git
5+
.vscode
6+
7+
# When using npm
8+
node_modules/*
9+
10+
# Configuration files
11+
.eslintrc.cjs
12+
babel.config.cjs
13+
jest.config.cjs
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
tsconfigRootDir: __dirname,
5+
project: ['./tsconfig.json'],
6+
},
7+
overrides: [
8+
// Student provided files
9+
{
10+
files: ['*.ts'],
11+
excludedFiles: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
12+
extends: '@exercism/eslint-config-typescript',
13+
},
14+
// Exercism given tests
15+
{
16+
files: ['*.test.ts'],
17+
excludedFiles: ['custom.test.ts'],
18+
env: {
19+
jest: true,
20+
},
21+
extends: '@exercism/eslint-config-typescript/maintainers',
22+
},
23+
// Student provided tests
24+
{
25+
files: ['custom.test.ts'],
26+
env: {
27+
jest: true,
28+
},
29+
extends: '@exercism/eslint-config-typescript',
30+
},
31+
// Exercism provided files
32+
{
33+
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
34+
excludedFiles: ['custom.test.ts'],
35+
extends: '@exercism/eslint-config-typescript/maintainers',
36+
},
37+
],
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"therealowenrees"
4+
],
5+
"files": {
6+
"solution": [
7+
"pop-count.ts"
8+
],
9+
"test": [
10+
"pop-count.test.ts"
11+
],
12+
"example": [
13+
".meta/proof.ci.ts"
14+
]
15+
},
16+
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
17+
"source": "Christian Willner, Eric Willigers",
18+
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const eggCount = (displayValue: number): number => {
2+
let count = 0
3+
4+
while (displayValue !== 0) {
5+
count += displayValue & 1
6+
displayValue >>= 1
7+
}
8+
9+
return count
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[559e789d-07d1-4422-9004-3b699f83bca3]
13+
description = "0 eggs"
14+
15+
[97223282-f71e-490c-92f0-b3ec9e275aba]
16+
description = "1 egg"
17+
18+
[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5]
19+
description = "4 eggs"
20+
21+
[0c18be92-a498-4ef2-bcbb-28ac4b06cb81]
22+
description = "13 eggs"

exercises/practice/pop-count/.yarn/releases/yarn-3.6.4.cjs

Lines changed: 874 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarnPath: .yarn/releases/yarn-3.6.4.cjs
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: ['@exercism/babel-preset-typescript'],
3+
plugins: [],
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
verbose: true,
3+
projects: ['<rootDir>'],
4+
testMatch: [
5+
'**/__tests__/**/*.[jt]s?(x)',
6+
'**/test/**/*.[jt]s?(x)',
7+
'**/?(*.)+(spec|test).[jt]s?(x)',
8+
],
9+
testPathIgnorePatterns: [
10+
'/(?:production_)?node_modules/',
11+
'.d.ts$',
12+
'<rootDir>/test/fixtures',
13+
'<rootDir>/test/helpers',
14+
'__mocks__',
15+
],
16+
transform: {
17+
'^.+\\.[jt]sx?$': 'babel-jest',
18+
},
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@exercism/typescript-pop-count",
3+
"version": "1.0.0",
4+
"description": "Exercism exercises in Typescript.",
5+
"private": true,
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/exercism/typescript"
9+
},
10+
"type": "module",
11+
"engines": {
12+
"node": "^18.16.0 || >=20.0.0"
13+
},
14+
"devDependencies": {
15+
"@exercism/babel-preset-typescript": "^0.4.0",
16+
"@exercism/eslint-config-typescript": "^0.5.0",
17+
"@types/jest": "^29.5.2",
18+
"@types/node": "~18.16.16",
19+
"babel-jest": "^29.5.0",
20+
"core-js": "~3.30.2",
21+
"eslint": "^8.42.0",
22+
"jest": "^29.5.0",
23+
"typescript": "~5.0.4"
24+
},
25+
"scripts": {
26+
"test": "yarn lint:types && jest --no-cache",
27+
"lint": "yarn lint:types && yarn lint:ci",
28+
"lint:types": "yarn tsc --noEmit -p .",
29+
"lint:ci": "eslint . --ext .tsx,.ts"
30+
},
31+
"packageManager": "[email protected]"
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { eggCount } from './pop-count'
2+
3+
describe('PopCount', () => {
4+
it('0 eggs', () => {
5+
const expected = 0
6+
const actual = eggCount(0)
7+
expect(actual).toEqual(expected)
8+
})
9+
10+
xit('1 egg', () => {
11+
const expected = 1
12+
const actual = eggCount(16)
13+
expect(actual).toEqual(expected)
14+
})
15+
16+
xit('4 eggs', () => {
17+
const expected = 4
18+
const actual = eggCount(89)
19+
expect(actual).toEqual(expected)
20+
})
21+
22+
xit('13 eggs', () => {
23+
const expected = 13
24+
const actual = eggCount(2000000000)
25+
expect(actual).toEqual(expected)
26+
})
27+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// This is only a SKELETON file for the 'Pop Count' exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
//
5+
6+
export const eggCount = (displayValue: unknown): unknown => {
7+
throw new Error('Remove this statement and implement this function')
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"display": "Configuration for Exercism TypeScript Exercises",
3+
"compilerOptions": {
4+
// Allows you to use the newest syntax, and have access to console.log
5+
// https://www.typescriptlang.org/tsconfig#lib
6+
"lib": ["ESNEXT", "dom"],
7+
// Make sure typescript is configured to output ESM
8+
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
9+
"module": "ES2020",
10+
// Since this project is using babel, TypeScript may target something very
11+
// high, and babel will make sure it runs on your local Node version.
12+
// https://babeljs.io/docs/en/
13+
"target": "ESNext", // ESLint doesn't support this yet: "es2022",
14+
15+
"strict": true,
16+
"esModuleInterop": true,
17+
"skipLibCheck": true,
18+
"forceConsistentCasingInFileNames": true,
19+
20+
// Because we'll be using babel: ensure that Babel can safely transpile
21+
// files in the TypeScript project.
22+
//
23+
// https://babeljs.io/docs/en/babel-plugin-transform-typescript/#caveats
24+
"isolatedModules": true
25+
},
26+
"include": ["*.ts", "*.tsx", ".meta/*.ts", ".meta/*.tsx"],
27+
"exclude": ["node_modules"]
28+
}

0 commit comments

Comments
 (0)