Skip to content

Instantly share code, notes, and snippets.

View jurisk's full-sized avatar

jurisk

View GitHub Profile
@jurisk
jurisk / luhn.hs
Last active February 27, 2020 07:52
doubleOddElements :: [Int] -> [Int]
doubleOddElements [] = []
doubleOddElements (x: []) = [x]
doubleOddElements (x0 : x1 : xs) = (x0 : x1 * 2 : doubleOddElements xs)
digits :: Integral x => x -> [x]
digits 0 = []
digits x = digits (x `div` 10) ++ [x `mod` 10]
isLuhn :: Int -> Bool
trait Cartesian[T] {
def cartesianProduct(input: List[Set[T]]): Set[List[T]]
}
/** https://www.hackerrank.com/challenges/filter-elements */
object Solution extends App {
def filterElements(list: List[Int], times: Int, processed: Set[Int] = Set.empty): List[Int] = {
if (list.isEmpty) Nil
else {
val elem = list.head
val x = if (processed.contains(elem)) {
Nil // already seen
} else {
if (list.count(_ == elem) >= times) List(elem) // right count