E
Eli Bendersky
Hello all,
How would you code, in the most Rubyish way possible: (1) given an
array, decide whether it is strictly increasing, and (2) given two
arrays, decide whether the first is a subsequence of the second.
Here are my trivial implementations:
def increasing_sequence?(seq)
seq.each_with_index do |x, i|
return false if i > 0 and seq[i - 1] >= x
end
return true
end
def is_subsequence_of?(seq, other_seq)
i = 0
seq.each do |x|
while other_seq != x
i += 1
return false if (i >= other_seq.length)
end
end
return true
end
I'm wondering if (1) is there a more Rubyish way to implement this ?
and (2) is there a more efficient way to implement this in pure Ruby ?
Thanks
How would you code, in the most Rubyish way possible: (1) given an
array, decide whether it is strictly increasing, and (2) given two
arrays, decide whether the first is a subsequence of the second.
Here are my trivial implementations:
def increasing_sequence?(seq)
seq.each_with_index do |x, i|
return false if i > 0 and seq[i - 1] >= x
end
return true
end
def is_subsequence_of?(seq, other_seq)
i = 0
seq.each do |x|
while other_seq != x
i += 1
return false if (i >= other_seq.length)
end
end
return true
end
I'm wondering if (1) is there a more Rubyish way to implement this ?
and (2) is there a more efficient way to implement this in pure Ruby ?
Thanks