From 535758dc00bff7a3554ec7faf2457a8b5e2a6176 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Tue, 7 Jul 2020 14:50:06 -0300 Subject: [PATCH] Add function that convert array into State type --- src/functions/state.ts | 29 +++++++++++++++------ src/utils/state.ts | 4 ++- tests/state.spec.ts | 57 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/functions/state.ts b/src/functions/state.ts index e42e2e0..73efd6d 100644 --- a/src/functions/state.ts +++ b/src/functions/state.ts @@ -1,4 +1,4 @@ -import { State, StateItem } from "../utils/state"; +import { State, StateItem, StateAsList } from "../utils/state"; // Using this function because literal comparison between 2 types (state1 == state2) always return false @@ -30,7 +30,8 @@ function isSolvable(state: State){ } function quantityOfInversions(state: State): number{ - const stateList = convertStateInArray(state); + let stateList = convertStateInArray(state); + stateList = stateList.filter((val) => {return val !== 0}); // Do not consider 0 when counting inversions let inversions = 0; for(let i in stateList){ for (let j = Number(i)+1; j < stateList.length; j ++){ @@ -43,18 +44,30 @@ function quantityOfInversions(state: State): number{ } //TODO: Melhorar a forma de fazer isso -function convertStateInArray(state: State): StateItem[]{ - let stateList: StateItem[] = []; +function convertStateInArray(state: State): StateAsList{ + let stateList: StateAsList = []; for(let l in state){ for (let c in state[l]){ - if(state[l][c] !== 0){// Do not consider 0 when counting inversions - stateList.push(state[l][c]); - } + stateList.push(state[l][c]); } } return stateList; } +//TODO: Melhorar a forma de fazer isso +function convertArrayInState(stateList: StateAsList): State{ + let state: State = [[0,0,0],[0,0,0],[0,0,0]]; + for(let l in state){ + for (let c in state[l]){ + const item = stateList.shift(); + if(item){ + state[l][c] = item; + } + } + } + return state; +} + function readState(state: State){ console.log('---------'); for(let l of state){ @@ -63,4 +76,4 @@ function readState(state: State){ console.log('---------'); } -export { areEqual, includes, readState, isSolvable } \ No newline at end of file +export { areEqual, includes, readState, isSolvable, convertArrayInState, convertStateInArray } \ No newline at end of file diff --git a/src/utils/state.ts b/src/utils/state.ts index e8c0b9a..bef186d 100644 --- a/src/utils/state.ts +++ b/src/utils/state.ts @@ -2,4 +2,6 @@ type StateItem = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 0; type State = [[StateItem, StateItem, StateItem], [StateItem, StateItem, StateItem], [StateItem, StateItem, StateItem]]; -export { State, StateItem } \ No newline at end of file +type StateAsList = StateItem[]; + +export { State, StateItem, StateAsList } \ No newline at end of file diff --git a/tests/state.spec.ts b/tests/state.spec.ts index 4b45f3a..cbaae12 100644 --- a/tests/state.spec.ts +++ b/tests/state.spec.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { areEqual, includes, isSolvable } from '../src/functions/state'; -import { State } from '../src/utils/state'; +import { areEqual, includes, isSolvable, convertArrayInState, convertStateInArray } from '../src/functions/state'; +import { State, StateAsList } from '../src/utils/state'; describe('State Smoke Tests', () => { it('Should exist areEqual function', () => { @@ -31,6 +31,25 @@ describe('State Smoke Tests', () => { const state: State = [[4,5,8], [0,1,6], [7, 2, 3]]; expect(isSolvable(state)).to.be.a('Boolean'); }); + + it('Should exist convertArrayInState function', () => { + expect(convertArrayInState).to.exist; + }); + + it('Should return Array(State) when call convertArrayInState function', () => { + const stateList: StateAsList = [0,0,0,0,0,0,0,0,0]; + expect(convertArrayInState(stateList)).to.be.an('Array'); + }); + + it('Should exist convertStateInArray function', () => { + expect(convertStateInArray).to.exist; + }); + + it('Should return Array when call convertStateInArray function', () => { + const state: State = [[0,0,0],[0,0,0],[0,0,0]]; + expect(convertStateInArray(state)).to.be.an('Array'); + }); + }); describe('Testing returns of areEqual function', () => { @@ -144,4 +163,38 @@ describe('Testing returns of isSolvable function', () => { expect(isSolvable(state)).to.eql(false); }); +}); + +describe('Testing returns of convertArrayInState function', () => { + it('Should return this state using this array', () => { + const stateList: StateAsList = [1, 2, 3, 4, 5, 6, 7, 8, 0]; + const expectedState: State = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]; + expect(convertArrayInState(stateList)).to.be.eql(expectedState); + }); + + it('Should return this state using this array', () => { + const stateList: StateAsList = [4, 5, 8, 1, 2, 6, 7, 3, 0]; + const expectedState: State = [[4, 5, 8], [1, 2, 6], [7, 3, 0]]; + expect(convertArrayInState(stateList)).to.be.eql(expectedState); + }); +}); + +describe('Testing returns of convertStateInArray function', () => { + it('Should return this array using this state', () => { + const state: State = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]; + const expectedStateList: StateAsList = [1, 2, 3, 4, 5, 6, 7, 8, 0]; + expect(convertStateInArray(state)).to.be.eql(expectedStateList); + }); + + it('Should return this array using this state', () => { + const state: State = [[4, 5, 8], [6, 7, 3], [1, 2, 0]]; + const expectedStateList: StateAsList = [4, 5, 8, 6, 7, 3, 1, 2, 0]; + expect(convertStateInArray(state)).to.be.eql(expectedStateList); + }); + + it('Should return this array using this state', () => { + const state: State = [[4, 2, 8], [6, 1, 3], [5, 7, 0]]; + const expectedStateList: StateAsList = [4, 2, 8, 6, 1, 3, 5, 7, 0]; + expect(convertStateInArray(state)).to.be.eql(expectedStateList); + }); }); \ No newline at end of file