Skip to content

Instantly share code, notes, and snippets.

@taylorlapeyre
Last active August 29, 2015 14:16
Show Gist options
  • Save taylorlapeyre/7e0c2620bb86293517a0 to your computer and use it in GitHub Desktop.
Save taylorlapeyre/7e0c2620bb86293517a0 to your computer and use it in GitHub Desktop.
interleave
(defn interleave [arrays]
(let [largest-length (apply max (map count arrays))]
(->> (for [i (range largest-length)]
(map #(nth % i nil) arrays))
(flatten)
(remove nil?))))
(interleave [[1 2 3] ["a" "b" "c"]])
def interleave(arrays):
largest_array_length = len(max(arrays))
result = []
for i in range(largest_array_length):
for array in arrays:
if len(array) > i: result.append(array[i])
return result
interleave([[1, 3, 5], ["a", "b", "c", "d", "e"]])
def interleave(arrays)
largest_length = arrays.map(&:length).max
result = []
0.upto(largest_length) do |i|
arrays.each { |a| result << a[i] if a.length > i }
end
result
end
interleave [[1, 2, 3], ["a", "b", "c"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment