Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Created May 27, 2025 23:46
Show Gist options
  • Save kracekumar/61d21b08c977d8db69853175b6575c4d to your computer and use it in GitHub Desktop.
Save kracekumar/61d21b08c977d8db69853175b6575c4d to your computer and use it in GitHub Desktop.
Given two arrays, return all pairs (where each number is in each array) whose sum is an odd number.
import gleam/list
import gleam/result
pub fn oddsum(arr1, arr2) {
list.map(arr1, fn(a) {
list.map(arr2, fn(b) { [a, b]})}) |> list.flatten |> list.map(fn(x){
case {{result.unwrap(list.first(x), 0) + result.unwrap(list.last(x), 0)} % 2} {
1 -> x
_ -> []
}
}) |> list.filter(fn(y){list.length(y) > 1})
}
pub fn main() {
oddsum([9, 14, 6, 2, 11], [8, 4, 7, 20])|> echo
oddsum([10, 12, 14, 18, 20], [2, 4, 6, 8])|> echo
}
src/main.gleam:15
[[9, 8], [9, 4], [9, 20], [14, 7], [6, 7], [2, 7], [11, 8], [11, 4], [11, 20]]
src/main.gleam:16
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment