-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProblem.java
More file actions
177 lines (147 loc) · 5.94 KB
/
Problem.java
File metadata and controls
177 lines (147 loc) · 5.94 KB
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
package com.codedifferently.coding.level.beginner.set01;
public class Problem {
/* Problem 1
The parameter dcHero is true if it is an DC Hero,
and the parameter avengerHero is true if the hero is an Avenger.
Avengers will assemble if it is not a DC Hero, or they're an Avenger, or there is at least an Avenger on the team.
Return true if the avengers are ready to assemble.
Example:
avengersAssemble(false, false) --> true
avengersAssemble(false, true) --> true
avengersAssemble(true, false) --> false
avengersAssemble(true, true) --> true
*/
public static Boolean avengersAssemble(boolean dcHero, boolean avengerHero) {
if (dcHero == false && avengerHero == false){
return true;
}
else if(dcHero == false && avengerHero){
return true;
}
else if (dcHero && avengerHero == false){
return false;
}
else {
return true;
}
}
/* Problem 3
You and your friends are out walking on the boardwalk at Atlantic City
and decided to go to Casino for the fun of it. One person won
___ amount of money. You're trying to find out if the amount was between 90-100 or 190-200.
Return true if the amount of money is within 10 of 100 or 200.
Tip: Math.abs(num) computes the absolute value of a number.
nearValue(93) --> true
nearValue(90) --> true
nearValue(89) --> false
*/
public static Boolean nearValue(int n) {
if(( Math.abs(n) >=90 && Math.abs(n)<=100) || (Math.abs(n) >= 190 && Math.abs(n)<=200)){
return true;
}
return false;
}
/* Problem 4
The instructor provided you with a non-empty string and a number n.
The instructor wants you to remove the letter at index n and provide a new string.
Tip: Value of n should be a valid index of a given letter in the original string,
such as 0 or str.length()-1 inclusive
missingLetter("kitten", 1) --> "ktten"
missingLetter("kitten", 0) --> "itten"
missingLetter("kitten", 4) --> "kittn"
*/
public static String missingLetter(String letter, int n) {
String firstHalf = letter.substring(0,n);
String secondHalf = letter.substring(n+1, letter.length());
return firstHalf+secondHalf;
}
/* Problem 5
Given the word of the day, take the last letter and
return a new word of the day when you add the last letter to the front and back of the word.
For example, "founder" yields "rfounderr".
Tip: The word of the day will be a length 1 or more
wordOfDay("cat") --> "tcatt"
wordOfDay("Hello" --> "oHelloo"
wordOfDay("a") --> "aaa"
*/
public static String wordOfDay(String word) {
char lastLetter = word.charAt(word.length()-1);
return lastLetter + word + lastLetter;
}
/* Problem 6
Johnnys favorite pharse to say is hi, he wants to know if the pharses given start with "hi".
Provide Johnny with a string, and return true if the string starts with "hi" and false otherwise.
beginWithHi("hi there") --> true
beginWithHi("hi") --> true
beginWithHi("hello hi") --> false
*/
public static Boolean beginWithHi(String phrase) {
if (phrase.substring(0,2).equals("hi")){
return true;
}
return false;
}
/* Problem 7
If you think of the basic numbers 0-20, you will notice that 13-19 contain the word "teen" in them.
The math instructor provides you with 3 numbers, you need to decided if 1 or more of them contain teen.
Return true if 1 or more of them contain teen.
containTeen(13, 20, 10) --> true
containTeen(20, 19, 10) --> true
containTeen(20, 10, 13) --> true
*/
public static Boolean containTeen(int one, int two, int three){
return(!(one >= -12 && one <=12) || !(two >= -12 && two <=12) || !(three >= -12 && three <=12));
}
/* Problem 8
Your local rapper is looking to create his next hook for his next track. He wants to add phrases that begin with "mix".
But he decides he wants to take it a step further and accept any phrase except the "m" can be any letter or number.
He needs your help to make sure the list of given phrases begin with what was asked.
Return true if the given phrase begins with "mix", or any beginning letter or number following "ix"
startWithIx("mix snacks") --> true
startWithIx("pix snacks") --> true
startWithIx("piz snacks") --> false
*/
public static Boolean startWithIx(String phrase) {
return (phrase.substring(0,3) .equals("mix") || phrase.substring(1,3).equals("ix"));
}
/* Problem 9
Provide two numbers, evaluate both numbers to see which one is nearest to the value 10.
Some numbers may have the same range in how near they are to 10; such as 13 and 7 both are 3 from 10;
In that case, we would consider that event a tie.
Tip: Math.abs(n) returns the absolute value of a number
Return whichever number is nearest to 10, or return 0 for the event of a tie.
near10(8, 13) --> 8
near10(13, 8) --> 8
near10(13, 7) --> 0
*/
public static Integer near10(int one, int two) {
if (Math.abs(10-one) < Math.abs(10 - two)) {
return one;
}
else if (Math.abs(10-one) > Math.abs(10-two)) {
return two;
}
else {
return 0;
}
}
/* Problem 10
Determine if the given string contains between 1 and 3 'e' characters.
Only if the string contains between 1 and 3 'e' characters; return true.
containE("Hello") --> true
containE("Heelle") --> true
containE("Heelele") --> false
*/
public static Boolean containE(String str) {
int eCounter = 0;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == 'e') {
eCounter++;
}
}
if(1 <= eCounter && eCounter <= 3){
return true;
}
return false;
}
}