Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 035c532

Browse files
Kyu 8 | 'this' is a problem
1 parent 09fb1eb commit 035c532

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

‎src/main/java/kyu8/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Kata Found realized
4040

4141
- [Be Concise I - The Ternary Operator](beConciseITheTernaryOperator)
4242

43+
- [Be Concise IV - Index of an element in an array]()
44+
4345
- [Find Nearest square number](findNearestSquareNumber)
4446

4547

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Be Concise IV - Index of an element in an array
2+
3+
## Task
4+
5+
Provided is a function `Kata` which accepts two parameters in the following order: `array, element` and `return`s the *
6+
*index** of
7+
the element if found and `"Not found"` otherwise. Your task is to shorten the code as much as possible in order to meet
8+
the strict character count requirements of the Kata. (no more than 161) **You may assume that all array elements are
9+
unique.**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package kyu8.beConciseIndexOfElementInArray;
2+
3+
public class Solution {
4+
public static String kata(String[] a, String b) {
5+
int i = 0;
6+
for (String s : a) {
7+
if (s.equals(b)) return "" + i;
8+
i++;
9+
}
10+
return "Not found";
11+
}
12+
}

‎src/main/java/kyu8/thisIsAProblem/NameMe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public String getLastName() {
2020
}
2121

2222
public String getFullName() {
23-
return this.firstName + " " + this.lastName;
23+
return this.fullName;
2424
}
2525
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kyu8.beConciseIndexOfElementInArray;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class testSolution {
9+
@Test
10+
@DisplayName("should behave as expected")
11+
void testFind() {
12+
String[] array = {"2","3","5","7","11"};
13+
assertEquals(Solution.kata(array, "5"), "2");
14+
assertEquals(Solution.kata(array, "11"), "4");
15+
}
16+
17+
@Test
18+
@DisplayName("Not found")
19+
void testNoutFound() {
20+
String[] array = {"2","3","5","7","11"};
21+
assertEquals(Solution.kata(array, "843646"), "Not found");
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /