Skip to content

Commit

Permalink
fix: integer length
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Oct 28, 2020
1 parent 1ada2fd commit 3354e08
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lib/typescript/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './num-length.function';
export * from './integer-length.function';
export * from './num-start.function';
export * from './num-at.function';
export * from './lcm.function';
Expand Down
43 changes: 43 additions & 0 deletions src/lib/typescript/functions/integer-length.function.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai';
import { integerLength, integerLengthMath } from './integer-length.function';

describe(`Integer length`, () => {
it(`should work with numbers larger than 1`, async () => {
expect(integerLength(123)).to.equal(3);
expect(integerLength(7653)).to.equal(4);
expect(integerLength(85476847)).to.equal(8);
});

it(`should work with 1`, async () => {
expect(integerLength(1)).to.equal(1);
});

it(`should work with 0`, async () => {
expect(integerLength(0)).to.equal(1);
});

it(`should work with negative numbers`, async () => {
expect(integerLength(-34312)).to.equal(5);
});
});

// tslint:disable: deprecation
describe(`Integer length (Math)`, () => {
it(`should work with numbers larger than 1`, async () => {
expect(integerLengthMath(123)).to.equal(3);
expect(integerLengthMath(7653)).to.equal(4);
expect(integerLengthMath(85476847)).to.equal(8);
});

it(`should work with 1`, async () => {
expect(integerLengthMath(1)).to.equal(1);
});

it(`should work with 0`, async () => {
expect(integerLengthMath(0)).to.equal(0);
});

it(`should work with negative numbers`, async () => {
expect(integerLengthMath(-34312)).to.equal(5);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Returns the length of the integer part of a number.
*/
export function integerLength(n: number): number {
return Math.floor(n).toString().length;
return Math.floor(Math.abs(n)).toString().length;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/typescript/functions/num-at.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param n the number of whichs length will be returned
*/
export function numAt(n: number, p: number): number {
return parseInt(n.toString()[p], 10);
return parseInt(n.toString()[p], 10) || 0;
}

/**
Expand Down
22 changes: 0 additions & 22 deletions src/lib/typescript/functions/num-length.function.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/typescript/intcode/intcode.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { numAt, numLength } from '@lib/functions';
import { integerLength, numAt } from '@lib/functions';
import { Instruction, toInstruction } from './instruction.enum';
import { Mode } from './mode.enum';

Expand Down Expand Up @@ -130,7 +130,7 @@ export class IntCodeComputer implements Iterable<number | undefined> {
}

private getMode(v: number, n: number): Mode | undefined {
return numAt(v, numLength(v) - n - 3);
return numAt(v, integerLength(v) - n - 3);
}

public *[Symbol.iterator](): IterableIterator<number | undefined> {
Expand Down

0 comments on commit 3354e08

Please sign in to comment.