Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • steven.liatti/poo2019numeric
  • prosperm.takougou/poo2019numeric
  • francois.hofer/poo2019numeric
3 results
Select Git revision
Show changes
Commits on Source (15)
File added
File added
public class Hangman {
private String secretWord;
private String currWord;
private int attemp;
private int failedAttempt;
private boolean lastTry;
private Hangman(String word){
this.secretWord = word;
this.currWord = "_".repeat(word.length());
this.attemp = 0;
this.failedAttempt = 0;
this.lastTry = true;
}
public static Hangman withSecretWord(String word){
return new Hangman(word);
}
private String currentWord(){
return this.currWord;
}
public void printCurrentWord(){
System.out.println("Current Word:" + this.currentWord());
}
private int attemps(){
return this.attemp;
}
private String secretWord(){
return this.secretWord;
}
private boolean lastTry(){
return this.lastTry;
}
public int failedAttempts(){
return this.failedAttempt;
}
public void printGallow(){
printHang(this.failedAttempts());
}
public void printGallowIfError(){
if (!this.lastTry()){
printHang(this.failedAttempts());
}
}
private String check(char letter){
StringBuilder newStr = new StringBuilder(this.currentWord());
for ( int i = 0; i < this.secretWord().length(); i++){
if (this.secretWord().charAt(i) == letter){
newStr.setCharAt(i, letter);
}
}
return newStr.toString();
}
public Hangman tryWith(char letter){
Hangman newH = this;
newH.attemp++;
String checked = check(letter);
if (newH.currentWord().equals(checked)){
newH.failedAttempt++;
newH.lastTry = false;
return newH;
}
newH.lastTry = true;
newH.currWord = checked;
return newH;
}
public boolean isFinished(){
if (this.failedAttempts() == 9 || !this.currentWord().contains("_")){
return true;
}
return false;
}
public boolean hasWon(){
if (this.failedAttempts()==9){
return false;
}
return true;
}
private static void printHang(int i){
String i0 = "";
String i1 = "----------\n|/\n|\n|\n|\n|\n|\n|";
String i2 = "----------\n|/ |\n|\n|\n|\n|\n|\n|";
String i3 = "----------\n|/ |\n| (.)\n|\n|\n|\n|\n|";
String i4 = "----------\n|/ |\n| (̣̣ )\n| |\n|\n|\n|\n|";
String i5 = "----------\n|/ |\n| (̣̣ )\n| /|\n|\n|\n|\n|";
String i6 = "----------\n|/ |\n| (̣̣ )\n| /|\\ \n|\n|\n|\n|";
String i7 = "----------\n|/ |\n| (̣̣ )\n| /|\\ \n| | \n|\n|\n|";
String i8 = "----------\n|/ |\n| (̣̣ )\n| /|\\\n| | \n| / \n|\n|";
String i9 = "----------\n|/ |\n| (̣̣ )\n| /|\\\n| | \n| / \\\n|\n|";
String [] drawHangMan = {i0, i1, i2, i3, i4, i5, i6, i7, i8, i9};
System.out.println(drawHangMan[i]);
}
public static void main(String[] args){
Hangman h = Hangman.withSecretWord("mystère");
String currentWord = h.currentWord();
}
}
\ No newline at end of file
package ch.hepia.numeric;
import java.util.List;
import java.util.ArrayList;
public class Transposed{
private List<Double> vecT;
public Transposed(){
this.vecT = new ArrayList<>();
};
public Transposed(Vector That){
this.vecT = new ArrayList<>();
for (double elem : That.getValues()){vecT.add(elem);}
};
public List<Double> getValues(){
return this.vecT;
}
public int len() {
return this.getValues().size();
}
public double get(int i) {
return this.getValues().get(i);
}
public double dot(Vector That){
double res = 0.0;
Integer count = 0;
for (double coo : this.vecT) {
res = res + (coo * That.get(count));
count = count + 1;
}
return res;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
};
public Transposed sub(Transposed That){
Transposed resT = new Transposed();
Integer count = 0;
for (double coo : this.getValues()) {
resT.vecT.add(coo - That.getValues().get(count));
count = count + 1;
}
return resT;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
};
@Override
public String toString() {
String tr = "Transposed[";
for (double val: this.getValues()){
tr = tr.concat(val + ", ");
}
tr = tr.substring(0, tr.length()-2).concat("]");
return tr;
}
@Override
public boolean equals(Object obj){
if (this == obj) return true;
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Transposed tra = (Transposed) obj;
if (tra.len() != this.len()){
return false;
}
for (int i=0;i<tra.len();i++){
if (Double.compare(tra.getValues().get(i), this.getValues().get(i))!=0){
return false;
}
}
return true;
}
}
\ No newline at end of file
package ch.hepia.numeric; package ch.hepia.numeric;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.function.DoubleFunction; import java.util.function.DoubleFunction;
import java.util.function.Function; import java.util.function.Function;
final public class Vector { final public class Vector {
private Vector() {} private List<Double> values;
private Vector() {
this.values = new ArrayList<>();
}
private Vector(Double... elements) { private Vector(Double... elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> vec = new ArrayList<>();
for (double elem : elements){vec.add(elem);}
this.values = vec;
} }
private Vector(List<Double> elements) { private Vector(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); this.values = elements;
} }
public Transposed t() { public Transposed t() {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return new Transposed(this);
}
public List<Double> getValues(){
return this.values;
} }
public int len() { public int len() {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return this.getValues().size();
} }
public double get(int i) { public double get(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return this.getValues().get(i);
} }
void set(int i, double value) { void set(int i, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); this.values.set(i, value);
} }
public Vector add(Vector that) { public Vector add(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for(int i=0; i < this.len();i++){
newVec.add(this.get(i)+that.get(i));
}
return new Vector(newVec);
} }
public Vector mul(double m) { public Vector mul(double m) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int i=0; i<this.len();i++){
newVec.add(this.get(i)*m);
}
return new Vector(newVec);
} }
public Vector sub(Vector that) { public Vector sub(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return this.add(that.mul(-1.0));
} }
public double norm() { public double norm() {
throw new UnsupportedOperationException("This feature isn't implemented yet"); double sum = 0.0;
for (int i=0; i<this.len();i++){
sum += Math.pow(this.get(i),2.0);
}
return Math.sqrt(sum);
} }
public Vector sliceFrom(int i) { public Vector sliceFrom(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int j=0; j < this.len();j++){
if (j >= i){
newVec.add(this.get(j));
}
}
return new Vector(newVec);
} }
public Vector sliceTo(int i) { public Vector sliceTo(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int j=0; j < this.len();j++){
if (j < i){
newVec.add(this.get(j));
}
}
return new Vector(newVec);
} }
public Vector slice(int from, int to) { public Vector slice(int from, int to) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); Vector newVec = this;
newVec = newVec.sliceFrom(from);
newVec = newVec.sliceTo(to-from);
return newVec;
} }
public Vector removed(int i) { public Vector removed(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = this.getValues();
newVec.remove(i);
return new Vector(newVec);
} }
public Vector concat(Vector that) { public Vector concat(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = this.getValues();
newVec.addAll(that.getValues());
return new Vector(newVec);
} }
public Vector map(DoubleFunction<Double> f) { public Vector map(DoubleFunction<Double> f) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> accVec = this.getValues();
List<Double> newVec = new ArrayList<>();
for (Double val : accVec){
newVec.add(f.apply(val));
}
return new Vector(newVec);
} }
public Vector copy() { public Vector copy() {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return new Vector(this.getValues());
} }
void checkVectorLengthOrThrow(Vector that){ void checkVectorLengthOrThrow(Vector that){
...@@ -77,53 +125,97 @@ final public class Vector { ...@@ -77,53 +125,97 @@ final public class Vector {
} }
public static Vector of(Double... elements) { public static Vector of(Double... elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return new Vector(elements);
} }
public static Vector of(List<Double> elements) { public static Vector of(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return new Vector(elements);
} }
public static Vector empty() { public static Vector empty() {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return new Vector();
} }
public static Vector fill(int nb, double value) { public static Vector fill(int nb, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int i = 0; i < nb ; i++){
newVec.add(value);
}
return new Vector(newVec);
} }
public static Vector zeros(int nb) { public static Vector zeros(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int i = 0; i < nb ; i++){
newVec.add(0.0);
}
return new Vector(newVec);
} }
public static Vector ones(int nb) { public static Vector ones(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int i = 0; i < nb ; i++){
newVec.add(1.0);
}
return new Vector(newVec);
} }
public static Vector linespace(double from, double to, int nb) { public static Vector linespace(double from, double to, int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); double diff = to - from;
double hop = diff / (nb-1);
List<Double> newVec = new ArrayList<>();
for (int i = 0; i <= nb -1; i++){
newVec.add(hop*i);
}
return new Vector(newVec);
} }
public static Vector tabulate(int nb, Function<Integer, Double> f) { public static Vector tabulate(int nb, Function<Integer, Double> f) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); List<Double> newVec = new ArrayList<>();
for (int i=0; i < nb; i++){
newVec.add(f.apply(i));
}
return new Vector(newVec);
} }
public static Vector sum(List<Vector> vs) { public static Vector sum(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); Vector totalVec = Vector.zeros(vs.get(0).len());
for (Vector vec : vs){
totalVec = totalVec.add(vec);
}
return totalVec;
} }
public static double norms(List<Vector> vs) { public static double norms(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); return Vector.sum(vs).norm();
} }
@Override @Override
public String toString() { public String toString() {
throw new UnsupportedOperationException("This feature isn't implemented yet"); String vec = "Vector[";
for (double val: this.getValues()){
vec = vec.concat(val + ", ");
}
vec = vec.substring(0, vec.length()-2).concat("]");
return vec;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
throw new UnsupportedOperationException("This feature isn't implemented yet"); if (this == obj) return true;
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Vector vec = (Vector) obj;
if (vec.len() != this.len()){
return false;
}
for (int i=0;i<vec.len();i++){
if (Double.compare(vec.getValues().get(i), this.getValues().get(i))!=0){
return false;
}
}
return true;
} }
} }
\ No newline at end of file
public class BinaryHeap{
private int[] heap;
public BinaryHeap(){
this.heap = new int[10];
}
public BinaryHeap(int[] values){
int[] newHeap = new int[values.length];
for (int value : values){
for (int i = 0; i < newHeap.length; i++){
}
System.out.println(newHeap[1]);
}
this.heap = newHeap;
}
public static void main(String[] args){
BinaryHeap test = new BinaryHeap(new int[]{10,2});
}
}
\ No newline at end of file
package ch.hepia.numeric; package ch.hepia.numeric;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//import sun.jvmstat.monitor.VmIdentifier;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
...@@ -20,6 +23,7 @@ class VectorTest { ...@@ -20,6 +23,7 @@ class VectorTest {
assertEquals(Vector.empty().len(), 0); assertEquals(Vector.empty().len(), 0);
assertEquals(Vector.of(1.0, 2.0, 1.0).len(), 3); assertEquals(Vector.of(1.0, 2.0, 1.0).len(), 3);
} }
@Test @Test
void vectorCreation() { void vectorCreation() {
assertEquals(Vector.zeros(3), Vector.of(0.0, 0.0, 0.0)); assertEquals(Vector.zeros(3), Vector.of(0.0, 0.0, 0.0));
...@@ -28,11 +32,13 @@ class VectorTest { ...@@ -28,11 +32,13 @@ class VectorTest {
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)); assertEquals(Vector.tabulate(4, i -> i*5.0), Vector.of(0.0, 5.0, 10.0, 15.0));
} }
@Test @Test
void vectorLinespace() { void vectorLinespace() {
assertEquals(Vector.linespace(0.0, 1.0, 3), Vector.of(0.0, 0.5, 1.0)); assertEquals(Vector.linespace(0.0, 1.0, 3), Vector.of(0.0, 0.5, 1.0));
assertEquals(Vector.linespace(0.0, 1.0, 5), Vector.of(0.0, 0.25, 0.5, 0.75, 1.0)); assertEquals(Vector.linespace(0.0, 1.0, 5), Vector.of(0.0, 0.25, 0.5, 0.75, 1.0));
} }
@Test @Test
void vectorNormAndSum() { void vectorNormAndSum() {
assertEquals(Vector.of(1.0, 2.0, 2.0).norm(), 3.0); assertEquals(Vector.of(1.0, 2.0, 2.0).norm(), 3.0);
...@@ -43,6 +49,7 @@ class VectorTest { ...@@ -43,6 +49,7 @@ class VectorTest {
); );
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 @Test
void vectorTranspose() { void vectorTranspose() {
Vector v1 = Vector.of(1.0, 2.0, 3.0); Vector v1 = Vector.of(1.0, 2.0, 3.0);
......