Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package Untoasted_Craven;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Feedback_2 {
  6.  
  7.     private static String morseAlphabet = " .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
  8.     private static String asciiAlphabet = " a b c d e f g h i j k l m n o p q r s t u v w x y z";
  9.  
  10.     public static void main(String[] args) {
  11.         // initialization
  12.         ArrayList<String> morseTable = createMorseLookupTable(morseAlphabet);
  13.         ArrayList<String> asciiTable = createAsciiLookupTable(asciiAlphabet);
  14.  
  15.         // note: we implicitly require the lookup tables to have the same size! (1 to 1 mapping)
  16.         assert (morseTable.size() == asciiTable.size());
  17.  
  18.         // "application part": our test code
  19.         String testWord = "brandon";
  20.         String smorsed = smorse(morseTable, asciiTable, splitIntoLetters(testWord));
  21.         System.out.println(smorsed);
  22.     }
  23.  
  24.     public static ArrayList<String> createMorseLookupTable(String alphabet) {
  25.         String[] letters = alphabet.split(" "); // note: it's sufficient to split only once!
  26.  
  27.         ArrayList<String> result = new ArrayList<String>();
  28.  
  29.         for (int i = 0; i < letters.length; i++) {
  30.             result.add(letters[i]);
  31.         }
  32.  
  33.         return result;
  34.     }
  35.  
  36.     public static ArrayList<String> createAsciiLookupTable(String alphabet) {
  37.         String[] letters = alphabet.split(" "); // note: it's sufficient to split only once!
  38.  
  39.         ArrayList<String> result = new ArrayList<String>();
  40.  
  41.         for (int i = 0; i < letters.length; i++) {
  42.             result.add(letters[i]);
  43.         }
  44.  
  45.         return result;
  46.     }
  47.  
  48.     public static ArrayList<String> splitIntoLetters(String word) {
  49.         String[] letters = word.split("a{0}"); // note: it's sufficient to split only once!
  50.  
  51.         ArrayList<String> result = new ArrayList<String>();
  52.  
  53.         for (int i = 0; i < letters.length; i++) {
  54.             result.add(letters[i]);
  55.         }
  56.  
  57.         return result;
  58.     }
  59.  
  60.     public static String smorse(ArrayList<String> morseTable, ArrayList<String> asciiTable, ArrayList<String> word) {
  61.         String result = "";
  62.  
  63.         int wordIndex = 0;
  64.  
  65.         for (int asciiIndex = 0; asciiIndex < asciiTable.size(); asciiIndex++) {
  66.             boolean lettersMatch = word.get(wordIndex).equals(asciiTable.get(asciiIndex));
  67.  
  68.             if (lettersMatch) {
  69.                 result += morseTable.get(asciiIndex);
  70.                 wordIndex++;
  71.                 asciiIndex = 0;
  72.             }
  73.  
  74.             if (wordIndex == word.size()) {
  75.                 break;
  76.             }
  77.         }
  78.  
  79.         return result;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement