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

Skip to content

Commit 9651af6

Browse files
committed
Add some docs, tests and cleanup the code a bit
1 parent 1ab80e6 commit 9651af6

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ of three print “Fizz” instead of the number and for the multiples of five pr
128128
This is a basic problem where we need to find if a number is present on the
129129
given array.
130130

131+
#### Basic word count problem but without using REGEX expressions.
132+
133+
Write a function which returns the number of words in a given string - without using REGEX expressions
134+
135+
```
136+
given "disco1 hero2" or
137+
" disco1 hero2" or
138+
"disco1 hero2 " or
139+
" disco1 hero2 "
140+
then the word count should be 2, the solution should ignore the spaces.
141+
```
142+
143+
#### Find the last index of a slice on a given array
144+
145+
Write a function which prints the index of the last occurrence of the entire sequence within a given array
146+
WITHOUT using inbuilt **sliding**, **lastIndexOf** and **lastIndexOfSlice** functions.
147+
131148
## Useful commands
132149

133150
### Enable scala style check on compile

src/main/scala/com/codingmaniacs/others/CollectionChallenges.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object CollectionChallenges {
99
(a, s) match {
1010
case (List(), List()) => List()
1111
case (ls, List()) => ls
12-
case (ina :+ enda, ins :+ ends) if enda == ends => lastIndexSeen(ina, ins)
12+
case (ina :+ endA, ins :+ endS) if endA == endS => lastIndexSeen(ina, ins)
1313
case (ina :+ _, _) => lastIndexSeen(ina, s)
1414
}
1515
}

src/main/scala/com/codingmaniacs/others/StringChallenges.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ object StringChallenges {
1414
* @return Reverse representation of the string
1515
*/
1616
def flip(str: String): String = {
17-
str.foldRight("") ((el, acc) => acc + el)
17+
str.foldRight("")((el, acc) => acc + el)
1818
}
1919

20+
/**
21+
* Given a string it returns the number of words contained in the string
22+
*
23+
* @param str String
24+
* @return Number of words found on the string using split method
25+
*/
2026
def wordCountWithSplit(str: String): Int = {
2127
str
2228
.trim
@@ -25,23 +31,28 @@ object StringChallenges {
2531
.count(w => !w.equals(""))
2632
}
2733

34+
/**
35+
* Given a string it returns the number of words contained in the string
36+
*
37+
* @param str String
38+
* @return Number of words found on the string using recursion
39+
*/
2840
def wordCountWithRec(str: String): Int = {
2941

3042
@tailrec
3143
def wordCountRec(charSeq: List[Char], spaces: Int): Int = {
3244
charSeq match {
3345
case Nil => spaces + 1
34-
case h :: th :: tail if h == 32 && th == 32 => {
46+
case h :: th :: tail if h == 32 && th == 32 =>
3547
wordCountRec(th :: tail, spaces)
36-
}
37-
case h :: th :: tail if h == 32 && th != 32 => {
48+
case h :: th :: tail if h == 32 && th != 32 =>
3849
wordCountRec(tail, spaces + 1)
39-
}
4050
case _ :: tail => {
4151
wordCountRec(tail, spaces)
4252
}
4353
}
4454
}
55+
4556
str match {
4657
case "" => 0
4758
case _ =>

src/test/scala/com/codingmaniacs/others/CollectionChallengesSpec.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,15 @@ class CollectionChallengesSpec extends Specification {
4444
val reverse = CollectionChallenges.lastIndexOfSlice(s, slice)
4545
reverse mustEqual expected
4646
}
47+
48+
"find the last index of a big slice on an array" in {
49+
// scalastyle:off magic.number
50+
val s = Array(7,2,3,4,6,9,3,4,6,21,8)
51+
val slice = Array(3,4,6,9)
52+
// scalastyle:on magic.number
53+
val expected = 2
54+
val reverse = CollectionChallenges.lastIndexOfSlice(s, slice)
55+
reverse mustEqual expected
56+
}
4757
}
4858
}

0 commit comments

Comments
 (0)