-
Notifications
You must be signed in to change notification settings - Fork 0
/
apexsandbox.cls
414 lines (370 loc) · 12.4 KB
/
apexsandbox.cls
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// https://www.apexsandbox.io/problem/18
// use above url, switch 18 with the problem no indicated with a #
public with sharing class apexsandbox {
public Boolean isTeenager(Integer age) {
// #18
Boolean res;
if(age>12 && age <=19 ) {
res = true;
} else {
res = false;
}
return res;
}
public Integer diff(Integer a, Integer b) {
// #4
Integer res;
res = Math.abs(a-b);
return res;
}
public Boolean sumEquals(Integer a, Integer b, Integer c) {
// #14
Boolean res;
if (a+b==c) {
res = true;
} else {
res = false;
}
return res;
}
public Boolean ascendingOrder(Integer a, Integer b, Integer c) {
// #20
if (a<=b && b<=c) {
return true;
} else {
return false;
}
}
public String aOrAn(String word) {
// #21
String res = '';
List<String> vowels = new List<String>{
'a',
'e',
'i',
'o',
'u'
};
List<String> letters = word.split('');
if (vowels.contains(letters.get(0))) {
res= 'an '+word;
}else {
res= 'a '+word;
}
return res;
}
public static Integer findLargest(Integer num1, Integer num2, Integer num3) {
// #3
List<Integer> numbers = new List<Integer>{
num1, num2, num3 };
numbers.sort();
return numbers[numbers.size()-1];
}
public Boolean isPassed(Boolean passedExam, Boolean passedAssignments, Boolean passedProject) {
// #19
List<Boolean> all = new List<Boolean>{
passedExam,
passedAssignments,
passedProject
};
List<Boolean> passed = new List<Boolean>();
for (Boolean passOrFail : all) {
if (passOrFail) {
passed.add(passOrFail);
}
}
if (passed.size()>=2) {
return true;
} else {
return false;
}
}
public Boolean isEndWithZero(Integer num){
// #90
if (num - Math.round(num/10)*10 == 0) {
return true;
} else {
return false;
}
}
public String whichTwo(Integer a, Integer b, Integer c) {
// #15
if (a+b==c) {
return 'c';
} else if (a+c==b) {
return 'b';
} else if (b+c==a) {
return 'a';
} else {
return '';
}
}
public String evenOrOdd(Integer num) {
// #5
if (Math.mod(num,2) == 0) {
return 'even';
} else {
return 'odd';
}
}
public Integer rockPaperScissors(String player1, String player2) {
// #12
// player 1 wins
Boolean cond1 = (player1 == 'rock' && player2 == 'scissors');
Boolean cond2 = (player1 == 'scissors' && player2 == 'paper');
Boolean cond3 = (player1 == 'paper' && player2 == 'rock');
Boolean player1Wins = (cond1 || cond2 || cond3);
if (player1 == player2) {
return 0;
} else if (player1Wins) {
return 1;
} else {
return 2;
}
}
public String ageGroup(Integer n) {
/*
#17 Given a person's age, return their age group as a string:
'Infant' for ages 0-1, 'Child' for ages 2 - 14, 'Youth' for
ages 15 - 21, and 'Adult' for ages 22+.
*/
if (n>=22) {
return 'Adult';
} else if (n>=15) {
return 'Youth';
} else if (n>=2) {
return 'Child';
} else {
return 'Infant';
}
}
public Boolean companionPlants(String plant1, String plant2) {
/*
#54 Some plants are considered companion plants. They grow
better when planted next to each other. For the purpose of
this problem, we consider the following plants to be
companions: lettuce and cucumbers, lettuce and onions,
onions and carrots, and onions and tomatoes.
*/
// lettuce and cucumbers
// lettuce and onions
// onions and carrots
// onions and tomatoes
String p = plant1+plant2; // p: plants
// c: condition
Boolean c1 = (p.contains('lettuce') && p.contains('cucumbers'));
Boolean c2 = (p.contains('lettuce') && p.contains('onions'));
Boolean c3 = (p.contains('onions') && p.contains('carrots'));
Boolean c4 = (p.contains('onions') && p.contains('tomatoes'));
if (c1 || c2 || c3 || c4) {
return true;
} else {
return false;
}
}
public Boolean isLeapYear(Integer year) {
/*
#6 A year is considered a leap year if it is evenly divisible by 4, with
the exception of years that are also evenly divisible by 100. Years evenly
divisible by 100 must also be evenly evenly divisible by 400 to by
considered leap years. Implement a method isLeapYear that takes as input an
Integer year and returns true if the year is a leap year, and false
otherwise.
*/
Boolean div4 = (Math.mod(year, 4) == 0);
Boolean div100 = (Math.mod(year, 100) == 0);
Boolean div400 = (Math.mod(year, 400) == 0);
if (div4) {
if (div100) {
if (div400) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
public Boolean isPrime(Integer num) {
/*
#7 Implement the function isPrime that takes as input an
integer greater than 1, returns true if the integer is a prime
number, and returns false otherwise. Assume that the input
will always be greater than 1.
*/
List<Integer> dividers = new List<Integer>();
for (Integer i = 2; i < num; i++) {
if ((Math.mod(num,i) == 0)) {
dividers.add(i);
}
}
if (num <2) {
return false;
} else if (num==2) {
return true;
} else if (dividers.size()>0) {
return false;
} else {
return true;
}
}
public Integer sumToN(Integer n) {
/*
#16 Implement the method sumToN that calculates and
returns the sum of all numbers (inclusive) from 1 to n.
Assume that n will be non-zero positive integer.
*/
Integer res = n*(n+1)/2;
return res;
}
public String formatName(String firstName, String lastName) {
// #9
return firstName + ' ' + lastName;
}
public String formatName(String firstName, String lastName) {
// #10
Boolean lastBlank = String.IsBlank(lastName);
Boolean firstBlank = String.IsBlank(firstName);
Boolean bothBlank = (lastBlank && firstBlank);
if (bothBlank) {
return '';
} else if ( lastBlank ) {
return firstName;
} else if (firstBlank ) {
return lastName;
} else {
return lastName + ', ' + firstName;
}
}
public String nameFromEmail(String email) {
/*
#11 Implement a function nameFromEmail that takes as input a
valid email address in the format
[email protected]. The function should extract
the first name and last name from
this email address and return a capitalized full name (i.e.
FirstName LastName). Assume that the input will always be
a valid email address with both the first name and last
name separated by a period (.).
*/
String firstName = email.substringBefore('.');
String lastName = email.subStringBetween('.','@');
String firstNameLower = firstName.toLowerCase();
String lastNameLower = lastName.toLowerCase();
return firstNameLower.capitalize()+' '+lastNameLower.capitalize();
}
public String changeTimeFormat(String strTime) {
/*
#79 13:50 and 01:50 PM are 24-hour and 12-hour
representations of the same time. Implement the method
changeTimeFormat that takes as input a string strTime
formatted as a 24-hour string, and returns the equivalent
12-hour string.
*/
strTime = strTime+' ';
String hourStr = strTime.subStringBefore(':');
String minStr = strTime.subStringBetween(':', ' ');
Integer hour = Integer.valueOf(hourStr);
Integer min = Integer.valueOf(minStr);
String res = '';
String amPm = '';
if (hour <12) {
amPm = ' AM';
} else {
hour = hour - 12;
amPm = ' PM';
}
if (hour<10 && hour>0) {
res = '0'+ hour.format();
}else if (hour == 0) {
hour= hour +12;
res = ''+hour.format();
} else {
res = ''+ hour.format();
}
if (min<10) {
res = res + ':' +'0'+ min.format() + amPm;
} else {
res = res+ ':' + min.format() + amPm;
}
return res;
}
public Integer fibonacci(Integer n) {
// #13 Fibonacci series
/*
The first two numbers in the fibonacci sequence are 1, and all other numbers in the sequence are defined as the sum of the last two fibonacci numbers. The first 10 numbers in the fibonacci sequence are 1, 1, 2, 3, 5, 8, 13, 21, 34, and 55. Implement the function fibonacci that takes as input an Integer n and returns the nth fibonacci number. Assume that n will always be greater than 0.
*/
if (n==1) {
return 1;
} else if (n==2) {
return 1;
} else {
Integer fib_n_2 = 1; // fib(n-2)
Integer fib_n_1 = 1; // fib(n-1)
Integer fib_n; // fib(n)
for (Integer i = 3; i <= n; i++) {
fib_n = fib_n_1+fib_n_2;
fib_n_2 = fib_n_1;
fib_n_1 = fib_n;
}
return fib_n;
}
}
public Integer nextPrime(Integer num) {
// #8
/*
A prime number is a number greater than 1 that is not evenly
divisible by any number greater than one and smaller than
itself. For example, 13 is a prime number because it is not
evenly divisible by any number from 2 to 12. Implement the
function nextPrime that takes as input an integer num and
returns the smallest prime number greater than num.
*/
Integer res=num+1;
while (!IsPrim3(res)) {
res++;
}
return res;
}
public Boolean isPrim3(Integer numb) {
List<Integer> dividers = new List<Integer>();
for (Integer i = 2; i < numb; i++) {
if ((Math.mod(numb,i) == 0)) {
dividers.add(i);
}
}
if (numb<2) {
return false;
} else if (numb==2) {
return true;
} else if (dividers.size()>0) {
return false;
} else {
return true;
}
}
public String reverseWordsInASentence(String sentence){
// #113
/*
Implement a function reverseWordsInASentence that will take
a String containing words separated by spaces as an
argument, and return a string with the order of the words
reversed.
*/
if (sentence == null) {
return null;
}else {
List<String> words = new List<String>();
words = sentence.split(' ');
List<String> res = new List<String>();
Integer index = words.size()-1;
for (Integer i = 0; i < words.size(); i++) {
res.add(words[index-i]);
}
return String.join(res,' ');
}
}
}