Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package introToJava;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Solution {
- // raise digits to number of their occurrences
- public static String raiseToPower(String str) {
- StringBuilder sb = new StringBuilder();
- int elementsSum = 0;
- for(int i = 0; i < str.length(); i++) {
- int num = Character.getNumericValue(str.charAt(i));
- if(hasOccured(str, i) == false) {
- int numCount = countOccurences(str, i);
- num = pow(num, numCount);
- elementsSum += num;
- char charToAppend = toLetter(num);
- sb.append(charToAppend);
- }
- }
- //int sumOfElements = getSum(sb.toString());
- String cipher = vigenere(sb.toString(),elementsSum);
- sb.replace(0, sb.length(), cipher);
- return sb.toString();
- }
- // check if number has occurred before
- public static boolean hasOccured(String str, int index) {
- for(int i = index - 1; i >= 0; i--) {
- if(Character.getNumericValue(str.charAt(i)) == Character.getNumericValue(str.charAt(index))) {
- return true;
- }
- }
- return false;
- }
- // counts the number of occurrences
- public static int countOccurences(String str, int index) {
- int result = 1;
- for(int i = index + 1; i < str.length(); i++) {
- if(Character.getNumericValue(str.charAt(i)) == Character.getNumericValue(str.charAt(index))) {
- result++;
- }
- }
- return result;
- }
- // raises base to exp power
- public static int pow(int base, int exp) {
- int result = 1;
- for( ; exp > 0; exp--) {
- result *= base;
- }
- return result;
- }
- public static char toLetter(int number) {
- char c = (char)((number%26)+'A'-1);
- return c;
- }
- // public static int getSum(String str) {
- // int sum = 0;
- //
- // for(int i = 0; i < str.length(); i++) {
- // sum += (int)str.charAt(i) - 64;
- // }
- //
- // return sum;
- // }
- public static String vigenere(String str, int sumOfElements) {
- StringBuilder sb = new StringBuilder();
- sb.append(str);
- // find number of digits
- int digitCount = 0;
- int tempSum = sumOfElements;
- while(tempSum != 0) {
- tempSum /= 10;
- digitCount++;
- }
- //put digits in an array
- int[] arrayOfDigits = new int[digitCount];
- int arrayIndex = digitCount-1;
- tempSum = sumOfElements;
- while(tempSum != 0) {
- int digit = tempSum % 10;
- tempSum /= 10;
- arrayOfDigits[arrayIndex--] = digit;
- }
- //digits.remove(digits.size()-1);
- tempSum = sumOfElements;
- for(int i = 0; i < str.length(); i++) {
- if(digitCount < 2) { // if only 1 digit, then all letter must be increased by it
- //retrieve letter at this position
- int retrieved = (int)str.charAt(i) + sumOfElements;
- if(retrieved > 90) {
- retrieved = (64 + sumOfElements) - (90 - (int)str.charAt(i)); //ascii ('A'-1 + sumOfElements) - ('Z' - charAt(i))
- }
- char charToReplace = (char)retrieved;
- sb.setCharAt(i, charToReplace);
- }
- else { // else implement vigenere cipher
- for(int j = 0; j < digitCount && i < str.length(); j++, i++) {
- int retrieved = (int)str.charAt(i) + arrayOfDigits[j];
- if(retrieved > 90) {
- retrieved = (64 + arrayOfDigits[j]) - (90 - (int)str.charAt(i)); //ascii ('A'-1 + digit) - ('Z' - charAt(i))
- }
- char charToReplace = (char)retrieved;
- sb.setCharAt(i, charToReplace);
- }
- i--;
- }
- }
- return sb.toString();
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String userInput = input.nextLine();
- StringBuilder s = new StringBuilder();
- s.append(userInput);
- String raisedString = raiseToPower(userInput);
- System.out.println(raisedString);
- // StringBuilder sb = new StringBuilder();
- // sb.append(userInput);
- // int num = getSum(sb.toString());
- //
- // System.out.println(num);
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement