Skip to content

Commit

Permalink
Add function that convert array into State type
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 7, 2020
1 parent e8c9e65 commit 535758d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
29 changes: 21 additions & 8 deletions src/functions/state.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 ++){
Expand All @@ -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){
Expand All @@ -63,4 +76,4 @@ function readState(state: State){
console.log('---------');
}

export { areEqual, includes, readState, isSolvable }
export { areEqual, includes, readState, isSolvable, convertArrayInState, convertStateInArray }
4 changes: 3 additions & 1 deletion src/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
type StateAsList = StateItem[];

export { State, StateItem, StateAsList }
57 changes: 55 additions & 2 deletions tests/state.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 535758d

Please sign in to comment.