Advertisement
yasenst

that works

Sep 20th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package introToJava;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class Solution {
  6.    
  7.     // raise digits to number of their occurrences
  8.     public static String raiseToPower(String str) {
  9.         StringBuilder sb = new StringBuilder();
  10.         int elementsSum = 0;
  11.         for(int i = 0; i < str.length(); i++) {
  12.             int num = Character.getNumericValue(str.charAt(i));
  13.            
  14.            
  15.                 if(hasOccured(str, i) == false) {
  16.                     int numCount = countOccurences(str, i);
  17.                     num = pow(num, numCount);
  18.                     elementsSum += num;
  19.                     char charToAppend = toLetter(num);
  20.                     sb.append(charToAppend);
  21.                 }
  22.            
  23.         }
  24.         //int sumOfElements = getSum(sb.toString());
  25.         String cipher = vigenere(sb.toString(),elementsSum);
  26.         sb.replace(0, sb.length(), cipher);
  27.         return sb.toString();
  28.     }
  29.    
  30.     // check if number has occurred before
  31.     public static boolean hasOccured(String str, int index) {
  32.         for(int i = index - 1; i >= 0; i--) {
  33.             if(Character.getNumericValue(str.charAt(i)) == Character.getNumericValue(str.charAt(index))) {
  34.                 return true;
  35.             }
  36.         }
  37.         return false;
  38.     }
  39.    
  40.     // counts the number of occurrences
  41.     public static int countOccurences(String str, int index) {
  42.         int result = 1;
  43.         for(int i = index + 1; i < str.length(); i++) {
  44.             if(Character.getNumericValue(str.charAt(i)) == Character.getNumericValue(str.charAt(index))) {
  45.                 result++;
  46.             }
  47.         }
  48.         return result;
  49.     }
  50.    
  51.     // raises base to exp power
  52.     public static int pow(int base, int exp) {
  53.         int result = 1;
  54.         for( ; exp > 0; exp--) {
  55.             result *= base;
  56.         }
  57.         return result;
  58.     }
  59.    
  60.     public static char toLetter(int number) {  
  61.        
  62.         char c = (char)((number%26)+'A'-1);
  63.        
  64.         return c;
  65.        
  66.     }
  67.    
  68. //  public static int getSum(String str) {
  69. //      int sum = 0;
  70. //     
  71. //      for(int i = 0; i < str.length(); i++) {
  72. //          sum += (int)str.charAt(i) - 64;
  73. //      }
  74. //     
  75. //      return sum;
  76. //  }
  77.    
  78.     public static String vigenere(String str, int sumOfElements) {
  79.         StringBuilder sb = new StringBuilder();
  80.         sb.append(str);
  81.        
  82.         // find number of digits
  83.         int digitCount = 0;
  84.         int tempSum = sumOfElements;
  85.        
  86.         while(tempSum != 0) {
  87.             tempSum /= 10;
  88.             digitCount++;
  89.         }
  90.        
  91.         //put digits in an array
  92.         int[] arrayOfDigits = new int[digitCount];
  93.         int arrayIndex = digitCount-1;
  94.         tempSum = sumOfElements;
  95.        
  96.         while(tempSum != 0) {
  97.             int digit = tempSum % 10;
  98.             tempSum /= 10;
  99.             arrayOfDigits[arrayIndex--] = digit;
  100.         }
  101.         //digits.remove(digits.size()-1);
  102.        
  103.        
  104.         tempSum = sumOfElements;
  105.        
  106.        
  107.         for(int i = 0; i < str.length(); i++) {
  108.             if(digitCount < 2) { // if only 1 digit, then all letter must be increased by it
  109.                 //retrieve letter at this position
  110.                 int retrieved = (int)str.charAt(i) + sumOfElements;
  111.                 if(retrieved > 90) {
  112.                     retrieved = (64 + sumOfElements) - (90 - (int)str.charAt(i)); //ascii ('A'-1 + sumOfElements) - ('Z' - charAt(i))
  113.                 }
  114.                 char charToReplace = (char)retrieved;
  115.                 sb.setCharAt(i, charToReplace);
  116.             }
  117.             else { // else implement vigenere cipher
  118.                 for(int j = 0; j < digitCount && i < str.length(); j++, i++) {
  119.                     int retrieved = (int)str.charAt(i) + arrayOfDigits[j];
  120.                     if(retrieved > 90) {
  121.                         retrieved = (64 + arrayOfDigits[j]) - (90 - (int)str.charAt(i)); //ascii ('A'-1 + digit) - ('Z' - charAt(i))
  122.                     }
  123.                     char charToReplace = (char)retrieved;
  124.                     sb.setCharAt(i, charToReplace);
  125.                 }
  126.                 i--;
  127.             }
  128.         }
  129.        
  130.         return sb.toString();
  131.     }
  132.  
  133.     public static void main(String[] args) {
  134.        
  135.         Scanner input = new Scanner(System.in);
  136.         String userInput = input.nextLine();
  137.         StringBuilder s = new StringBuilder();
  138.         s.append(userInput);
  139.         String raisedString = raiseToPower(userInput);
  140.         System.out.println(raisedString);
  141.  
  142.        
  143. //      StringBuilder sb = new StringBuilder();
  144. //      sb.append(userInput);
  145. //      int num = getSum(sb.toString());
  146. //     
  147. //      System.out.println(num);
  148.    
  149.        
  150.         input.close();
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement