Advertisement
JeffGrigg

Untitled

Dec 25th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.80 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class SolutionTest extends TestCase {
  6.  
  7.     public void testMultipleValues() {
  8.         assertIsBeautiful("");
  9.         isNotBeautifiable("a");
  10.         isNotBeautifiable("aa");
  11.         assertIsBeautiful("ab");
  12.         isNotBeautifiable("abc");
  13.         assertIsBeautiful("aabb");
  14.         isBeautifiable("abba");
  15.         isBeautifiable("abbc");
  16.         isNotBeautifiable("abaa");
  17.         isNotBeautifiable("abcde");
  18.         assertIsBeautiful("aaabbb");
  19.         isNotBeautifiable("abaaab");
  20.         isBeautifiable("abccba");
  21.         isBeautifiable("aabbba");
  22.         isNotBeautifiable("abcdefghijklmnopqrstuvwxyza");
  23.         isBeautifiable(copies(80, "abcdefghijklmnopqrstuvwxyz") + "aa");
  24. //        isBeautifiable(copies(3846, "abcdefghijklmnopqrstuvwxyz") + "aa");
  25. //        isBeautifiable(copies(4000, "abcdefghijklmnopqrstuvwxyz") + "aa");
  26.         isNotBeautifiable("abbb");
  27.         isNotBeautifiable("aabbbb");
  28.     }
  29.  
  30.     private String copies(final int numberOfCopies, final String value) {
  31.         final StringBuilder stringBuilder = new StringBuilder();
  32.         for (int counter = 0; counter < numberOfCopies; ++counter) {
  33.             stringBuilder.append(value);
  34.         }
  35.         return stringBuilder.toString();
  36.     }
  37.  
  38.     private static void assertIsBeautiful(final String beautifulStringValue) {
  39.         assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", true, isBeautiful(beautifulStringValue));
  40.         assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", reverse(beautifulStringValue), Solution.makeBeautiful(beautifulStringValue));
  41.     }
  42.  
  43.     private static void isNotBeautifiable(final String original) {
  44.         assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
  45.         assertEquals("The String value <" + original + "> is NOT beautiful;", null, Solution.makeBeautiful(original));
  46.     }
  47.  
  48.     private static void isBeautifiable(final String original) {
  49.         System.out.println("isBeautifiable " + original.length() + " : " + original);
  50.         assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
  51.  
  52.         final String butified = Solution.makeBeautiful(original);
  53.         if (butified == null) {
  54.             fail("The String value <" + original + "> is should be beautifyable.");
  55.         } else {
  56.             assertEquals("The String value <" + original + "> is should be beautifyable, as value <" + butified + ">;",
  57.                     true, isBeautiful(butified));
  58.             assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
  59.                     original.length(), butified.length());
  60.             assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
  61.                     sortChars(original), sortChars(butified));
  62.         }
  63.     }
  64.  
  65.     private static boolean isBeautiful(final String value) {
  66.         if (value.length() == 0) {
  67.             return true;
  68.         }
  69.         final int centerIndex = value.length() / 2;
  70.         for (int charIdx = 0; charIdx <= centerIndex; ++charIdx) {
  71.             if (value.charAt(charIdx) == value.charAt(value.length() - charIdx - 1)) {
  72.                 return false;   // Found duplicate character.
  73.             }
  74.         }
  75.         return true;    // No duplicates found.
  76.     }
  77.  
  78.     private static String reverse(final CharSequence value) {
  79.         return new StringBuilder(value).reverse().toString();
  80.     }
  81.  
  82.     private static String sortChars(final String original) {
  83.         final char[] characters = original.toCharArray();
  84.         Arrays.sort(characters);
  85.         return new String(characters);
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement