Skip to content
Snippets Groups Projects
Commit 6ee51da6 authored by tanguy.dietrich's avatar tanguy.dietrich
Browse files

pull branch master

parents c97108da deb582ff
No related branches found
No related tags found
1 merge request!2Tanguy
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
# Numeric
David Carballo
Tanguy Dietrich
Ce repository contient l'énoncé du TP "numeric" et un squelette maven.
Nous vous conseillons de vous abonner aux notifications (watch) pour ne pas manquer des annonces ou des changements.
......@@ -138,4 +139,3 @@ mvn test
### Notes
Le plugin `maven-assembly-plugin` est configuré et permet d'embarquer les dépendances dans un seul `jar`.
......@@ -2,7 +2,7 @@ recupere l'etat actuel du projet a partir de git
>git clone https://githepia.hesge.ch/david.carballo/poo2019numeric
Changement de branche
>git checkout -b NomBranche
>git checkout NomBranche
ajoute les nouvelle modification
>git add .
......@@ -18,3 +18,7 @@ AUTRE :
verifie si il y a des changement
>git diff
A faire : norm, norms, sum, Transposed,
fait : add, mul, sub, zero, fill, of, empty, len
package ch.hepia.hangman;
public class Hangman
{
/*
currentWord = l'avancement actuel du mot ex : _a___ == David
guessWord = le mot rechercher ex David
letter = la lettre entrers
Ex :
currentWord = _____
guessWord = David
letter = 'a'
resultat = _a____
*/
public static String check(String currentWord,String guessWord,char letter)
{
//les string sont immutable en java
//conversion en tableau de char
char[] tempArray= currentWord.toCharArray();
for(int i=0;i<guessWord.length();i++)
{
if(guessWord.charAt(i)==letter)//a ajouter les test majuscule
{
tempArray[i]=letter;
}
}
return String.valueOf(tempArray);
}
/*
guessWord = le mot a cacher
Ex :
guessWord = David
resultat = ______
*/
public static String createWord(String guessWord)
{
char tempArray[]=guessWord.toCharArray();
for(int i =0;i<guessWord.length();i++)
{
tempArray[i]='_';
}
return String.valueOf(tempArray);
}
/*
Affiche l'etat de la potence en fonction du nombre d'essai.
*/
public static String showHangman(int essai)
{
String pot ="";
pot=pot.concat("----------\n");
switch(essai)
{
case 1:
pot=pot.concat("|/\n");
for(int i=0;i<6;i++)
{
pot=pot.concat("|\n");
}
break;
case 2:
pot=pot.concat("|/ |\n");
for(int i=0;i<6;i++)
{
pot=pot.concat("|\n");
}
break;
case 3 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
for(int i=0;i<5;i++)
{
pot=pot.concat("|\n");
}
break;
case 4 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
pot=pot.concat("| |\n");
for(int i=0;i<4;i++)
{
pot=pot.concat("|\n");
}
break;
case 5 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
pot=pot.concat("| /|\n");
for(int i=0;i<4;i++)
{
pot=pot.concat("|\n");
}
break;
case 6 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
pot=pot.concat("| /|\\\n");
for(int i=0;i<4;i++)
{
pot=pot.concat("|");
}
break;
case 7 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
pot=pot.concat("| /|\\\n");
pot=pot.concat("| | \n");
for(int i=0;i<3;i++)
{
pot=pot.concat("|");
}
break;
case 8 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
pot=pot.concat("| /|\\\n");
pot=pot.concat("| | \n");
pot=pot.concat("| / \n");
for(int i=0;i<2;i++)
{
pot=pot.concat("|");
}
break;
case 9 :
pot=pot.concat("|/ |\n");
pot=pot.concat("| (_)\n");
pot=pot.concat("| /|\\\n");
pot=pot.concat("| | \n");
pot=pot.concat("| / \\\n");
for(int i=0;i<2;i++)
{
pot=pot.concat("|\n");
}
break;
}
pot=pot.concat("");
return pot;
}
}
package ch.hepia.numeric;
import java.util.List;
import java.util.ArrayList;
public class Transposed{
final private Vector v;
......@@ -7,8 +10,15 @@ public class Transposed{
Transposed(Vector v) {
this.v = v;
}
public double dot(Vector vect){
throw new UnsupportedOperationException("This feature isn't implemented yet");
public double dot(Vector vect)
{
double resultat=0;
for (int index=0;index<=vect.len()-1;index++)
{
resultat+=v.get(index)*vect.get(index);
}
return resultat;
}
public Transposed sub(Transposed t){
......@@ -18,4 +28,33 @@ public class Transposed{
public Vector t(){
return this.v;
}
@Override
public String toString() {
List<String> strings = new ArrayList<String>();
for(int i=0;i<this.t().len();i++)
{
strings.add(Double.valueOf(this.t().get(i)).toString());
}
return "Transposed"+String.valueOf(strings);
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Transposed)
{
for(int i=0;i<this.t().len();i++)
{
if(((Transposed)obj).t().get(i)!=this.t().get(i))
{
return false;
}
}
return true;
}
else
{
return false;
}
}
}
......@@ -7,7 +7,7 @@ import java.util.function.Function;
final public class Vector {
private List<Double> lstVector;// = new ArrayList<Double>();
final private List<Double> lstVector;// = new ArrayList<Double>();
private Vector() {
this.lstVector=new ArrayList<Double>();
......@@ -19,15 +19,10 @@ final public class Vector {
{
lstVector.add(e);
}
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
private Vector(List<Double> elements) {
this.lstVector=new ArrayList<Double>();
for(double e:elements)
{
lstVector.add(e);
}
//throw new UnsupportedOperationException("This feature isn't implemented yet");
this.lstVector=elements;
}
public Transposed t() {
......@@ -35,78 +30,111 @@ final public class Vector {
}
public int len() {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.lstVector.size();
}
public double get(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.lstVector.get(i);
}
void set(int i, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
this.lstVector.set(i,value);
}
public Vector add(Vector that) {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.len();i++)
{
v.lstVector.add(Double.sum(this.lstVector.get(i),that.lstVector.get(i)));
v.lstVector.add(Double.sum(this.get(i),that.get(i)));
}
return v;
}
public Vector mul(double m) {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(double e:this.lstVector)
{
v.lstVector.add(this.lstVector.get(i)*m);
v.lstVector.add(e*m);
}
return v;
}
public Vector sub(Vector that) {
Vector v = new Vector();
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.len();i++)
{
v.lstVector.add(this.lstVector.get(i)-that.lstVector.get(i));
v.lstVector.add(this.get(i)-that.get(i));
}
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public double norm() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
double power=0;
for(double e:this.lstVector)
{
power+=(e*e);
}
return Math.sqrt(power);
}
public Vector sliceFrom(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return slice (i,this.len());
}
public Vector sliceTo(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return slice (0,i);
}
public Vector slice(int from, int to) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v =new Vector();
if (from>=to){
return v;
}
else
{
for (double d : this.lstVector.subList(from,to))
{
v.lstVector.add(d);
}
return v;
}
}
public Vector removed(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public Vector removed(int i)
{
Vector v = this.copy();
v.lstVector.remove(i);
return v;
}
public Vector concat(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public Vector concat(Vector that)
{
Vector v = Vector.zeros(this.len()+that.len());
for(int i=0;i<v.len();i++)
{
if(i<this.len())
{
v.set(i,this.get(i));
}
else
{
v.set(i,that.get(i-this.len()));
}
}
return v;
}
public Vector map(DoubleFunction<Double> f) {
public Vector map(DoubleFunction<Double> f) {//ne pas faire
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector copy() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public Vector copy()
{
Vector v = Vector.zeros(this.len());
for(int i=0;i<v.len();i++)
{
v.set(i,this.get(i));
}
return v;
}
void checkVectorLengthOrThrow(Vector that){
......@@ -114,63 +142,67 @@ final public class Vector {
}
public static Vector of(Double... elements) {
Vector v = new Vector();
for(double e:elements)
{
v.lstVector.add(e);
}
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
return new Vector(elements);
}
public static Vector of(List<Double> elements) {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(double e:elements)
{
v.lstVector.add(e);
}
return v;
return new Vector(elements);
}
public static Vector empty() {
Vector v = new Vector();
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
return new Vector();
}
public static Vector fill(int nb, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector zeros(int nb) {
Vector v = new Vector();
for(int i = 0; i < nb; i++)
{
v.lstVector.add(0.0);
v.lstVector.add(value);
}
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector zeros(int nb) {
return Vector.fill(nb,0.0);
}
public static Vector ones(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return Vector.fill(nb,1.0);
}
public static Vector linespace(double from, double to, int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public static Vector linespace(double from, double to, int nb)
{
double step=(to-from)/(nb-1);
Vector v = Vector.zeros(nb);
for(int i=0;i<nb;i++)
{
v.set(i,from+(step*i));
}
return v;
}
public static Vector tabulate(int nb, Function<Integer, Double> f) {
//ne pas faire
public static Vector tabulate(int nb, Function<Integer, Double> f)
{
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector sum(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public static Vector sum(List<Vector> vs)
{
int length=vs.get(0).len();
Vector v = Vector.zeros(length);
for(int e=0;e<length;e++)
{
for(Vector vLst:vs)
{
v.set(e,Double.sum(v.get(e),vLst.get(e)));
}
}
return v;
}
public static double norms(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return sum(vs).norm();
}
......@@ -181,25 +213,17 @@ final public class Vector {
// Apply formatting to the string if necessary
strings.add(d.toString());
}
if(this instanceof ch.hepia.numeric.Vector)
{
return "Vector"+String.valueOf(strings);
}
else
{
return "Transposed"+String.valueOf(strings);
}
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
/*
@Override
public boolean equals(Object obj) {
if(this instanceof ch.hepia.numeric.Vector)
if(obj instanceof Vector)
{
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.len();i++)
{
if(((Vector)obj).lstVector.get(i)!=this.lstVector.get(i))
if(((Vector)obj).get(i)!=this.get(i))
{
return false;
}
......@@ -208,10 +232,7 @@ final public class Vector {
}
else
{
//a ecrire
return true;
return false;
}
//throw new UnsupportedOperationException("boolean This feature isn't implemented yet");
}
*/
}
package ch.hepia.structure;
import java.util.List;
import java.util.ArrayList;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.Random;
final public class BinaryHeap
{
final private List<Integer> lstBinaryHeap;
private int taille;
public BinaryHeap()
{
this.lstBinaryHeap=new ArrayList<Integer>();
this.taille=0;
}
public void push(int value)
{
this.taille++;
this.lstBinaryHeap.add(value);
this.sort();
}
public int peek()
{
return this.get(0);
}
public int pop()
{
int val=this.peek();
this.set(0,0);
for(int i=0;i<this.depth();i++)
{
this.sort();
}
if(this.size()>0)
{
this.lstBinaryHeap.remove(this.size()-1);
this.taille--;
}
return val;
}
public void addAll(List<Integer> lst)
{
for(int val:lst)
{
this.push(val);
}
}
public boolean isEmpty()
{
if(this.size()==0)
{
return true;
}
else
{
return false;
}
}
public int depth()
{
int index=this.size();
int dptCompteur=1;
while(index!=0)
{
index=this.getParentNodeIndex(index);
dptCompteur++;
}
return dptCompteur;
}
public boolean exist(int k)
{
for(int val:this.lstBinaryHeap)
{
if(k==val)
{
return true;
}
}
return false;
}
public static void populate(BinaryHeap heap, int size)
{
Random rand = new Random();
for(int i=0;i<size;i++)
{
heap.push(rand.nextInt(100));
}
}
private void sort()
{
int index=this.size()-1;
while(index!=0)
{
if(this.getParentNodeValue(index)<this.get(index))
{
//les place doivent etre changer
this.switchPlace(this.getParentNodeIndex(index),index);
//test la valeur d'a cote
}
else
{
//test si la branche existe
if(this.getSameLevelIndex(index)<this.size())
{
//si elle existe
index=this.getSameLevelIndex(index);//passe a l'index d'a coter
if(this.getParentNodeValue(index)<this.get(index))
{
//les place doivent etre changer
this.switchPlace(this.getParentNodeIndex(index),index);
}
}
}
index=this.getParentNodeIndex(index);
}
}
private void switchPlace(int index1,int index2)
{
int tmp;
tmp=this.lstBinaryHeap.get(index1);
this.set(index1,this.get(index2));
this.set(index2,tmp);
}
public void print()
{
int index=0;
for(int etage=1;etage<=this.depth();etage++)//e
{
for(int nToShow=0;nToShow<Math.pow(2,etage-1);nToShow++)//n
{
if(index<this.size())
{
System.out.print(" "+this.get(index)+" ");
}
index++;
}
System.out.println("");
}
System.out.println("");
}
public int size()
{
return this.taille;
}
private int getSameLevelIndex(int index)
{
if(index%2==0)
{
return index-1;
}
else
{
return index+1;
}
}
private int getRightNodeIndex(int index)
{
return (2*index)+1;
}
private int getLeftNodeIndex(int index)
{
return (2*index)+2;
}
private int getParentNodeIndex(int index)
{
return (index-1) / 2;
}
private int getRightNodeValue(int index)
{
return this.get(this.getRightNodeIndex(index));
}
private int getLeftNodeValue(int index)
{
return this.get(this.getLeftNodeIndex(index));
}
private int getParentNodeValue(int index)
{
return this.get(this.getParentNodeIndex(index));
}
private int get(int index)
{
return this.lstBinaryHeap.get(index);
}
private void set(int index, int val)
{
this.lstBinaryHeap.set(index,val);
}
}
package ch.hepia.hangman;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
class HangmanTests
{
@Test
void check_create()
{
String hiddenWord= Hangman.createWord("david");
assertEquals(hiddenWord,"_____");
}
@Test
void check_test()
{
String hiddenWord= Hangman.createWord("david");
hiddenWord=Hangman.check(hiddenWord,"david",'d');
assertEquals(hiddenWord,"d___d");
}
@Test
void check_potence()
{
String potence =Hangman.showHangman(2);
assertEquals(potence,"----------\n|/ |\n|\n|\n|\n|\n|\n|\n");
}
}
......@@ -15,25 +15,26 @@ class VectorTest {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(-1.0, -2.0, -3.0);
Vector v3 = Vector.of(-1.0, -2.0, -3.0);
//Transposed t1= new Transposed(v2);
//System.out.println(v1.toString());
//assertEquals(v1.add(v2), Vector.of(0.0, 0.0, 0.0));
assertEquals(v1.add(v2).toString(), Vector.of(0.0, 0.0, 0.0).toString());
assertEquals(v1.sub(v2).toString(), Vector.of(2.0, 4.0, 6.0).toString());
assertEquals(v1.mul(3.0).toString(), Vector.of(3.0, 6.0, 9.0).toString());
Vector v4 = Vector.of(-50.0, -2.0, -3.0);
assertEquals(v1.add(v2), Vector.of(0.0, 0.0, 0.0));
assertEquals(v1.sub(v2), Vector.of(2.0, 4.0, 6.0));
assertEquals(v4.sub(v3), Vector.of(-49.0, 0.0, 0.0));
assertEquals(v1.mul(3.0), Vector.of(3.0, 6.0, 9.0));
assertEquals(v1.mul(-0.0), Vector.of(-0.0, -0.0, -0.0));
assertEquals(v1.mul(0.0), Vector.of(0.0, 0.0, 0.0));
assertEquals(Vector.empty().len(), 0);
assertEquals(Vector.of(1.0, 2.0, 1.0).len(), 3);
}
@Test
void vectorCreation() {
assertEquals(Vector.zeros(3).toString(), Vector.of(0.0, 0.0, 0.0).toString());
//assertEquals(Vector.ones(3), Vector.of(1.0, 1.0, 1.0));
assertEquals(Vector.zeros(3), Vector.of(0.0, 0.0, 0.0));
assertEquals(Vector.ones(3), Vector.of(1.0, 1.0, 1.0));
//assertEquals(Vector.of(1.0, 2.0).map( d -> d * 2.0), Vector.of(2.0, 4.0));
//assertEquals(Vector.fill(3, 1.7), Vector.of(1.7, 1.7, 1.7));
assertEquals(Vector.fill(3, 1.7), Vector.of(1.7, 1.7, 1.7));
//assertEquals(Vector.tabulate(4, i -> i*5.0), Vector.of(0.0, 5.0, 10.0, 15.0));
}
/*
@Test
void vectorLinespace() {
assertEquals(Vector.linespace(0.0, 1.0, 3), Vector.of(0.0, 0.5, 1.0));
......@@ -49,7 +50,9 @@ class VectorTest {
Vector.norms(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0)))
);
assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))), Vector.of(1.0, 2.0, 2.0));
assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))), Vector.of(1.0, 2.0, 2.0));
}
@Test
void vectorTranspose() {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
......@@ -64,15 +67,14 @@ class VectorTest {
assertEquals( v3.t().dot(v4), 14.0 );
assertEquals( v3.t().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t());
}
*/
@Test
void vectorToString() {
Vector v = Vector.of(1.0, 2.0, 3.0);
assertEquals(v.toString(), "Vector[1.0, 2.0, 3.0]");
//System.out.println(v.t().toString());
//assertEquals(v.t().toString(), "Transposed[1.0, 2.0, 3.0]");
assertEquals(v.t().toString(), "Transposed[1.0, 2.0, 3.0]");
}
/*
@Test
void vectorSlice() {
Vector v = Vector.of(1.0, 2.0, 3.0, 4.0, 5.0);
......@@ -83,11 +85,12 @@ class VectorTest {
assertEquals(v.slice(4,2), Vector.empty());
assertEquals(v.removed(2), Vector.of(1.0, 2.0, 4.0, 5.0));
}
@Test
void vectorConcat() {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(-1.0, -2.0, -3.0);
assertEquals(v1.concat(v2), Vector.of(1.0, 2.0, 3.0, -1.0, -2.0, -3.0));
}
*/
}
package ch.hepia.structure;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
class BinaryHeapTest
{
@Test
void BinaryHeapMainOpreationTest()
{
List<Integer> lst = new ArrayList<Integer>();
BinaryHeap bHeap= new BinaryHeap();
BinaryHeap bHeap2= new BinaryHeap();
BinaryHeap bHeapRand= new BinaryHeap();
BinaryHeap bHeapEmpty= new BinaryHeap();
BinaryHeap.populate(bHeapRand,20);
bHeap.push(100);lst.add(100);
bHeap.push(19);lst.add(19);
bHeap.push(36);lst.add(36);
bHeap.push(17);lst.add(17);
bHeap.push(3);lst.add(3);
bHeap.push(25);lst.add(25);
bHeap.push(1);lst.add(1);
bHeap.push(2);lst.add(2);
bHeap.push(7);lst.add(7);
bHeap.push(40);lst.add(40);
bHeap2.addAll(lst);
bHeap.print();
//bHeap2.print();
bHeapRand.print();
assertEquals(bHeap.size(),10);
assertEquals(bHeapRand.size(),20);
assertEquals(bHeapEmpty.isEmpty(),true);
assertEquals(bHeapRand.isEmpty(),false);
assertEquals(bHeap.exist(7),true);
assertEquals(bHeap.exist(42),false);
assertEquals(bHeap.depth(),4);
assertEquals(bHeap.peek(),100);
assertEquals(bHeap.pop(),100);
assertEquals(bHeap.pop(),40);
assertEquals(bHeap.pop(),36);
assertEquals(bHeap.size(),7);
//bHeap.print();
//assertEquals(bHeap,bHeap2);//ecrire un override pour equals
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment