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
module BeginningHaskellTalkExamples where | |
isPalindromeWithRecursion :: String -> Bool | |
isPalindromeWithRecursion str = | |
loop strEndIndex where | |
strEndIndex = length str - 1 | |
loop i = | |
if (i <= (div strEndIndex 2)) then True | |
else if (str !! i) /= str !! (strEndIndex - i) then False |
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
module PalindromeIndex | |
//Hackertank problem | |
//Palindrome Index | |
// Given a string, , of lowercase letters, determine the index of the character whose removal will make a | |
// palindrome. If is already a palindrome or no such character exists, then print . There will always be a | |
// valid solution, and any correct answer is acceptable. For example, if "bcbc", we can either remove 'b' | |
// at index or 'c' at index . | |
// Input Format | |
// The first line contains an integer, , denoting the number of test cases. | |
// Each line of the subsequent lines (where ) describes a test case in the form of a single string, |
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
module Anagram | |
//Hackerrank problem | |
//Anagram | |
// Sid is obsessed with reading short stories. Being a CS student, he is doing some interesting frequency | |
// analysis with the books. He chooses strings and in such a way that . | |
// Your task is to help him find the minimum number of characters of the first string he needs to change to | |
// enable him to make it an anagram of the second string. | |
// Note: A word x is an anagram of another word y if we can produce y by rearranging the letters of x. | |
// Input Format |
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
val readOnlyList: List<Int> = listOf(1, 2, 3, 4) |
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
val mutableList: MutableList<Int> = mutableListOf(4, 5, 6) |
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
fun main(args: Array<String>) { | |
val mutableList: MutableList<Int> = mutableListOf(4, 5, 6) | |
val listNotToBeAmended: List<Int> = mutableList | |
println("listNotToBeAmended before add: $listNotToBeAmended") | |
mutableList.add(3) | |
println("listNotToBeAmended after add to underlying list: $listNotToBeAmended") | |
} |
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
fun dropWhile(list: List<Int>, f: (Int) -> Boolean) |
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
sealed class List<out E> | |
object Empty: List<Nothing>() { | |
override fun toString()= "[]" | |
} | |
data class Cons<E>(val head: E, val tail: List<E>): List<E>() { | |
override fun toString(): String = "$head :: $tail" | |
} | |
fun main() { |
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
fun <E> dropWhile(list: List<E>, f: (E) -> Boolean): List<E> { | |
tailrec fun loop(remaining: List<E>): List<E> = | |
when(remaining) { | |
is Empty -> remaining | |
is Cons -> if (f(remaining.head)) loop(remaining.tail) else remaining | |
} | |
return loop(list) | |
} |
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
tailrec fun <E> dropWhile(list: List<E>, f: (E) -> Boolean): List<E> = | |
when(list) { | |
Empty -> Empty | |
is Cons -> if (f(list.head)) dropWhile(list.tail, f) else list | |
} |
OlderNewer