Skip to content

Commit

Permalink
Add proofs and stubs for all practice exercises (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
SleeplessByte authored Apr 10, 2021
1 parent 9c4c4a4 commit 961cac4
Show file tree
Hide file tree
Showing 325 changed files with 5,456 additions and 5,484 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default <T, O>(list: T[], accumulator: (arg: T) => O): O[] => {
export function accumulate<T, O>(list: T[], accumulator: (arg: T) => O): O[] {
const out = []
let idx = -1
const end = list.length
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/accumulate/accumulate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import accumulate from './accumulate'
import { accumulate } from './accumulate'

describe('accumulate()', () => {
it('accumulation empty', () => {
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/accumulate/accumulate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function accumulate(list: unknown, accumulator: unknown): never {
throw new Error('Remove this statement and implement this function')
}
12 changes: 12 additions & 0 deletions exercises/practice/acronym/.meta/proof.ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function parse(phrase: string): string {
if (typeof phrase !== 'undefined' && phrase !== undefined) {
const match = phrase.match(/[A-Z]+[a-z]*|[a-z]+/g)
return !match
? ''
: match.reduce(
(acronym: string, word: string) => (acronym += word[0].toUpperCase()),
''
)
}
return ''
}
15 changes: 0 additions & 15 deletions exercises/practice/acronym/acronym.example.ts

This file was deleted.

16 changes: 7 additions & 9 deletions exercises/practice/acronym/acronym.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import Acronym from './acronym'
import { parse } from './acronym'

describe('Acronym are produced from', () => {
it('title cased phrases', () => {
expect(Acronym.parse('Portable Network Graphics')).toEqual('PNG')
expect(parse('Portable Network Graphics')).toEqual('PNG')
})

xit('other title cased phrases', () => {
expect(Acronym.parse('Ruby on Rails')).toEqual('ROR')
expect(parse('Ruby on Rails')).toEqual('ROR')
})

xit('inconsistently cased phrases', () => {
expect(Acronym.parse('HyperText Markup Language')).toEqual('HTML')
expect(parse('HyperText Markup Language')).toEqual('HTML')
})

xit('phrases with punctuation', () => {
expect(Acronym.parse('First In, First Out')).toEqual('FIFO')
expect(parse('First In, First Out')).toEqual('FIFO')
})

xit('other phrases with punctuation', () => {
expect(Acronym.parse('PHP: Hypertext Preprocessor')).toEqual('PHP')
expect(parse('PHP: Hypertext Preprocessor')).toEqual('PHP')
})

xit('phrases with punctuation and sentence casing', () => {
expect(Acronym.parse('Complementary metal-oxide semiconductor')).toEqual(
'CMOS'
)
expect(parse('Complementary metal-oxide semiconductor')).toEqual('CMOS')
})
})
6 changes: 2 additions & 4 deletions exercises/practice/acronym/acronym.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export default class Acronym {
public static parse(phrase: string): string {
return ''
}
export function parse(phrase: unknown): unknown {
throw new Error('Remove this statement and implement this function')
}
59 changes: 59 additions & 0 deletions exercises/practice/all-your-base/.meta/proof.ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const isValidBase = (base: number): boolean => {
return !base || base < 2 || Math.floor(base) !== base
}

const isInputValid = (array: number[], base: number): boolean => {
if (!array || !array.length) {
return false
}
const val = base - 1
for (let i = 0, n = array.length; i < n; i++) {
const tmp = array[i]
if (tmp > val || tmp < 0) {
return false
}
}
return true
}

const convertFromDecimalToBase = (
num: number,
outputBase: number
): number[] => {
let tmp = num
const result = []
while (tmp) {
result.unshift(tmp % outputBase)
tmp = Math.floor(tmp / outputBase)
}
return result
}

export function convert(
array: number[],
inputBase: number,
outputBase: number
): number[] {
if (isValidBase(inputBase)) {
throw new Error('Wrong input base')
}
if (isValidBase(outputBase)) {
throw new Error('Wrong output base')
}
const regexp = new RegExp('^0.', 'g')
const str = array.join('')
if (str.match(regexp) || !isInputValid(array, inputBase)) {
throw new Error('Input has wrong format')
}
if (str === '0') {
return [0]
}
if (str === '1') {
return [1]
}
const decimalValue = array.reduce(
(accumulator, value) => accumulator * inputBase + value,
0
)
return convertFromDecimalToBase(decimalValue, outputBase)
}
61 changes: 0 additions & 61 deletions exercises/practice/all-your-base/all-your-base.example.ts

This file was deleted.

48 changes: 23 additions & 25 deletions exercises/practice/all-your-base/all-your-base.test.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,117 @@
import Converter from './all-your-base'

const converter = new Converter()
import { convert } from './all-your-base'

describe('Converter', () => {
it('single bit one to decimal', () => {
expect(converter.convert([1], 2, 10)).toEqual([1])
expect(convert([1], 2, 10)).toEqual([1])
})

xit('binary to single decimal', () => {
expect(converter.convert([1, 0, 1], 2, 10)).toEqual([5])
expect(convert([1, 0, 1], 2, 10)).toEqual([5])
})

xit('single decimal to binary', () => {
expect(converter.convert([5], 10, 2)).toEqual([1, 0, 1])
expect(convert([5], 10, 2)).toEqual([1, 0, 1])
})

xit('binary to multiple decimal', () => {
expect(converter.convert([1, 0, 1, 0, 1, 0], 2, 10)).toEqual([4, 2])
expect(convert([1, 0, 1, 0, 1, 0], 2, 10)).toEqual([4, 2])
})

xit('decimal to binary', () => {
expect(converter.convert([4, 2], 10, 2)).toEqual([1, 0, 1, 0, 1, 0])
expect(convert([4, 2], 10, 2)).toEqual([1, 0, 1, 0, 1, 0])
})

xit('trinary to hexadecimal', () => {
expect(converter.convert([1, 1, 2, 0], 3, 16)).toEqual([2, 10])
expect(convert([1, 1, 2, 0], 3, 16)).toEqual([2, 10])
})

xit('hexadecimal to trinary', () => {
expect(converter.convert([2, 10], 16, 3)).toEqual([1, 1, 2, 0])
expect(convert([2, 10], 16, 3)).toEqual([1, 1, 2, 0])
})

xit('15-bit integer', () => {
expect(converter.convert([3, 46, 60], 97, 73)).toEqual([6, 10, 45])
expect(convert([3, 46, 60], 97, 73)).toEqual([6, 10, 45])
})

xit('empty list', () => {
expect(() => {
converter.convert([], 2, 10)
convert([], 2, 10)
}).toThrowError('Input has wrong format')
})

xit('single zero', () => {
expect(converter.convert([0], 10, 2)).toEqual([0])
expect(convert([0], 10, 2)).toEqual([0])
})

xit('multiple zeros', () => {
expect(() => {
converter.convert([0, 0, 0], 10, 2)
convert([0, 0, 0], 10, 2)
}).toThrowError('Input has wrong format')
})

xit('leading zeros', () => {
expect(() => {
converter.convert([0, 6, 0], 7, 10)
convert([0, 6, 0], 7, 10)
}).toThrowError('Input has wrong format')
})

xit('negative digit', () => {
expect(() => {
converter.convert([1, -1, 1, 0, 1, 0], 2, 10)
convert([1, -1, 1, 0, 1, 0], 2, 10)
}).toThrowError('Input has wrong format')
})

xit('invalid positive digit', () => {
expect(() => {
converter.convert([1, 2, 1, 0, 1, 0], 2, 10)
convert([1, 2, 1, 0, 1, 0], 2, 10)
}).toThrowError('Input has wrong format')
})

xit('first base is one', () => {
expect(() => {
converter.convert([], 1, 10)
convert([], 1, 10)
}).toThrowError('Wrong input base')
})

xit('second base is one', () => {
expect(() => {
converter.convert([1, 0, 1, 0, 1, 0], 2, 1)
convert([1, 0, 1, 0, 1, 0], 2, 1)
}).toThrowError('Wrong output base')
})

xit('first base is zero', () => {
expect(() => {
converter.convert([], 0, 10)
convert([], 0, 10)
}).toThrowError('Wrong input base')
})

xit('second base is zero', () => {
expect(() => {
converter.convert([7], 10, 0)
convert([7], 10, 0)
}).toThrowError('Wrong output base')
})

xit('first base is negative', () => {
expect(() => {
converter.convert([1], -2, 10)
convert([1], -2, 10)
}).toThrowError('Wrong input base')
})

xit('second base is negative', () => {
expect(() => {
converter.convert([1], 2, -7)
convert([1], 2, -7)
}).toThrowError('Wrong output base')
})

xit('both bases are negative', () => {
expect(() => {
converter.convert([1], -2, -7)
convert([1], -2, -7)
}).toThrowError('Wrong input base')
})

xit('wrong output_base base not integer', () => {
expect(() => {
converter.convert([0], 3, 2.5)
convert([0], 3, 2.5)
}).toThrowError('Wrong output base')
})
})
7 changes: 7 additions & 0 deletions exercises/practice/all-your-base/all-your-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function convert(
digits: unknown,
inputBase: unknown,
outputBase: unknown
): unknown {
throw new Error('Remove this statement and implement this function')
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Allergies {
export class Allergies {
private allergenIndex: number

private possibleAllergies = [
Expand Down Expand Up @@ -44,5 +44,3 @@ class Allergies {
return allergicTo
}
}

export default Allergies
2 changes: 1 addition & 1 deletion exercises/practice/allergies/allergies.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Allergies from './allergies'
import { Allergies } from './allergies'

describe('allergicTo', () => {
it('no allergies means not allergic', () => {
Expand Down
13 changes: 13 additions & 0 deletions exercises/practice/allergies/allergies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class Allergies {
constructor(allergenIndex: unknown) {
throw new Error('Remove this statement and implement this function')
}

public list(): unknown {
throw new Error('Remove this statement and implement this function')
}

public allergicTo(allergen: unknown): unknown {
throw new Error('Remove this statement and implement this function')
}
}
Loading

0 comments on commit 961cac4

Please sign in to comment.