Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cb3a06

Browse files
authored
Find Number in Array (#9)
* Solution to findNumber in array and getNumbers in range challenges
1 parent 21c1354 commit 1cb3a06

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ Write a program that prints the numbers from 1 to 100. But for multiples
110110
of three print “Fizz” instead of the number and for the multiples of five print
111111
“Buzz”. For numbers which are multiples of both three and five print “FizzBuzz
112112

113+
#### Find number in Array
114+
115+
This is a basic problem where we need to find if a number is present on the
116+
given array.
117+
113118
## Useful commands
114119

115120
### Enable scala style check on compile
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.codingmaniacs.hackerrank
2+
3+
import scala.annotation.tailrec
4+
5+
object CollectionChallenges {
6+
7+
def findNumber(arr: Array[Int], k: Int): String = {
8+
9+
@tailrec
10+
def findK(ls: List[Int], n: Int) : String = {
11+
ls match {
12+
case Nil => "NO"
13+
case h :: _ if h == n => "YES"
14+
case _ :: tail => findK(tail, n)
15+
}
16+
}
17+
18+
findK(arr.toList, k)
19+
}
20+
21+
def findNumbers(start: Int, end: Int, predicate: Int => Boolean) : Array[Int] = {
22+
def findNumbersRec(res: List[Int], rem: List[Int], predicate: Int => Boolean) : Array[Int] = {
23+
rem match {
24+
case Nil => res.toArray
25+
case h :: tail if predicate(h) => findNumbersRec(res ::: List(h), tail, predicate)
26+
case _ :: tail => findNumbersRec(res, tail, predicate)
27+
}
28+
}
29+
30+
findNumbersRec(List(), (start to end).toList, predicate)
31+
}
32+
33+
def oddNumbers(start: Int, end: Int): Array[Int] = {
34+
def oddPredicate(h: Int) : Boolean = {
35+
h % 2 != 0
36+
}
37+
findNumbers(start, end, oddPredicate)
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.codingmaniacs.hackerrank
2+
3+
import org.specs2.mutable.Specification
4+
5+
class CollectionChallengesSpec extends Specification {
6+
"The number finder" should {
7+
"say yes if the a number exist on an array" in {
8+
// scalastyle:off magic.number
9+
val s = Array(1, 2, 3, 4, 5)
10+
// scalastyle:on magic.number
11+
val element = 1
12+
val expected = "YES"
13+
val result = CollectionChallenges.findNumber(s, element)
14+
result mustEqual expected
15+
}
16+
17+
"say no if the a number doesn't exist on an array" in {
18+
// scalastyle:off magic.number
19+
val s = Array(1, 2, 3, 4, 5)
20+
// scalastyle:on magic.number
21+
val element = 6
22+
val expected = "NO"
23+
val result = CollectionChallenges.findNumber(s, element)
24+
result mustEqual expected
25+
}
26+
}
27+
28+
"The odd / even generator" should {
29+
"find all the odd numbers between a range" in {
30+
val start = 2
31+
val end = 5
32+
// scalastyle:off magic.number
33+
val expected = Array(3, 5)
34+
// scalastyle:on magic.number
35+
val oddNumbers = CollectionChallenges.oddNumbers(start, end)
36+
oddNumbers mustEqual expected
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)