Too many solvers not providing additional problems!
Here’s another… Assuming you have an array of numeric data, write a
method that returns an array of progressive sums. That is:
prog_sum( [1, 5, 13, -6, 20] ) => [1, 6, 19, 13, 33]
Too many solvers not providing additional problems!
Here’s another… Assuming you have an array of numeric data, write a
method that returns an array of progressive sums. That is:
prog_sum( [1, 5, 13, -6, 20] ) => [1, 6, 19, 13, 33]
On Monday 22 September 2008 23:26:34 Martin DeMello wrote:
On Mon, Sep 22, 2008 at 2:07 PM, Matthew M. [email protected]
wrote:Too many solvers not providing additional problems!
Here’s another… Assuming you have an array of numeric data, write a
method that returns an array of progressive sums. That is:prog_sum( [1, 5, 13, -6, 20] ) => [1, 6, 19, 13, 33]
def prog_sum(ary)
ary.inject([0, []]) {|(s, a), i| [s+i, a<<(s+i)]}.last
end
And what about
def prog_sum(array)
sum = 0; array.collect { |e| sum+=e }
end
class Array
def to_s
collect{|x| x.to_s.gsub(/\n/,"\n| “)}.join(”\n|-- ")
end
end
puts [:foo, [:bar, [:baz, :quux], :hello, :world], :done].to_s
Works fine for given example but fails for example with
puts [:a, [:b, :c]].to_s
a
|-- b
| |-- c
Still working on this…
New question
Write a oneliner next_fib(n) which gives the smallest Fibonacci number
greater than n
2008/9/22 Martin DeMello [email protected]
Summary coming late today.
Technically not one line, but it’s under 80 chars and is recursive…
def f n;self.zip(n==2?self:f(n-1));end;def repeat n;f(n).flatten;end
…doesn’t work for n < 2
Offered quiz is a no-brainer; mostly for golfing…
Given an epsilon, compute PI to that precision.
Todd
Matthew M. [email protected] wrote:
New challange:
Starting with an array, find the first permutation of the elements of
that array that is lexicographically greater than (i.e. sorts after)
the given array.I was tempted to port the C++ next_permutation code, but then I
realized I have class.
I’ve done it, and posted it here, but It’s not one line. I’m not sure
if that’s even doable.
What I do know is that most permutation generators in Ruby just look
at positions, not data, and by doing so, they yield identical
permutations in a single iteration when there are indentical data
elements.
–Ken
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs