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

[jbchr] BarcodeValidator | BerlinClock | LangtonAnt #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,6 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# MacOs
.DS_Store
1 change: 1 addition & 0 deletions katas/BarcodeValidator/solutions/jbchr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.pytest_cache
31 changes: 31 additions & 0 deletions katas/BarcodeValidator/solutions/jbchr/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Janek's Barcode validator

I used Python with for this project as I had to choose one unfamiliar programming language and it seemed easy to setup.

## Installation

You need to have installed python (v3) and pip to run the project.

Then install dependencies:

```
pip install -r requirements.txt
```

## Run the validator

```
python3 src/barcode_validator.py 28170280
```

## Run tests

```
python3 -m unittest tests/test_barcode_validator.py
```

or in watch mode:

```
ptw
```
12 changes: 12 additions & 0 deletions katas/BarcodeValidator/solutions/jbchr/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
attrs==22.2.0
colorama==0.4.6
docopt==0.6.2
exceptiongroup==1.1.0
iniconfig==2.0.0
packaging==23.0
pluggy==1.0.0
pytest==7.2.1
pytest-watch==4.2.0
pywatchman==1.4.1
tomli==2.0.1
watchdog==2.2.1
Empty file.
24 changes: 24 additions & 0 deletions katas/BarcodeValidator/solutions/jbchr/src/barcode_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import math
import sys

def validate(code):
# Cases: no numbers, not length 8 or 12
if not code.isnumeric() or not (len(code) != 8 or len(code) != 12):
return False

# build product checksum
checksum_product = 0
for i in range(0, len(code) - 1):
multiplier = 3 if i % 2 == 0 else 1
checksum_product = checksum_product + multiplier * int(code[i])

# build checksum
checksum = math.ceil(checksum_product / 10) * 10 - checksum_product

#return result
return checksum == int(code[len(code) - 1])


# CLI
print(validate(sys.argv[1]))

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from src.barcode_validator import *
import unittest
class TestValidate(unittest.TestCase):
def test_ean_8(self):
self.assertEqual(validate("40170725"), True)
self.assertEqual(validate("28170280"), True)

def test_ean_8_bad_checksum(self):
self.assertEqual(validate("40170726"), False)

def test_gtin(self):
self.assertEqual(validate("4912345678904"), True)

def test_gtin_bad_checksum(self):
self.assertEqual(validate("4912345678901"), False)

def test_bad_cases(self):
self.assertEqual(validate("1"), False)
self.assertEqual(validate(""), False)
self.assertEqual(validate("AAAAAAAA"), False)
self.assertEqual(validate("AAAAAAAACBDF"), False)

if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions katas/BerlinClock/solutions/jbchr/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions katas/BerlinClock/solutions/jbchr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
35 changes: 35 additions & 0 deletions katas/BerlinClock/solutions/jbchr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Janek's Berlin Clock

![alt text](/docs/images/preview.png)

I choose react and three.js for this project because I don't have much experience in it so I wanted to try it. I chose Next.js because the kata's description was asking for a server and this was an easy way to expose an api route. Honestly, I read over the description that the kata should follow TDD, so this application was not developed test-driven.

The client will query the server every second about the current berlin clock time. In a real application this computation might done on the client as it is inexpensive. Furthermore, the request might be delayed leading in the clock not "blinking" regularily but in different intervals.

## Tech Stack

- Next.js / React
- React Three Fiber / Three.js

## Usage

First install dependencies:

```
npm install
```

then start dev server

```
npm run dev
```

The website will be served on http://localhost:3000 and the server's only api route is available on http://localhost:3000/api/berlin-clock-time

## Notes / Todo / Improvements

The following things should be implemented in the future:

- Server should return time in the client's timezone
- Error handling, when server response is invalid, or server failed to answer
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions katas/BerlinClock/solutions/jbchr/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// jest.config.js
const nextJest = require('next/jest')

const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})

// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const customJestConfig = {
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
}

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)
6 changes: 6 additions & 0 deletions katas/BerlinClock/solutions/jbchr/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}

module.exports = nextConfig
Loading