commit 81e57251aa59f4d29aaa7c040f01931b0dbcde01 Author: Mysaa Date: Mon May 24 18:53:41 2021 +0200 Premier commit - Introduction à git diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e108870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.classpath +.project +.settings +bin/ diff --git a/src/com/bernard/condorcet/ScrutinConDOrcet.java b/src/com/bernard/condorcet/ScrutinConDOrcet.java new file mode 100644 index 0000000..697ce59 --- /dev/null +++ b/src/com/bernard/condorcet/ScrutinConDOrcet.java @@ -0,0 +1,106 @@ +package com.bernard.condorcet; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class ScrutinConDOrcet { + + public static void main(String[] args) throws IOException { + Election e = voteReader(new File("/home/mysaa/Desktop/votes")); + List results = condorc(e); + System.out.println(results); + } + + public static final Election voteReader(File f) throws IOException{ + BufferedReader bis = new BufferedReader(new FileReader(f)); + List votes = new ArrayList(); + List candidats = new ArrayList(); + String line; + while((line = bis.readLine()) != null) { + int li = line.indexOf("#"); + if(li!=-1) + line = line.substring(0, li); + String[] bulletin = line.split(","); + Integer[] bultindex = new Integer[bulletin.length]; + for(int i = 0;i votes = new ArrayList(); + List candidats = new ArrayList(); + + + + Integer[][] out = new Integer[votes.size()][]; + votes.toArray(out); + String[] kdidats = new String[candidats.size()]; + candidats.toArray(kdidats); + return new Election(out,kdidats); + + + } + + public static List condorc(Election e) { + return condorc(e.candidats,e.votes); + } + public static List condorc(String[] candidats,Integer[][] votes) { + final int n = candidats.length; + int[] duels = new int[n]; + Arrays.fill(duels, 0); + for(int i = 0;i indexof(a, i2,n) < indexof(a, j2,n)).count(); + long votesj = Arrays.stream(votes).filter(a -> indexof(a, j2,n) < indexof(a, i2,n)).count(); + + System.out.println(candidats[i]+">"+candidats[j]+"- "+votesi+" sont pour et "+votesj+" sont contre"); + + if(votesi>votesj) + duels[i]++; + else if(votesj>votesi) + duels[j]++; + } + } + return IntStream.range(0, candidats.length).boxed().sorted((i,j) -> Integer.compare(duels[i],duels[j])).map(i->Integer.toString(duels[i])+":"+candidats[i]).collect(Collectors.toList()); + + + } + public static final int indexof(Integer[] a, int i,int defaultValue) { + return IntStream.range(0, a.length).filter(j->a[j]==i).findAny().orElse(defaultValue); + } + + static class Election{ + Integer[][] votes; + String[] candidats; + public Election(Integer[][] out, String[] candidats) { + this.votes = out; + this.candidats = candidats; + } + + } + +} diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..aac6f00 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,3 @@ +module scrutinPeyrel { + requires java.xml; +} \ No newline at end of file