TP01 + TP02 Fibbo
This commit is contained in:
parent
f39b5c0f50
commit
7f720447c3
@ -1,7 +1,5 @@
|
||||
import java.util.Collections;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class Frequence<T> {
|
||||
public T mostFrequent(T[] tab) {
|
||||
|
@ -2,12 +2,22 @@ public class Association<T> {
|
||||
private T element;
|
||||
private int frequency;
|
||||
|
||||
private Association<T> suivant;
|
||||
|
||||
|
||||
public Association(T e, int f) {
|
||||
this.element = e;
|
||||
this.frequency = f;
|
||||
}
|
||||
|
||||
public Association<T> getSuivant() {
|
||||
return suivant;
|
||||
}
|
||||
|
||||
public void setSuivant(Association<T> suivant) {
|
||||
this.suivant = suivant;
|
||||
}
|
||||
|
||||
public void setElement(T e) {
|
||||
this.element = e;
|
||||
}
|
||||
@ -24,10 +34,6 @@ public class Association<T> {
|
||||
return this.frequency;
|
||||
}
|
||||
|
||||
public Association suivant() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + this.element + ", " + this.frequency + "]";
|
||||
|
@ -0,0 +1,59 @@
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
|
||||
public class Frequences<T> {
|
||||
public T mostFrequent(T[] tab) {
|
||||
Hashtable<T, Integer> indexList = new Hashtable<T, Integer>();
|
||||
|
||||
for (T element : tab) {
|
||||
indexList.put(element, indexList.get(element) == null ? 1 : ((Integer)indexList.get(element)) + 1);
|
||||
}
|
||||
|
||||
T element = tab[0];
|
||||
Integer count = indexList.get(tab[0]);
|
||||
|
||||
for(Map.Entry<T, Integer> entry : indexList.entrySet()) {
|
||||
if (entry.getValue() > count) {
|
||||
element = entry.getKey();
|
||||
count = entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
public Liste<T> getFrequences(T[] tab) {
|
||||
Hashtable<T, Integer> indexList = new Hashtable<T, Integer>();
|
||||
|
||||
for (T element : tab) {
|
||||
indexList.put(element, indexList.get(element) == null ? 1 : ((Integer)indexList.get(element)) + 1);
|
||||
}
|
||||
|
||||
Association<T> lastEntry = null;
|
||||
boolean first = true;
|
||||
|
||||
Liste<T> list = new Liste<T>();
|
||||
|
||||
for(Map.Entry<T, Integer> entry : indexList.entrySet()) {
|
||||
if (first) {
|
||||
lastEntry = new Association<T>(entry.getKey(), entry.getValue());
|
||||
list.setFirstElement(lastEntry);
|
||||
first = false;
|
||||
} else {
|
||||
Association<T> newEntry = new Association<T>(entry.getKey(), entry.getValue());
|
||||
lastEntry.setSuivant(newEntry);
|
||||
lastEntry = newEntry;
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Frequences<String> f = new Frequences<String>();
|
||||
|
||||
String[] tab = {"test", "test", "test", "12", "12", "12", "12"};
|
||||
|
||||
System.out.println(f.mostFrequent(tab));
|
||||
}
|
||||
}
|
41
DEV 3.2/TP01/Fréquences/Liste.java
Normal file
41
DEV 3.2/TP01/Fréquences/Liste.java
Normal file
@ -0,0 +1,41 @@
|
||||
public class Liste<T> {
|
||||
private Association<T> a;
|
||||
|
||||
public Liste() {
|
||||
}
|
||||
|
||||
public void setFirstElement(Association<T> e) {
|
||||
this.a = e;
|
||||
}
|
||||
|
||||
public void add(T element) {
|
||||
Association<T> b = a;
|
||||
|
||||
boolean found = false;
|
||||
while (b.getSuivant() != null) {
|
||||
if (b.getElement().equals(element)) {
|
||||
b.setFrequency(b.getFrequency() + 1);
|
||||
found = true;
|
||||
break;
|
||||
} else b = b.getSuivant();
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
b.setSuivant(new Association<T>(element, 1));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String str = "";
|
||||
|
||||
Association<T> b = a;
|
||||
while (b.getSuivant() != null) {
|
||||
str += b.toString() + ", ";
|
||||
b = b.getSuivant();
|
||||
}
|
||||
|
||||
str = str.substring(0, str.length() - 2);
|
||||
return str;
|
||||
}
|
||||
}
|
33
DEV 3.2/TP02/Appels/Appels.java
Normal file
33
DEV 3.2/TP02/Appels/Appels.java
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Appels
|
||||
*/
|
||||
public class Appels {
|
||||
|
||||
private static int factorielle(int n, int indent) {
|
||||
for (int i = 0; i < indent; i++) System.out.print(" ");
|
||||
System.out.println("input: "+ n);
|
||||
|
||||
int res;
|
||||
switch (n) {
|
||||
case 0:
|
||||
res = 0;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
res = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
res = n * factorielle(n-1, indent+1);
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < indent; i++) System.out.print(" ");
|
||||
System.out.println("output: " + res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(factorielle(7, 0));
|
||||
}
|
||||
}
|
48
DEV 3.2/TP02/Tableaux/Tableaux.java
Normal file
48
DEV 3.2/TP02/Tableaux/Tableaux.java
Normal file
@ -0,0 +1,48 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Tableaux {
|
||||
|
||||
public static int[] getTab(String[] args, int index, int[] tab) {
|
||||
if (index == 0) {
|
||||
if (args.length == 0) return new int[0];
|
||||
tab = new int[args.length];
|
||||
tab[0] = Integer.parseInt(args[0]);
|
||||
getTab(args, index + 1, tab);
|
||||
} else if (index < args.length) {
|
||||
tab[index] = Integer.parseInt(args[index]);
|
||||
getTab(args, index + 1, tab);
|
||||
}
|
||||
|
||||
return tab;
|
||||
}
|
||||
|
||||
public static int maxN(int[] tab, int index) {
|
||||
if (index < tab.length) {
|
||||
int n = maxN(tab, index + 1);
|
||||
return tab[index] > n ? tab[index] : n;
|
||||
} else return Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
public static int getEven(int[] tab, int index, int n) {
|
||||
if (index == 0) n = 0;
|
||||
|
||||
|
||||
if (index < tab.length) {
|
||||
return (tab[index] % 2 == 0 ? 1 : 0) + getEven(tab, index + 1, n);
|
||||
} else return 0;
|
||||
}
|
||||
|
||||
public static void showTab(int[] tab, int index) {
|
||||
if (index < tab.length) {
|
||||
showTab(tab, index + 1);
|
||||
System.out.print(tab[index] + " ");
|
||||
} if (index == 0) System.out.print("\n");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] tab = getTab(args, 0, null);
|
||||
System.out.println(getEven(tab, 0, 0));
|
||||
System.out.println(maxN(tab, 0));
|
||||
showTab(tab, 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user