-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Soluzione proposta Lab4 - Permutazioni
- Loading branch information
jimmy-sonny
committed
Apr 17, 2016
1 parent
b4628c1
commit 0a9f9e8
Showing
25 changed files
with
182 additions
and
705,055 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-330 Bytes
Lab4_Anagrammi/bin/it/polito/tdp/anagrammi/AnagrammiController.class
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
Lab4_Anagrammi/src/it/polito/tdp/anagrammi/AnagrammiController.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.