Skip to content

Commit

Permalink
Merge pull request #9 from sisko59/master
Browse files Browse the repository at this point in the history
feat (java) : ajout d'un permutateur de valeur
  • Loading branch information
djedje72 authored Oct 19, 2018
2 parents 9f4658b + 8517b11 commit 513fee2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static List<String> printAllKLength(char[] set, int k) {
int n = set.length;
List<String> result = new ArrayList<>();
printAllKLengthRec(set, "", n, k, result);
result.stream().forEach(System.out::println);
//result.stream().forEach(System.out::println);
return result;
}

Expand Down Expand Up @@ -47,4 +47,19 @@ static void printAllKLengthRec(char[] set,
n, k - 1, list);
}
}

public static List<String> permutation(String str) {
List<String> result = new ArrayList<>();
permutation("", str, result);
return result;
}

private static void permutation(String prefix, String str, List<String> list) {
int n = str.length();
if (n == 0) list.add(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n), list);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package com.isograd.test;

import com.isograd.helper.GenerateValueForSize;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class GenerateValueForKTest {

@Test
public void printAllKLength() {
System.out.println("First Test");
public void printAllGenerate() {
char[] set1 = {'a', 'b'};
int k = 3;
GenerateValueForSize.printAllKLength(set1, k);

System.out.println("\nSecond Test");
char[] set2 = {'a', 'b', 'c', 'd'};
k = 1;
GenerateValueForSize.printAllKLength(set2, k);
List<String> actual = GenerateValueForSize.printAllKLength(set1, k);

System.out.println(actual);
Assert.assertTrue(8==actual.size());
Assert.assertTrue(actual.contains("aaa"));
Assert.assertTrue(actual.contains("aab"));
Assert.assertTrue(actual.contains("aba"));
Assert.assertTrue(actual.contains("baa"));
Assert.assertTrue(actual.contains("bbb"));
}

@Test
public void printAllPermutation() {
String set1 = "ab";

List<String> actual = GenerateValueForSize.permutation(set1);

Assert.assertTrue(2==actual.size());
Assert.assertTrue(actual.contains("ab"));
Assert.assertTrue(actual.contains("ba"));
}
}

0 comments on commit 513fee2

Please sign in to comment.