Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Box.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { packBoxs } from './packBoxs';

const Box = () => {

let items: number[] = [1, 6, 3, 8, 4, 1, 6, 8, 9, 5, 2, 5, 7, 7, 3];
let boxs = packBoxs(items);

return (
<form >
<div>
<output>Cartons </output>
<div>{boxs}</div>
</div>
</form>
);
}

export default Box;
7 changes: 7 additions & 0 deletions src/packBox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { packBox } from './packBox';

test('test emballage un carton', () => {
const articles = [6, 5, 4];
const boxReceived = packBox(articles);
expect(boxReceived).toEqual([4, 6]);
});
15 changes: 15 additions & 0 deletions src/packBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const packBox = (itemsSorted: number[]): number[] => {
let box: number[] = [];
// Remplir le carton tant qu'il n'est pas rempli
while (itemsSorted[itemsSorted.length - 1] + itemsSorted[0] + countItemsBox(box) <= 10) {
box.push(itemsSorted[itemsSorted.length - 1])
itemsSorted.pop();
}
box.push(itemsSorted[0]);
itemsSorted.shift();
return box;
}

const countItemsBox = (box: number[]) => {
return box.reduce((a, b) => a + b, 0);
}
7 changes: 7 additions & 0 deletions src/packBoxs.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { packBoxs } from './packBoxs';

test('test emballage tout les articles', () => {
let articles: number[] = [1, 6, 3, 8, 4, 1, 6, 8, 9, 5, 2, 5, 7, 7, 3];
const boxsReceived = packBoxs(articles);
expect(boxsReceived).toEqual([[4, 6], [1, 9], [1, 8], [2, 8], [3, 7], [4, 6], [6], [5, 5]]);
});
17 changes: 17 additions & 0 deletions src/packBoxs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { packBox } from './packBox';

export const packBoxs = (articles: number[]) => {
let boxs = [];
if (articles.length !== 0) {
console.info("Début du traitement");
// Faire un tri descendant
let itemsSorted: number[] = articles.sort().reverse();
// Emballer les cartons jusqu'au dernier article
while (itemsSorted.length !== 0) {
// Emballer carton par carton
boxs.push(packBox(itemsSorted) + "/");
}
console.info("Fin du traintement. Nombre de boite est ", boxs.length);
}
return boxs;
}