-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proofs and stubs for all practice exercises (#443)
- Loading branch information
1 parent
9c4c4a4
commit 961cac4
Showing
325 changed files
with
5,456 additions
and
5,484 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...practice/accumulate/accumulate.example.ts → ...ses/practice/accumulate/.meta/proof.ci.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '' | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
Oops, something went wrong.