|
@@ -1,40 +1,84 @@
|
1
|
1
|
public class LoopFun
|
2
|
2
|
{
|
3
|
3
|
|
4
|
|
- /**
|
5
|
|
- * Given a number, return the factorial of that number.
|
6
|
|
- * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
|
7
|
|
- * @param number
|
8
|
|
- * @return the factorial of the number
|
9
|
|
- */
|
10
|
|
- public int factorial(int number){
|
11
|
|
- return -1;
|
12
|
|
- }
|
13
|
|
-
|
14
|
|
- /**
|
15
|
|
- * Given a phrase, get the acronym of that phrase. Acronym is the combination of
|
16
|
|
- * the first character of each word in upper case.
|
17
|
|
- * For example, given "Ruby on Rails", this method will return "ROR"
|
18
|
|
- * @param phrase
|
19
|
|
- * @return Upper case string of the first letter of each word
|
20
|
|
- */
|
21
|
|
- public String acronym(String phrase) {
|
22
|
|
- return null;
|
23
|
|
- }
|
24
|
|
-
|
25
|
|
- /**
|
26
|
|
- * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
|
27
|
|
- * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
|
28
|
|
- * at the end of the alphabet, it will wraps around.
|
29
|
|
- * For example:
|
30
|
|
- * 'a' => 'd'
|
31
|
|
- * 'w' => 'z'
|
32
|
|
- * 'x' => 'a'
|
33
|
|
- * 'y' => 'b'
|
34
|
|
- * @param word
|
35
|
|
- * @return the encrypted string by shifting each character by three character
|
36
|
|
- */
|
37
|
|
- public String encrypt(String word) {
|
38
|
|
- return null;
|
39
|
|
- }
|
|
4
|
+ /**
|
|
5
|
+ * Given a number, return the factorial of that number.
|
|
6
|
+ * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
|
|
7
|
+ * @param number
|
|
8
|
+ * @return the factorial of the number
|
|
9
|
+ */
|
|
10
|
+ public int factorial(int number){
|
|
11
|
+ int factorial = 1;
|
|
12
|
+
|
|
13
|
+ for (int i = 2; i <= number; i++) {
|
|
14
|
+ factorial *= i;
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ return factorial;
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ /**
|
|
21
|
+ * Given a phrase, get the acronym of that phrase. Acronym is the combination of
|
|
22
|
+ * the first character of each word in upper case.
|
|
23
|
+ * For example, given "Ruby on Rails", this method will return "ROR"
|
|
24
|
+ * @param phrase
|
|
25
|
+ * @return Upper case string of the first letter of each word
|
|
26
|
+ */
|
|
27
|
+ public String acronym(String phrase) {
|
|
28
|
+ StringBuilder sbAcronym = new StringBuilder();
|
|
29
|
+
|
|
30
|
+ String firstCapital = phrase.substring(0, 1).toUpperCase();
|
|
31
|
+ sbAcronym.append(firstCapital);
|
|
32
|
+
|
|
33
|
+ for (int i = 1; i < phrase.length(); i++) {
|
|
34
|
+ if (phrase.charAt(i) == ' ') {
|
|
35
|
+ sbAcronym.append(phrase.charAt(i + 1));
|
|
36
|
+ }
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ String Acronym = sbAcronym.toString().toUpperCase();
|
|
40
|
+
|
|
41
|
+ return Acronym;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
|
|
46
|
+ * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
|
|
47
|
+ * at the end of the alphabet, it will wraps around.
|
|
48
|
+ * For example:
|
|
49
|
+ * 'a' => 'd'
|
|
50
|
+ * 'w' => 'z'
|
|
51
|
+ * 'x' => 'a'
|
|
52
|
+ * 'y' => 'b'
|
|
53
|
+ * @param word
|
|
54
|
+ * @return the encrypted string by shifting each character by three character
|
|
55
|
+ */
|
|
56
|
+ public String encrypt(String word) {
|
|
57
|
+ // char[] toEncode = word.toCharArray();
|
|
58
|
+
|
|
59
|
+ StringBuilder sbEncryption = new StringBuilder();
|
|
60
|
+
|
|
61
|
+ char arr[] = {'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'};
|
|
62
|
+
|
|
63
|
+ int k;
|
|
64
|
+
|
|
65
|
+ for (int i = 0; i < 26; i++) {
|
|
66
|
+ for (int j = 0; j < word.length(); j++) {
|
|
67
|
+ if (word.charAt(j) == arr[i]){
|
|
68
|
+ k = i;
|
|
69
|
+
|
|
70
|
+ if (k < 23) {
|
|
71
|
+ sbEncryption.append(arr[(k + 3)]);
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ if (k >= 23) {
|
|
75
|
+ sbEncryption.append(arr[(k - 23)]);
|
|
76
|
+ }
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+ String encryption = sbEncryption.toString();
|
|
81
|
+
|
|
82
|
+ return encryption;
|
|
83
|
+ }
|
40
|
84
|
}
|