Fach (Class) |
Montag (Monday) |
Dienstag (Tuesday) |
Mittwoch (Wednesday) |
Donnerstag (Thursday) |
Freitag (Friday) |
Löschen (Delete) |
---|
class Book { constructor(name, times) { this.name = name; this.times = times; this.toString = () => { return this.name; } } } function testBookScheduler() { const books = [ new Book("Mathematics", [true, false, true, true, false]), new Book("Latin", [false, true, true, false, true]), new Book("Biology", [true, true, false, false, false]), new Book("History", [false, false, false, false, true]), new Book("English", [true, false, true, false, true]), new Book("German", [false, true, true, false, true]), new Book("Physics", [true, false, false, false, false]), new Book("Catholic-Religion", [false, true, false, false, true]), new Book("Chemistry", [false, false, true, true, false]) ]; const schedules = createBookSchedules(books); for(const schedule of schedules) { console.log("Person 1: "+schedule.person1); console.log("Person 2: "+schedule.person2); console.log() } } function createBookSchedules(books) { const maxCombinations = Math.pow(2, books.length); let booksPerson1 = []; let booksPerson2 = []; let bestAverage = -1; const bestCombinations = []; for(let combination = 0; combination < maxCombinations; combination++) { for(let i = 0; i < books.length; i++) { if((combination >> i) % 2 === 1) booksPerson1.push(books[i]); else booksPerson2.push(books[i]); } const avgDays = [0, 0, 0, 0, 0]; booksPerson1.forEach(book => { for(let i = 0; i < book.times.length; i++) if(book.times[i]) avgDays[i] += 1; }); booksPerson2.forEach(book => { for(let i = 0; i < book.times.length; i++) if(book.times[i]) avgDays[i] -= 1; }); let average = 0; for(const avgDay of avgDays) average += Math.abs(avgDay); average /= 5; if(bestAverage === -1 || average < bestAverage) { bestAverage = average; bestCombinations.splice(0, bestCombinations.length); bestCombinations.push(combination); } else if(bestAverage === average) { bestCombinations.push(combination); } booksPerson1 = []; booksPerson2 = []; } const objects = []; bestCombinations.forEach(combination => { for(let i = 0; i < books.length; i++) { if((combination >> i) % 2 === 1) booksPerson1.push(books[i]); else booksPerson2.push(books[i]); } objects.push({ combination: combination, average: bestAverage, person1: [...booksPerson1], person2: [...booksPerson2] }); booksPerson1 = []; booksPerson2 = []; }) return objects; }
import java.util.Arrays; import java.util.LinkedList; public class BookScheduler { record Book(String name, boolean[] times) {} public static final Book[] BOOKS = new Book[] { new Book("Mathematics", new boolean[] {true, false, true, true, false}), new Book("Latin", new boolean[] {false, true, true, false, true}), new Book("Biology", new boolean[] {true, true, false, false, false}), new Book("History", new boolean[] {false, false, false, false, true}), new Book("English", new boolean[] {true, false, true, false, true}), new Book("German", new boolean[] {false, true, true, false, true}), new Book("Physics", new boolean[] {true, false, false, false, false}), new Book("Catholic-Religion", new boolean[] {false, true, false, false, true}), new Book("Chemistry", new boolean[] {false, false, true, true, false}) }; public static void main(String[] args) { final int maxCombinations = 1 << BOOKS.length; final LinkedList<Book> booksPerson1 = new LinkedList<>(); final LinkedList<Book> booksPerson2 = new LinkedList<>(); double bestAverage = Double.NaN; final LinkedList<Integer> bestCombinations = new LinkedList<>(); for(int combination = 0; combination < maxCombinations; combination++) { for(int i = 0; i < BOOKS.length; i++) { if((combination >>> i) % 2 == 1) booksPerson1.add(BOOKS[i]); else booksPerson2.add(BOOKS[i]); } int[] avgDays = {0, 0, 0, 0, 0}; booksPerson1.forEach(book -> { for(int i = 0; i < book.times.length; i++) if(book.times[i]) avgDays[i] += 1; }); booksPerson2.forEach(book -> { for(int i = 0; i < book.times.length; i++) if(book.times[i]) avgDays[i] -= 1; }); double average = 0; for(int avgDay : avgDays) average += Math.abs(avgDay); average /= 5; if(Double.isNaN(bestAverage) || average < bestAverage) { bestAverage = average; bestCombinations.clear(); bestCombinations.add(combination); } else if(average == bestAverage) { bestCombinations.add(combination); } booksPerson1.clear(); booksPerson2.clear(); } final double average = bestAverage; bestCombinations.forEach(combination -> { for(int i = 0; i < BOOKS.length; i++) { if((combination >>> i) % 2 == 1) booksPerson1.add(BOOKS[i]); else booksPerson2.add(BOOKS[i]); } String s = Integer.toBinaryString(combination); s = "0".repeat(BOOKS.length - s.length()) + s; System.out.println("Combination: [" + s + "], average difference: " + average); System.out.println("Books: [Monday, Tuesday, Wednesday, Thursday, Friday]"); System.out.print("Person 1: "); booksPerson1.forEach(book -> System.out.print(book.name + " ")); System.out.println(); System.out.print("Person 2: "); booksPerson2.forEach(book -> System.out.print(book.name + " ")); System.out.println("\n"); booksPerson1.clear(); booksPerson2.clear(); }); } }