|
@@ -1,40 +1,75 @@
|
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
|
+
|
|
12
|
+ int result = 1;
|
|
13
|
+ for(int i = 2; i <= number; i++)
|
|
14
|
+ result *= i;
|
|
15
|
+ return result;
|
|
16
|
+
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ /**
|
|
20
|
+ * Given a phrase, get the acronym of that phrase. Acronym is the combination of
|
|
21
|
+ * the first character of each word in upper case.
|
|
22
|
+ * For example, given "Ruby on Rails", this method will return "ROR"
|
|
23
|
+ * @param phrase
|
|
24
|
+ * @return Upper case string of the first letter of each word
|
|
25
|
+ */
|
|
26
|
+ public String acronym(String phrase) {
|
|
27
|
+
|
|
28
|
+ String acronym = "";
|
|
29
|
+ acronym += phrase.toUpperCase().charAt(0);
|
|
30
|
+
|
|
31
|
+ for (int i = 1; i <= phrase.length() - 1; i++) {
|
|
32
|
+ if (phrase.charAt(i - 1) == ' ' || phrase.charAt(i - 1) == '-') {
|
|
33
|
+ acronym += phrase.toUpperCase().charAt(i);
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ return acronym;
|
|
38
|
+ }
|
|
39
|
+ /*
|
|
40
|
+
|
|
41
|
+ What can you learn from this solution?
|
|
42
|
+
|
|
43
|
+ A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
|
|
44
|
+
|
|
45
|
+ Here are some questions to help you reflect on this solution and learn the most from it.
|
|
46
|
+
|
|
47
|
+ What compromises have been made?
|
|
48
|
+ Are there new concepts here that I could read more about to develop my understanding?
|
|
49
|
+ */
|
|
50
|
+
|
|
51
|
+ /**
|
|
52
|
+ * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
|
|
53
|
+ * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
|
|
54
|
+ * at the end of the alphabet, it will wraps around.
|
|
55
|
+ * For example:
|
|
56
|
+ * 'a' => 'd'
|
|
57
|
+ * 'w' => 'z'
|
|
58
|
+ * 'x' => 'a'
|
|
59
|
+ * 'y' => 'b'
|
|
60
|
+ * @param word
|
|
61
|
+ * @return the encrypted string by shifting each character by three character
|
|
62
|
+ */
|
|
63
|
+ public String encrypt(String word) {
|
|
64
|
+ char swap = ' ';
|
|
65
|
+ char[] arr = word.toCharArray();
|
|
66
|
+ swap(arr, 0, word.length + 3);
|
|
67
|
+ int i = arr.length + 3 ;
|
|
68
|
+ while(i > 1) {
|
|
69
|
+ swap(arr, i, i-1);
|
|
70
|
+ }
|
|
71
|
+ return new swap;
|
|
72
|
+ }
|
|
73
|
+
|
40
|
74
|
}
|
|
75
|
+
|