Skip to content

Commit

Permalink
Soluzione proposta Lab4 - Permutazioni
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-sonny committed Apr 17, 2016
1 parent b4628c1 commit 0a9f9e8
Show file tree
Hide file tree
Showing 25 changed files with 182 additions and 705,055 deletions.
7 changes: 0 additions & 7 deletions Lab4_Anagrammi/bin/it/polito/tdp/anagrammi/Anagrammi.fxml

This file was deleted.

Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion Lab4_Anagrammi/bin/it/polito/tdp/anagrammi/application.css

This file was deleted.

8 changes: 0 additions & 8 deletions Lab4_Anagrammi/build.fxbuild

This file was deleted.

Binary file not shown.
7 changes: 0 additions & 7 deletions Lab4_Anagrammi/src/it/polito/tdp/anagrammi/Anagrammi.fxml

This file was deleted.

This file was deleted.

27 changes: 0 additions & 27 deletions Lab4_Anagrammi/src/it/polito/tdp/anagrammi/Main.java

This file was deleted.

1 change: 0 additions & 1 deletion Lab4_Anagrammi/src/it/polito/tdp/anagrammi/application.css

This file was deleted.

2 changes: 0 additions & 2 deletions Lab4_Anagrammi/.classpath → Lab4_Permutation/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.38-bin.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
8 changes: 1 addition & 7 deletions Lab4_Anagrammi/.project → Lab4_Permutation/.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lab4_Anagrammi</name>
<name>Lab4_Permutation</name>
<comment></comment>
<projects>
</projects>
Expand All @@ -10,14 +10,8 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file added Lab4_Permutation/bin/IterativePermutation.class
Binary file not shown.
Binary file added Lab4_Permutation/bin/Main.class
Binary file not shown.
Binary file added Lab4_Permutation/bin/RecursivePermutation1.class
Binary file not shown.
Binary file added Lab4_Permutation/bin/RecursivePermutation2.class
Binary file not shown.
Binary file added Lab4_Permutation/bin/Utils.class
Binary file not shown.
35 changes: 35 additions & 0 deletions Lab4_Permutation/src/IterativePermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.List;

public class IterativePermutation {

List<String> permutations = null;

public List<String> findPermutations(String word) {

permutations = new ArrayList<String>();

List<Character> wordArray = Utils.convertStringToList(word);
permutations.add(new String("" + wordArray.get(0)));

for (int j = 1; j < wordArray.size(); j++) {

char c = wordArray.get(j).charValue();

// Creo nuove permutazioni combinando c con tutte le altre
// permutazioni esistenti.

for (int i = permutations.size() - 1; i >= 0; i--) {

String str = permutations.remove(i);

for (int l = 0; l <= str.length(); l++) {

permutations.add(str.substring(0, l) + c + str.substring(l));
}
}
}

return permutations;
}
}
26 changes: 26 additions & 0 deletions Lab4_Permutation/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.List;

public class Main {

public static int n = 4;

public static void main(String[] args) {

String word = "Andrea";

RecursivePermutation1 rp1 = new RecursivePermutation1();
List<String> listResult = rp1.findPermutations1(word);
System.out.println("Numero di permutazioni: " + listResult.size());
System.out.println(listResult);

RecursivePermutation2 rp2 = new RecursivePermutation2();
listResult = rp2.findPermutations2(word);
System.out.println("Numero di permutazioni: " + listResult.size());
System.out.println(listResult);

IterativePermutation ip = new IterativePermutation();
listResult = ip.findPermutations(word);
System.out.println("Numero di permutazioni: " + listResult.size());
System.out.println(listResult);
}
}
37 changes: 37 additions & 0 deletions Lab4_Permutation/src/RecursivePermutation1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.ArrayList;
import java.util.List;

public class RecursivePermutation1 {

List<String> permutations = null;

public List<String> findPermutations1(String word) {

permutations = new ArrayList<String>();
recursivePermutations1(word, 0);

return permutations;
}

private void recursivePermutations1(String word, int passo) {
if (passo == word.length()) {
permutations.add(word);
return;
}

for (int i = passo; i < word.length(); i++) {

char[] chars = word.toCharArray();

// Genero una nuova soluzione
char temp = chars[passo];
chars[passo] = chars[i];
chars[i] = temp;

// Richiamo la funzione ricorsiva
recursivePermutations1(String.valueOf(chars), passo + 1);

// Trasformando l'array di chars non è necessario fare backtrack.
}
}
}
39 changes: 39 additions & 0 deletions Lab4_Permutation/src/RecursivePermutation2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.List;

public class RecursivePermutation2 {

List<String> permutations = null;

public List<String> findPermutations2(String word) {

permutations = new ArrayList<String>();
recursivePermutations2(new ArrayList<Character>(), word.toCharArray(), 0);

return permutations;
}

private void recursivePermutations2(List<Character> word, char[] characters, int passo) {

if (passo == characters.length) {
permutations.add(Utils.converListToString(word));
return;
}

for (int i = 0; i < characters.length; i++) {

// Controllo se il carattere i-esimo è già presente nella lista.
if (Utils.listCounter(word, characters[i]) < Utils.arrayCounter(characters, characters[i])) {
// Aggiungo un carattere alla mia word
word.add(Character.valueOf(characters[i]));

// Richiamo la funzione ricorsiva
recursivePermutations2(word, characters, passo + 1);

// Tolgo il carattere dalla mia word
word.remove(Character.valueOf(characters[i]));
}
}
}

}
44 changes: 44 additions & 0 deletions Lab4_Permutation/src/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.ArrayList;
import java.util.List;

public class Utils {

public static List<Character> convertStringToList(String word) {
List<Character> list = new ArrayList<Character>();
for (char c : word.toCharArray())
list.add(Character.valueOf(c));
return list;
}

public static String converListToString(List<Character> list){
StringBuilder sb = new StringBuilder();
for (Character c: list){
sb.append(c);
}
return sb.toString();
}

public static int listCounter(List<Character> word, char c) {

int listCounter = 0;

for (Character a: word)
if (a.charValue() == c)
listCounter++;

return listCounter;
}

public static int arrayCounter(char[] characters, char c) {

int arrayCounter = 0;

for (char a: characters) {
if (a == c)
arrayCounter++;
}

return arrayCounter;
}

}
Binary file removed ese04.pdf
Binary file not shown.
Loading

0 comments on commit 0a9f9e8

Please sign in to comment.