[2015-10-26] Challenge #238 [Easy] Consonants and Vowels : dailyprogrammer
You were hired to create words for a new language. However, your boss wants these words to follow a strict pattern of consonants and vowels. You are bad at creating words by yourself, so you decide it would be best to randomly generate them.
Your task is to create a program that generates a random word given a pattern of consonants (c) and vowels (v).
String consonants = "bcdfghjklmnpqrstvwxyz";
String vowels = "aeiou";
for (char c : args[0].toCharArray()) {
char out;
switch (c) {
case 'C': out = (char)(randomSelect(consonants) - 32); break;
case 'c': out = randomSelect(consonants); break;
case 'V': out = (char)(randomSelect(vowels) - 32); break;
case 'v': out = randomSelect(vowels); break;
default: throw new RuntimeException("Input must consist of "
+ "vVcC only.");
}
System.out.print(out);
}
}
// Returns a random character in a given String.
public static char randomSelect(String s) {
return s.charAt(rng.nextInt(s.length()));
}
Scala
def rand_choice(l:List[Char]): Char = {
val rnd = new java.util.Random()
l(rnd.nextInt(l.length))
}
val vowels = "aeiouy".toList
val consanants = "bcdfghjklmnpqrstvwxz".toList
def solve(s:String): String = {
def repl(c:Char): Char = {
c match {
case 'c' => rand_choice(consanants)
case 'v' => rand_choice(vowels)
case 'C' => rand_choice(consanants).toString.toUpperCase.charAt(0)
case 'V' => rand_choice(vowels).toString.toUpperCase.charAt(0)
}
}
s.toList.map(repl).mkString
}
Read full article from [2015-10-26] Challenge #238 [Easy] Consonants and Vowels : dailyprogrammer
You were hired to create words for a new language. However, your boss wants these words to follow a strict pattern of consonants and vowels. You are bad at creating words by yourself, so you decide it would be best to randomly generate them.
Your task is to create a program that generates a random word given a pattern of consonants (c) and vowels (v).
Input Description
Any string of the letters c and v, uppercase or lowercase.Output Description
A random lowercase string of letters in which consonants (bcdfghjklmnpqrstvwxyz) occupy the given 'c' indices and vowels (aeiou) occupy the given 'v' indices.Sample Inputs
cvcvcc CcvV cvcvcvcvcvcvcvcvcvcv
Sample Outputs
litunn ytie poxuyusovevivikutire
public static void main(String[] args) {String consonants = "bcdfghjklmnpqrstvwxyz";
String vowels = "aeiou";
for (char c : args[0].toCharArray()) {
char out;
switch (c) {
case 'C': out = (char)(randomSelect(consonants) - 32); break;
case 'c': out = randomSelect(consonants); break;
case 'V': out = (char)(randomSelect(vowels) - 32); break;
case 'v': out = randomSelect(vowels); break;
default: throw new RuntimeException("Input must consist of "
+ "vVcC only.");
}
System.out.print(out);
}
}
// Returns a random character in a given String.
public static char randomSelect(String s) {
return s.charAt(rng.nextInt(s.length()));
}
Scala
def rand_choice(l:List[Char]): Char = {
val rnd = new java.util.Random()
l(rnd.nextInt(l.length))
}
val vowels = "aeiouy".toList
val consanants = "bcdfghjklmnpqrstvwxz".toList
def solve(s:String): String = {
def repl(c:Char): Char = {
c match {
case 'c' => rand_choice(consanants)
case 'v' => rand_choice(vowels)
case 'C' => rand_choice(consanants).toString.toUpperCase.charAt(0)
case 'V' => rand_choice(vowels).toString.toUpperCase.charAt(0)
}
}
s.toList.map(repl).mkString
}
Read full article from [2015-10-26] Challenge #238 [Easy] Consonants and Vowels : dailyprogrammer