-
Notifications
You must be signed in to change notification settings - Fork 3
/
FunctionalOne.java
165 lines (125 loc) · 4.32 KB
/
FunctionalOne.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
*
*/
package coding.bat.solutions;
import java.util.List;
/**
* @author Aman Shekhar
*
*/
public class FunctionalOne {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
// --------------------------------------------------------------------------------------------
// Given a list of integers, return a list where each integer is multiplied by
// 2.
//
//
// doubling([1, 2, 3]) → [2, 4, 6]
// doubling([6, 8, 6, 8, -1]) → [12, 16, 12, 16, -2]
// doubling([]) → []
public List<Integer> doubling(List<Integer> nums) {
nums.replaceAll(n -> n * 2);
return nums;
}
// --------------------------------------------------------------------------------------------
// Given a list of integers, return a list where each integer is multiplied with
// itself.
//
//
// square([1, 2, 3]) → [1, 4, 9]
// square([6, 8, -6, -8, 1]) → [36, 64, 36, 64, 1]
// square([]) → []
public List<Integer> square(List<Integer> nums) {
nums.replaceAll(n -> n * n);
return nums;
}
// --------------------------------------------------------------------------------------------
// Given a list of strings, return a list where each string has "*" added at its
// end.
//
//
// addStar(["a", "bb", "ccc"]) → ["a*", "bb*", "ccc*"]
// addStar(["hello", "there"]) → ["hello*", "there*"]
// addStar(["*"]) → ["**"]
public List<String> addStar(List<String> strings) {
strings.replaceAll(n -> n + "*");
return strings;
}
// --------------------------------------------------------------------------------------------
// Given a list of strings, return a list where each string is replaced by 3
// copies of the string concatenated together.
//
//
// copies3(["a", "bb", "ccc"]) → ["aaa", "bbbbbb", "ccccccccc"]
// copies3(["24", "a", ""]) → ["242424", "aaa", ""]
// copies3(["hello", "there"]) → ["hellohellohello", "theretherethere"]
public List<String> copies3(List<String> strings) {
strings.replaceAll(n -> (n + n + n));
return strings;
}
// --------------------------------------------------------------------------------------------
// Given a list of strings, return a list where each string has "y" added at its
// start and end.
//
//
// moreY(["a", "b", "c"]) → ["yay", "yby", "ycy"]
// moreY(["hello", "there"]) → ["yhelloy", "ytherey"]
// moreY(["yay"]) → ["yyayy"]
public List<String> moreY(List<String> strings) {
strings.replaceAll(n -> ("y" + n + "y"));
return strings;
}
// --------------------------------------------------------------------------------------------
// Given a list of integers, return a list where each integer is added to 1 and
// the result is multiplied by 10.
//
//
// math1([1, 2, 3]) → [20, 30, 40]
// math1([6, 8, 6, 8, 1]) → [70, 90, 70, 90, 20]
// math1([10]) → [110]
public List<Integer> math1(List<Integer> nums) {
nums.replaceAll(n -> ((n + 1) * 10));
return nums;
}
// --------------------------------------------------------------------------------------------
// Given a list of non-negative integers, return an integer list of the
// rightmost digits. (Note: use %)
//
//
// rightDigit([1, 22, 93]) → [1, 2, 3]
// rightDigit([16, 8, 886, 8, 1]) → [6, 8, 6, 8, 1]
// rightDigit([10, 0]) → [0, 0]
public List<Integer> rightDigit(List<Integer> nums) {
nums.replaceAll(n -> (n % 10));
return nums;
}
// --------------------------------------------------------------------------------------------
// Given a list of strings, return a list where each string is converted to
// lower case (Note: String toLowerCase() method).
//
//
// lower(["Hello", "Hi"]) → ["hello", "hi"]
// lower(["AAA", "BBB", "ccc"]) → ["aaa", "bbb", "ccc"]
// lower(["KitteN", "ChocolaTE"]) → ["kitten", "chocolate"]
public List<String> lower(List<String> strings) {
strings.replaceAll(n -> n.toLowerCase());
return strings;
}
// --------------------------------------------------------------------------------------------
// Given a list of strings, return a list where each string has all its "x"
// removed.
//
//
// noX(["ax", "bb", "cx"]) → ["a", "bb", "c"]
// noX(["xxax", "xbxbx", "xxcx"]) → ["a", "bb", "c"]
// noX(["x"]) → [""]
public List<String> noX(List<String> strings) {
strings.replaceAll(n -> n.replace("x", ""));
return strings;
}
}