Created
August 31, 2017 06:13
-
-
Save Max-AR/68dca5c8972e82865f697ccabb5820b0 to your computer and use it in GitHub Desktop.
Quick dirty and recursive implementation of contains string function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ContainsApp extends App { | |
def contains(substr: String, string: String): Boolean ={ | |
var bool = false | |
def impl(letters: List[Char], word: List[Char], firstPass: Boolean = true): List[Char] ={ | |
if(letters.isEmpty || word.isEmpty){ | |
List() | |
} else if(firstPass && (letters.head != word.head)) { | |
//we want to force until we find a match | |
impl(letters, word.tail) | |
} else { | |
if(letters.head == word.head){ | |
if(letters.length == 1) | |
word | |
else | |
impl(letters.tail, word.tail, firstPass = false) | |
} else { | |
List() | |
} | |
} | |
} | |
val arr1 = substr.toList | |
val arr2 = string.toList | |
if (substr.length == 0) { | |
false | |
} else { | |
if(impl(arr1, arr2).isEmpty) | |
false | |
else | |
true | |
} | |
} | |
println(contains("ll", "Hello world").toString) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment