Skip to content

Instantly share code, notes, and snippets.

@NurseyitTursunkulov
Created February 28, 2018 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NurseyitTursunkulov/8688b49a0751753c61de558488ff5ab1 to your computer and use it in GitHub Desktop.
Save NurseyitTursunkulov/8688b49a0751753c61de558488ff5ab1 to your computer and use it in GitHub Desktop.
test2
package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
static int counter = 0;
static int startPosition = 0;
public static String alfavit2 = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) {
// write your code here
String textFromUser = getTextFromUser();
char[] wordsOfText = textFromUser.toCharArray();
for (int i = 0; i<wordsOfText.length;i++){
count(wordsOfText[i]);
}
System.out.println(counter);
}
private static void count(char bukva) {
ArrayList<Character> leftSection = divideToLeftSection(startPosition);
ArrayList<Character> rightSection = divideToRighSection(startPosition);
if (leftSection.contains(bukva)){
int index= leftSection.indexOf(bukva);
incrementCounter(bukva,index);
}
else if (rightSection.contains(bukva)){
Collections.reverse(rightSection);
int index= rightSection.indexOf(bukva);
incrementCounter(bukva, index);
}
}
private static void incrementCounter(char bukva, int index) {
counter = counter+ index;
int start = alfavit2.indexOf(bukva);
startPosition = start;
}
private static ArrayList<Character> divideToLeftSection(int startPosition) {
int endPosition = startPosition + 13;
ArrayList<Character> leftSection = new ArrayList<Character>();
initSection(startPosition, endPosition, leftSection);
return leftSection;
}
private static void initSection(int startPosition, int endPosition, ArrayList<Character> section) {
for (int i = startPosition;i <=endPosition; i++){
if (i >= 26 ){
section.add(alfavit2.charAt(i - 26));
}
else {
section.add(alfavit2.charAt(i));
}
}
}
private static ArrayList<Character> divideToRighSection(int startPosition){
ArrayList<Character> rightSection = new ArrayList<Character>();
int startPostOfrightSection = startPosition + 13;
int endposition = startPostOfrightSection + 13;
initSection(startPostOfrightSection, endposition, rightSection);
return rightSection;
}
private static String getTextFromUser() {
String input = "";
BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));
try {
input = inputStream.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment