Last active
August 29, 2015 14:16
-
-
Save taylorlapeyre/7e0c2620bb86293517a0 to your computer and use it in GitHub Desktop.
interleave
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
(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"]]) |
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
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"]]) |
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
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