Skip to content

Commit

Permalink
added reason(able) solutions.
Browse files Browse the repository at this point in the history
  • Loading branch information
shrynx committed Nov 24, 2017
0 parents commit 6b79be4
Show file tree
Hide file tree
Showing 88 changed files with 2,391 additions and 0 deletions.
112 changes: 112 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Environment
.env
.env.dev

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# build files
build
lib
es
dist

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# General
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk
9 changes: 9 additions & 0 deletions .merlin
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
####{BSB GENERATED: NO EDIT
FLG -ppx /Users/shriyans/Projects/reason/99.re/node_modules/bs-platform/bin/bsppx.exe
S /Users/shriyans/Projects/reason/99.re/node_modules/bs-platform/lib/ocaml
B /Users/shriyans/Projects/reason/99.re/node_modules/bs-platform/lib/ocaml
FLG -nostdlib -no-alias-deps -color always
FLG -w -30-40+6+7+27+32..39+44+45+101
S src
B lib/bs/src
####BSB GENERATED: NO EDIT}
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# 99 Problems

Solutions to 99 Problems implemented in [Reason](https://reasonml.github.io/).

[ninety-nine lisp problems]: http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html

## Status

Currently in progress.

### Working with lists

| No. | Description | Solution | | No. | Description | Solution |
| --- | -------------------- | ----------------------- | --- | --- | ------------------ | ----------------------- |
| 01 | Last element | [completed](src/p01.re) | | 15 | Replicate | [completed](src/p15.re) |
| 02 | Last two elements | [completed](src/p02.re) | | 16 | Drop every N'th | [completed](src/p16.re) |
| 03 | K'th element | [completed](src/p03.re) | | 17 | Split | [completed](src/p17.re) |
| 04 | Length | [completed](src/p04.re) | | 18 | Slice | [completed](src/p18.re) |
| 05 | Reverse | [completed](src/p05.re) | | 19 | Rotate | [completed](src/p19.re) |
| 06 | Palindrome | [completed](src/p06.re) | | 20 | Remove | [completed](src/p20.re) |
| 07 | Flatten | [completed](src/p07.re) | | 21 | Insert | [completed](src/p21.re) |
| 08 | Eliminate duplicates | [completed](src/p08.re) | | 22 | Range | [completed](src/p22.re) |
| 09 | Pack duplicates | [completed](src/p09.re) | | 23 | Random select | [completed](src/p23.re) |
| 10 | Run-length | [completed](src/p10.re) | | 24 | Random numbers | [completed](src/p24.re) |
| 11 | Modified run-length | [completed](src/p11.re) | | 25 | Random permutation | [completed](src/p25.re) |
| 12 | Decode run-length | [completed](src/p12.re) | | 26 | Combinations | [completed](src/p26.re) |
| 13 | Direct run-length | [completed](src/p13.re) | | 27 | Group | [completed](src/p27.re) |
| 14 | Duplicate | [completed](src/p14.re) | | 28 | Sort by length | [completed](src/p28.re) |

### Arithmetic

| No. | Description | Solution | | No. | Description | Solution |
| --- | -------------------- | ----------------------- | --- | --- | --------------------- | ----------------------- |
| 31 | Prime | [completed](src/p31.re) | | 37 | Improve 32 | [completed](src/p37.re) |
| 32 | GCD | [completed](src/p32.re) | | 38 | Compare 32 and 35 | [completed](src/p38.re) |
| 33 | Coprime | [completed](src/p33.re) | | 39 | List prime numbers | [completed](src/p39.re) |
| 34 | Euler's totient | [completed](src/p34.re) | | 40 | Goldbach's conjecture | [completed](src/p40.re) |
| 35 | Prime factors | [completed](src/p35.re) | | 41 | Goldbach compositions | [completed](src/p41.re) |
| 36 | Prime factors (pair) | [completed](src/p36.re) | | | | |

### Logic and Codes

| No. | Description | Solution |
| --- | ---------------------------------------- | ------------------------------------------ |
| 46 | Truth tables for logical expressions | [completed](src/p46.re) |
| 47 | Truth tables for logical expressions (2) | [completed (combined with 46)](src/p46.re) |
| 48 | Truth tables for logical expressions (3) | [completed](src/p48.re) |
| 49 | Gray code | [completed](src/p49.re) |
| 50 | Huffman code | [completed](src/p50.re) |

### Binary Trees

| No. | Description | Solution | | No. | Description | Solution |
| --- | ------------------- | --------------------------- | --- | --- | ------------------------ | ------------------------ |
| 54A | Check Binary Tree | **No need as type checked** | | 62 | Collect internals | [completed](src/p62.re) |
| 55 | Completely balanced | [completed](src/p55.re) | | 62B | Collect nodes at a level | [completed](src/p62b.re) |
| 56 | Symmetric | [completed](src/p56.re) | | 63 | Complete binary tree | [completed](src/p63.re) |
| 57 | Binary search trees | [completed](src/p57.re) | | 64 | Layout 1 | [completed](src/p64.re) |
| 58 | Generate-and-test | [completed](src/p58.re) | | 65 | Layout 2 | [completed](src/p65.re) |
| 59 | Height-balanced | [completed](src/p59.re) | | 66 | Layout 3 | [completed](src/p66.re) |
| 60 | 48 with N nodes | [completed](src/p60.re) | | 67A | Stringify | [completed](src/p67.re) |
| 61 | Count leaves | [completed](src/p61.re) | | 68 | Preorder and inorder | [completed](src/p68.re) |
| 61A | Collect leaves | [completed](src/p61a.re) | | 69 | Dotstring | [completed](src/p69.re) |

### Multiway Trees

| No. | Description | Solution |
| --- | -------------------- | --------------------------- |
| 70B | Check Multiway Tree | **No need as type checked** |
| 70C | Count nodes | [completed](src/p70c.re) |
| 70 | Node string | [completed](src/p70.re) |
| 71 | Internal path length | [completed](src/p71.re) |
| 72 | Bottom-up order | [completed](src/p72.re) |
| 73 | Lisp-like tree | [completed](src/p73.re) |

### Graphs

| No. | Description | Solution | | No. | Description | Solution |
| --- | --------------------- | ----------------------- | --- | --- | --------------------- | ----------------------- |
| 80 | Conversions | [completed](src/p80.re) | | 85 | Graph isomorphism | [completed](src/p85.re) |
| 81 | Path | [completed](src/p81.re) | | 86 | Graph coloration | [completed](src/p86.re) |
| 82 | Cycle | [completed](src/p82.re) | | 87 | Depth-first traversal | [completed](src/p87.re) |
| 83 | Spanning trees | [completed](src/p83.re) | | 88 | Connected components | [completed](src/p88.re) |
| 84 | Minimal spanning tree | [completed](src/p84.re) | | 89 | Bipartite graphs | Not implemented |

### Miscellaneous Problems

| No. | Description | Solution | | No. | Description | Solution |
| --- | ----------------------- | ----------------------- | --- | --- | -------------------- | ----------------------- |
| 90 | Eight queens problem | [completed](src/p90.re) | | 95 | English number words | [completed](src/p95.re) |
| 91 | Knight's tour | [completed](src/p91.re) | | 96 | Syntax checker | [completed](src/p96.re) |
| 92 | Von Koch's conjecture | [completed](src/p92.re) | | 97 | Sudoku | [completed](src/p97.re) |
| 93 | An arithmetic puzzle | [completed](src/p93.re) | | 98 | Nonograms | [completed](src/p98.re) |
| 94 | K-regular simple graphs | [completed](src/p94.re) | | 99 | Crossword puzzle | Not implemented |

## Acknowledgement

### References

- [99 Problems in OCaml](https://ocaml.org/learn/tutorials/99problems.html)

### Insipration

- [99 Problems in Haskell](https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems)
- [99 Problems in Lisp](https://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html)
- [99 Problems in Prolog](https://sites.google.com/site/prologsite/prolog-problems)
7 changes: 7 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "99.re",
"sources": ["src"],
"bsc-flags": ["-bs-super-errors"],
"package-specs": "commonjs",
"refmt": 3
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"private": true,
"name": "99.re",
"description": "99 problems with (reason)able solutions.",
"scripts": {
"clean": "bsb -clean-world",
"build": "bsb -make-world",
"watch": "bsb -make-world -w"
},
"keywords": [
"99 problems",
"reasonML",
"bucklescript"
],
"author": "Shriyans Bhatnagar <shriyansbhatnagar@gmail.com>",
"license": "MIT",
"devDependencies": {
"bs-platform": "^2.0.0"
}
}
7 changes: 7 additions & 0 deletions src/p01.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Find the last element of a list. */
let rec last = (list) =>
switch list {
| [] => None
| [x] => Some(x)
| [_, ...xs] => last(xs)
};
8 changes: 8 additions & 0 deletions src/p02.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Find the last but one element of a list. */
let rec last_but_one = (list) =>
switch list {
| []
| [_] => None
| [x, y] => Some((x, y))
| [_, ...xs] => last_but_one(xs)
};
6 changes: 6 additions & 0 deletions src/p03.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Find the K'th element of a list. The first element in the list is number 1. */
let rec element_at = (k, list) =>
switch list {
| [] => None
| [x, ...xs] => k == 1 ? Some(x) : element_at(k - 1, xs)
};
6 changes: 6 additions & 0 deletions src/p04.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Find the number of elements of a list. */
let rec length = (list) =>
switch list {
| [] => 0
| [_, ...xs] => 1 + length(xs)
};
9 changes: 9 additions & 0 deletions src/p05.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Reverse a list. */
let reverse = (list) => {
let rec aux = (acc, list) =>
switch list {
| [] => acc
| [x, ...xs] => aux([x, ...acc], xs)
};
aux([], list)
};
4 changes: 4 additions & 0 deletions src/p06.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Find out whether a list is a palindrome. */
/* either use the reverse function from the problem 05, or use the
built-in List.rev */
let is_palindrome = (xs) => xs == List.rev(xs);
14 changes: 14 additions & 0 deletions src/p07.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Flatten a nested list structure. */
type node('a) =
| One('a)
| Many(list(node('a)));

let flatten = (list) => {
let rec aux = (acc, list) =>
switch list {
| [] => acc
| [One(x), ...xs] => aux([x, ...acc], xs)
| [Many(x), ...xs] => aux(aux(acc, x), xs)
};
List.rev(aux([], list))
};
7 changes: 7 additions & 0 deletions src/p08.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Eliminate consecutive duplicates of list elements. */
let rec compress = (list) =>
switch list {
| [] => []
| [x] => [x]
| [x, ...[y, ..._] as z] => x == y ? compress(z) : [x, ...compress(z)]
};
11 changes: 11 additions & 0 deletions src/p09.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Pack consecutive duplicates of list elements into sublists. */
let pack = (list) => {
let rec aux = (curr, acc, list) =>
switch list {
| [] => []
| [x] => [[x, ...curr], ...acc]
| [x, ...[y, ..._] as z] =>
x == y ? aux([x, ...curr], acc, z) : aux([], [[x, ...curr], ...acc], z)
};
List.rev(aux([], [], list))
};
11 changes: 11 additions & 0 deletions src/p10.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Run-length encoding of a list. */
let encode = (list) => {
let rec aux = (count, acc, list) =>
switch list {
| [] => []
| [x] => [(count + 1, x), ...acc]
| [x, ...[y, ..._] as z] =>
x == y ? aux(count + 1, acc, z) : aux(0, [(count + 1, x), ...acc], z)
};
List.rev(aux(0, [], list))
};
16 changes: 16 additions & 0 deletions src/p11.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Modified run-length encoding. */
type rle('a) =
| One('a)
| Many(int, 'a);

let modified_encode = (list) => {
let create_tuple = (count, elem) => count == 1 ? One(elem) : Many(count, elem);
let rec aux = (count, acc, list) =>
switch list {
| [] => []
| [x] => [create_tuple(count + 1, x), ...acc]
| [x, ...[y, ..._] as z] =>
x == y ? aux(count + 1, acc, z) : aux(0, [create_tuple(count + 1, x), ...acc], z)
};
List.rev(aux(0, [], list))
};
15 changes: 15 additions & 0 deletions src/p12.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Decode a run-length encoded list. */
type rle('a) =
| One('a)
| Many(int, 'a);

let decode = (list) => {
let rec many = (acc, n, x) => n == 0 ? acc : many([x, ...acc], n - 1, x);
let rec aux = (acc, list) =>
switch list {
| [] => []
| [One(x), ...xs] => aux([x, ...acc], xs)
| [Many(n, x), ...xs] => aux(many(acc, n, x), xs)
};
aux([], List.rev(list))
};
Loading

0 comments on commit 6b79be4

Please sign in to comment.