Created
April 14, 2023 01:48
-
-
Save rluisb/edc8efca639c45894cfed7c92736808a to your computer and use it in GitHub Desktop.
Exercise solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Click `Run` to execute the snippet below! | |
*/ | |
import java.util.*; | |
/* | |
Description | |
An ancient language was recently uncovered which appears to be a close English language derivative. A group of researchers requires a program to translate English into this ancient text. Your task is to write a simple program to perform basic English to foreign language translation. | |
Translation rules | |
1. Separate each word into two parts. The first part is called the "prefix" and extends from the beginning of the word up to, but not including, the first vowel. The Rest of the word is called the "stem." | |
2. The translated text is formed by reversing the order of the prefix and stem and adding the letters "ay" to the end. For example, "sandwich." is composed of "s" + "andwich" + "ay" + "." and would translate to "andwichsay." | |
3. Preserve punctuation | |
Sample session | |
--> stop | |
opstay | |
--> no littering! | |
onay itteringlay! | |
--> no shirts, no shoes, no service | |
onay irtsshay, onay oesshay, onay ervicesay | |
Hints (There are many ways to solve this, you don't have to use this, but it's here if you want them) | |
A String can be split into a character array like so: | |
char[] letters = <String>.toLowerCase().toCharArray(); | |
The Character class has many useful static methods. See the provided JavaDocs, but here are a few: | |
Character.isDigit(c) Character.isLetter(c) | |
*/ | |
class Solution { | |
public static void main(String[] args) { | |
List<String> strings = new ArrayList<String>(); | |
strings.add("stop"); | |
strings.add("no littering!"); | |
strings.add("no shirts, no shoes, no service"); | |
strings.add("not !valid"); | |
for (String input : strings) { | |
System.out.println(translate(input)); | |
} | |
} | |
// not !valid -> otnay !alidvay | |
public static String translate(String input) { | |
List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u'); | |
StringBuilder resultBuilder = new StringBuilder(); | |
List<String> words = Arrays.asList(input.split(" ")); | |
words.forEach(word -> { | |
var indexOfFirstVowel = 0; | |
for (int i = 0; i < word.length(); i++) { | |
var currentChar = word.charAt(i); | |
if (vowels.contains(currentChar)) { | |
indexOfFirstVowel = i; | |
break; | |
} | |
} | |
var prefix = word.substring(indexOfFirstVowel); | |
var stem = word.substring(0, indexOfFirstVowel); | |
var firstCharacter = word.charAt(0); | |
var lastCharacter = word.charAt(word.length() - 1); | |
if (!Character.isAlphabetic(firstCharacter)) { | |
resultBuilder.append(firstCharacter).append(prefix.replace(String.valueOf(firstCharacter), "")).append(stem.replace(String.valueOf(firstCharacter), "")).append("ay").append(" "); | |
return; | |
} | |
if (!Character.isAlphabetic(lastCharacter)) { | |
resultBuilder.append(prefix.replace(String.valueOf(lastCharacter), "")).append(stem.replace(String.valueOf(lastCharacter), "")).append("ay").append(lastCharacter).append(" "); | |
return; | |
} | |
resultBuilder.append(prefix).append(stem).append("ay").append(" "); | |
}); | |
return resultBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment