Premier commit - Introduction à git
This commit is contained in:
commit
81e57251aa
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
bin/
|
||||
106
src/com/bernard/condorcet/ScrutinConDOrcet.java
Normal file
106
src/com/bernard/condorcet/ScrutinConDOrcet.java
Normal file
@ -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<String> results = condorc(e);
|
||||
System.out.println(results);
|
||||
}
|
||||
|
||||
public static final Election voteReader(File f) throws IOException{
|
||||
BufferedReader bis = new BufferedReader(new FileReader(f));
|
||||
List<Integer[]> votes = new ArrayList<Integer[]>();
|
||||
List<String> candidats = new ArrayList<String>();
|
||||
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<bulletin.length;i++) {
|
||||
if(!candidats.contains(bulletin[i]))
|
||||
candidats.add(bulletin[i]);
|
||||
bultindex[i] = candidats.indexOf(bulletin[i]);
|
||||
}
|
||||
votes.add(bultindex);
|
||||
}
|
||||
bis.close();
|
||||
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 final Election jsonVoteReader(File f) throws IOException{
|
||||
|
||||
List<Integer[]> votes = new ArrayList<Integer[]>();
|
||||
List<String> candidats = new ArrayList<String>();
|
||||
|
||||
|
||||
|
||||
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<String> condorc(Election e) {
|
||||
return condorc(e.candidats,e.votes);
|
||||
}
|
||||
public static List<String> condorc(String[] candidats,Integer[][] votes) {
|
||||
final int n = candidats.length;
|
||||
int[] duels = new int[n];
|
||||
Arrays.fill(duels, 0);
|
||||
for(int i = 0;i<n;i++) {
|
||||
for(int j = i+1;j<n;j++) {
|
||||
|
||||
// Duel entre candidat i et candidat j
|
||||
final int i2 = i,j2 = j;
|
||||
//Compte le nombre de votes pour (i plutot que j) puis (j plutot que i)
|
||||
long votesi = Arrays.stream(votes).filter(a -> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
3
src/module-info.java
Normal file
3
src/module-info.java
Normal file
@ -0,0 +1,3 @@
|
||||
module scrutinPeyrel {
|
||||
requires java.xml;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user