Skip to content

Commit 4aabfd5

Browse files
author
Sarah Akinkunmi
committed
some chapter 14 exercises and codewars solution
1 parent da1e507 commit 4aabfd5

19 files changed

+183
-1
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/chapter14/MorseCode.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package chapter14;
2+
3+
import java.util.Scanner;
4+
5+
public class MorseCode {
6+
private static final String[] morseCharacters = {
7+
".-", "-...", "-.-.", "-..", ".",
8+
"..-.", "--.", "....", "..", ".---",
9+
"-.-", ".-..", "--", "-.", "---", ".--.",
10+
"--.-", ".-.", "...", "-", "..-", "...-",
11+
".--", "-..-", "-.--", "--..",
12+
".----", "..---", "...--", "....-", ".....",
13+
"-....", "--...", "---..", "----.", "-----",
14+
" "
15+
};
16+
17+
private static final char[] normalCharacters = {
18+
'a', 'b', 'c', 'd', 'e', 'f', 'g',
19+
'h', 'i', 'j', 'k', 'l', 'm', 'n',
20+
'o', 'p', 'q', 'r', 's', 't', 'u',
21+
'v', 'w', 'x', 'y', 'z',
22+
'1', '2', '3', '4', '5',
23+
'6', '7', '8', '9', '0', ' '
24+
};
25+
26+
public static void main(String[] args) {
27+
Scanner scanner = new Scanner(System.in);
28+
System.out.println("Enter text here");
29+
String text = scanner.nextLine().toLowerCase();
30+
// System.out.println(toMorseCode(text));
31+
System.out.println(toEnglishAlphabet(text));
32+
}
33+
34+
private static String toMorseCode(String text) {
35+
char[] characters = text.toLowerCase().toCharArray();
36+
StringBuilder sb = new StringBuilder();
37+
38+
for(char c : characters){
39+
for (int i = 0; i < normalCharacters.length; i++) {
40+
if (c == normalCharacters[i]){
41+
sb.append(morseCharacters[i]).append(" ");
42+
break;
43+
}
44+
}
45+
}
46+
sb.deleteCharAt(sb.length() -1);
47+
return sb.toString();
48+
}
49+
50+
private static String toEnglishAlphabet(String text) {
51+
String[] characters = text.split("\\s{1,2}");
52+
StringBuilder sb = new StringBuilder();
53+
54+
for(String c : characters){
55+
for (int i = 0; i < morseCharacters.length; i++) {
56+
if (c.equals(morseCharacters[i])) {
57+
sb.append(normalCharacters[i]);
58+
break;
59+
}
60+
}
61+
}
62+
return sb.toString();
63+
}
64+
}
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1-
package chapter14;public class WordEquivalentOfCheckAmount {
1+
package chapter14;
2+
3+
import java.util.Scanner;
4+
5+
public class WordEquivalentOfCheckAmount {
6+
private static String[] hundredsArray = {
7+
"ONE", "TWO", "THREE",
8+
"FOUR", "FIVE", "SIX",
9+
"SEVEN", "EIGHT", "NINE"
10+
};
11+
12+
private static String[] tensArray = {
13+
"TWENTY", "THIRTY", "FOURTY",
14+
"FIFTY", "SIXTY", "SEVENTY",
15+
"EIGHTY", "NINETY"
16+
};
17+
18+
private static String[] unitsArray = {
19+
"ONE", "TWO", "THREE", "FOUR", "FIVE",
20+
"SIX", "SEVEN", "EIGHT", "NINE", "TEN",
21+
"ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
22+
"FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
23+
"NINETEEN"
24+
};
25+
26+
public static void main(String[] args) {
27+
Scanner scanner = new Scanner(System.in);
28+
System.out.println("Enter an amount less than $1000");
29+
double checkAmount = scanner.nextDouble();
30+
31+
if(checkAmount < 1000) {
32+
String amount = String.valueOf(checkAmount);
33+
System.out.println(amountToWords(amount));
34+
}
35+
}
36+
37+
private static String amountToWords(String amount) {
38+
char[] numberAmount = amount.toCharArray();
39+
StringBuilder sb = new StringBuilder();
40+
// for (int i = 0; i < numberAmount.length; i++) {
41+
sb.append(hundredsArray[numberAmount[0] - 1]).append("hundred");
42+
// }
43+
return sb.toString();
44+
}
245
}

src/codewars/PrinterErrors.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package codewars;
2+
3+
/*
4+
In a factory a printer prints labels for boxes. For one kind of boxes the printer has to use colors which,
5+
for the sake of simplicity, are named with letters from a to m. The colors used by the printer are recorded in
6+
a control string. For example a "good" control string would be aaabbbbhaijjjm meaning that the printer used
7+
three times color a, four times color b, one time color h then one time color a...Sometimes there are problems:
8+
lack of colors, technical malfunction and a "bad" control string is produced e.g. aaaxbbbbyyhwawiwjjjwwm with
9+
letters not from a to m. You have to write a function printer_error which given a string will return the
10+
error rate of the printer as a string representing a rational whose numerator is the number of errors and the
11+
denominator the length of the control string. Don't reduce this fraction to a simpler expression. The string
12+
has a length greater or equal to one and contains only letters from a to z.
13+
14+
Examples:
15+
s="aaabbbbhaijjjm"
16+
printer_error(s) => "0/14"
17+
18+
s="aaaxbbbbyyhwawiwjjjwwm"
19+
printer_error(s) => "8/22"
20+
*/
21+
public class PrinterErrors {
22+
public static String printerError(String s) {
23+
// your code
24+
String[] lettersInString = s.split("");
25+
int numbersOfErrorCharacters = 0;
26+
for(String c : lettersInString) {
27+
if (!(c.matches("[a-m]"))) {
28+
numbersOfErrorCharacters++;
29+
}
30+
}
31+
32+
return numbersOfErrorCharacters + "/" + s.length();
33+
}
34+
}

src/codewars/VowelCount.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package codewars;
2+
3+
public class VowelCount {
4+
public static int getCount(String str) {
5+
int vowelsCount = 0;
6+
// your code here
7+
char[] charArray = str.toCharArray();
8+
for(char c : charArray) {
9+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
10+
vowelsCount++;
11+
}
12+
}
13+
return vowelsCount;
14+
}
15+
}

test/codewars/PrinterErrorsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package codewars;
2+
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class PrinterErrorsTest {
9+
@Test
10+
public void test() {
11+
System.out.println("printerError Fixed Tests");
12+
String s="aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz";
13+
assertEquals("3/56", PrinterErrors.printerError(s));
14+
}
15+
}

test/codewars/VowelCountTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package codewars;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
6+
class VowelCountTest {
7+
@Test
8+
void testCase1() {
9+
assertEquals("Nope!", 5, VowelCount.getCount("abracadabra"));
10+
}
11+
}

0 commit comments

Comments
 (0)