[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

too complex for me...

Josselin

10/18/2006 9:21:00 PM

I need help from a ruby master... it's too hard with my present
understanding, any starting key will be apprciated ....

from a period range [start_date, end_date] in ayear , I need to
produce a list of special days like the following :
["2006-10-12","2006-11-08"] => { 10 : [ 12 , 31], 11 : [ 1, 8] }
or
["2006-10-12","2006-12-08"] => { 10 : [ 12 , 31], 11 : [ 1, 30] , 12
: [ 1, 12] }
which means, for each month in the range
the month number : [ period covered in the month] ,

where 'period covered in the month' is
[ 1st day of the period or 1st day of the month , last day of the
period or last day of the month ]

coudl I get any clue ?

thanks a lot

joss

16 Answers

Wilson Bilkovich

10/18/2006 10:10:00 PM

0

On 10/18/06, Josselin <josselin@wanadoo.fr> wrote:
> I need help from a ruby master... it's too hard with my present
> understanding, any starting key will be apprciated ....
>
> from a period range [start_date, end_date] in ayear , I need to
> produce a list of special days like the following :
> ["2006-10-12","2006-11-08"] => { 10 : [ 12 , 31], 11 : [ 1, 8] }
> or
> ["2006-10-12","2006-12-08"] => { 10 : [ 12 , 31], 11 : [ 1, 30] , 12
> : [ 1, 12] }
> which means, for each month in the range
> the month number : [ period covered in the month] ,
>
> where 'period covered in the month' is
> [ 1st day of the period or 1st day of the month , last day of the
> period or last day of the month ]
>
> coudl I get any clue ?
>

The first step, I would say, should be to convert your values from
strings into real Date objects.
require 'date'
d1 = Date.new 2006, 10, 12
d2 = Date.new 2006, 12, 8

date_range = (d1..d2)
date_range.each do |date|
puts date
end

You can then collect up the months and days into whatever format
you're looking for.

Max Muermann

10/18/2006 10:38:00 PM

0

On 10/19/06, Josselin <josselin@wanadoo.fr> wrote:
> I need help from a ruby master... it's too hard with my present
> understanding, any starting key will be apprciated ....
>
> from a period range [start_date, end_date] in ayear , I need to
> produce a list of special days like the following :
> ["2006-10-12","2006-11-08"] => { 10 : [ 12 , 31], 11 : [ 1, 8] }
> or
> ["2006-10-12","2006-12-08"] => { 10 : [ 12 , 31], 11 : [ 1, 30] , 12
> : [ 1, 12] }
> which means, for each month in the range
> the month number : [ period covered in the month] ,
>
> where 'period covered in the month' is
> [ 1st day of the period or 1st day of the month , last day of the
> period or last day of the month ]
>
> coudl I get any clue ?
>
> thanks a lot
>
> joss
>

I had to make a change to the return values. They are now an array (to
maintain order), with the first element being a string looking like
"2006-10", as the code works for multiple dates, the second element is
an array with the period covered.

Lots of special case code for handling year boundaries. I'm sure this
can be tightened up, I'd be interested to hear views as to how...

require 'date'

def coverage rg
from = Date.parse(rg[0])
to = Date.parse(rg[1])

cover = []

# build a list of all month indexes we need to iterate over
if from.year==to.year
# trivial case, both dates fall within the same year
month_range = (from.month..to.month )
else
# complex case - dates in different years

# first year - form month to december
month_range = (from.month..12).to_a

# more than one year covered?
(to.year-from.year-1).times do |year|
# jan to dec
month_range += (1..12).to_a
end
# last year - jan to to.month
month_range += (1..to.month).to_a
end

y = from.year

month_range.each do |m|
if from < Date.new(y,m,1)
first_day = 1
else
first_day = from.day
end

# passing -1 as the day gets the last day of the month
end_of_month = Date.new(y,m,-1)

if to > end_of_month
last_day = end_of_month.day
else
last_day = to.day
end

cover << ["#{y}-#{m}",[first_day, last_day]]

# next year?
y +=1 if m==12
end
cover
end

p coverage(["2006-10-12","2008-02-12"])

Cheers,
Max

Josselin

10/19/2006 5:54:00 AM

0

On 2006-10-19 00:37:43 +0200, "Max Muermann" <ruby@muermann.org> said:

> On 10/19/06, Josselin <josselin@wanadoo.fr> wrote:
>> I need help from a ruby master... it's too hard with my present
>> understanding, any starting key will be apprciated ....
>>
>> from a period range [start_date, end_date] in ayear , I need to
>> produce a list of special days like the following :
>> ["2006-10-12","2006-11-08"] => { 10 : [ 12 , 31], 11 : [ 1, 8] }
>> or
>> ["2006-10-12","2006-12-08"] => { 10 : [ 12 , 31], 11 : [ 1, 30] , 12
>> : [ 1, 12] }
>> which means, for each month in the range
>> the month number : [ period covered in the month] ,
>>
>> where 'period covered in the month' is
>> [ 1st day of the period or 1st day of the month , last day of the
>> period or last day of the month ]
>>
>> coudl I get any clue ?
>>
>> thanks a lot
>>
>> joss
>>
>
> I had to make a change to the return values. They are now an array (to
> maintain order), with the first element being a string looking like
> "2006-10", as the code works for multiple dates, the second element is
> an array with the period covered.
>
> Lots of special case code for handling year boundaries. I'm sure this
> can be tightened up, I'd be interested to hear views as to how...
>
> require 'date'
>
> def coverage rg
> from = Date.parse(rg[0])
> to = Date.parse(rg[1])
>
> cover = []
>
> # build a list of all month indexes we need to iterate over
> if from.year==to.year
> # trivial case, both dates fall within the same year
> month_range = (from.month..to.month )
> else
> # complex case - dates in different years
>
> # first year - form month to december
> month_range = (from.month..12).to_a
>
> # more than one year covered?
> (to.year-from.year-1).times do |year|
> # jan to dec
> month_range += (1..12).to_a
> end
> # last year - jan to to.month
> month_range += (1..to.month).to_a
> end
>
> y = from.year
>
> month_range.each do |m|
> if from < Date.new(y,m,1)
> first_day = 1
> else
> first_day = from.day
> end
>
> # passing -1 as the day gets the last day of the month
> end_of_month = Date.new(y,m,-1)
>
> if to > end_of_month
> last_day = end_of_month.day
> else
> last_day = to.day
> end
>
> cover << ["#{y}-#{m}",[first_day, last_day]]
>
> # next year?
> y +=1 if m==12
> end
> cover
> end
>
> p coverage(["2006-10-12","2008-02-12"])
>
> Cheers,
> Max

Thanks a lot I'll work on it today...

joss

Erik Veenstra

10/19/2006 8:40:00 PM

0

A possible solution...

It clearly shows every step in the process. Usually, the 5
lines within the loop are reduced to 1 or 2 lines, but you'll
end up with less readable code.
Not a good idea for newbies... ;]

Notice that it isn't very efficient when handling very long
periods.

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

require "date"

start = Date.parse(ARGV.shift)
finish = Date.parse(ARGV.shift)
hash = {}

(start..finish).each do |date|
month = [date.year, date.month]
days = hash[month] || []
first_day = days.shift || date.day
last_day = date.day
hash[month] = [first_day, last_day]
end

p start.to_s
p finish.to_s
p hash

----------------------------------------------------------------


Klaus Schadenfreude

9/22/2012 3:58:00 PM

0

>deep <deep@dudu.org> wrote in talk.politics.guns :

>On Sat, 22 Sep 2012 08:47:00 -0700 (PDT), STEADY EDDY
><steadyeddy4u@yahoo.com> wrote:
>
>>On Sep 21, 7:39??pm, Flint <age...@section31.org> wrote:
>>> http://www.philly.com/philly/news/politics/state/20120921_N......
>>>
>>> Off to the tall weeds with you LookieWookie, TooMassiveTwerp, and Deep
>>> Dipshit!
>>>
>>> Laugh...Laugh...Laugh...
>>>
>>> --
>>> MFB
>>
>>Obama is leading in the polls that count. Romney "misspoke" at a fund
>>raiser and pretty much flushed his campaign down the toilet. It
>>doesn't matter to me who wins the election in 2012. I want Romney but,
>>if Obama wins I will still make money.
>
>Whoever gets elected, the stock market is going to crash and
>hyperinflation will make paper money nearly worthless.

Got a month, date, and year??

deep

9/22/2012 4:18:00 PM

0

On Sat, 22 Sep 2012 08:57:42 -0700, Klaus Schadenfreude
<klausschadenfreude@yahoo.com> wrote:

>>deep <deep@dudu.org> wrote in talk.politics.guns :
>
>>On Sat, 22 Sep 2012 08:47:00 -0700 (PDT), STEADY EDDY
>><steadyeddy4u@yahoo.com> wrote:
>>
>>>On Sep 21, 7:39?pm, Flint <age...@section31.org> wrote:
>>>> http://www.philly.com/philly/news/politics/state/20120921_N......
>>>>
>>>> Off to the tall weeds with you LookieWookie, TooMassiveTwerp, and Deep
>>>> Dipshit!
>>>>
>>>> Laugh...Laugh...Laugh...
>>>>
>>>> --
>>>> MFB
>>>
>>>Obama is leading in the polls that count. Romney "misspoke" at a fund
>>>raiser and pretty much flushed his campaign down the toilet. It
>>>doesn't matter to me who wins the election in 2012. I want Romney but,
>>>if Obama wins I will still make money.
>>
>>Whoever gets elected, the stock market is going to crash and
>>hyperinflation will make paper money nearly worthless.
>
>Got a month, date, and year??

Read up on the global depression leading up to WWII and you will get
an idea of how things will probably play out.

Klaus Schadenfreude

9/22/2012 4:38:00 PM

0

>deep <deep@dudu.org> wrote in talk.politics.guns :

>On Sat, 22 Sep 2012 08:57:42 -0700, Klaus Schadenfreude
><klausschadenfreude@yahoo.com> wrote:
>
>>>deep <deep@dudu.org> wrote in talk.politics.guns :
>>
>>>On Sat, 22 Sep 2012 08:47:00 -0700 (PDT), STEADY EDDY
>>><steadyeddy4u@yahoo.com> wrote:
>>>
>>>>On Sep 21, 7:39??pm, Flint <age...@section31.org> wrote:
>>>>> http://www.philly.com/philly/news/politics/state/20120921_N......
>>>>>
>>>>> Off to the tall weeds with you LookieWookie, TooMassiveTwerp, and Deep
>>>>> Dipshit!
>>>>>
>>>>> Laugh...Laugh...Laugh...
>>>>>
>>>>> --
>>>>> MFB
>>>>
>>>>Obama is leading in the polls that count. Romney "misspoke" at a fund
>>>>raiser and pretty much flushed his campaign down the toilet. It
>>>>doesn't matter to me who wins the election in 2012. I want Romney but,
>>>>if Obama wins I will still make money.
>>>
>>>Whoever gets elected, the stock market is going to crash and
>>>hyperinflation will make paper money nearly worthless.
>>
>>Got a month, date, and year??
>
>Read up on the global depression leading up to WWII and you will get
>an idea of how things will probably play out.

Great! We came out of that smelling like a rose!

steadyeddy4u

9/22/2012 5:10:00 PM

0

On Sep 22, 8:54 am, deep <d...@dudu.org> wrote:
> On Sat, 22 Sep 2012 08:47:00 -0700 (PDT), STEADY EDDY
>
>
>
>
>
>
>
>
>
> <steadyedd...@yahoo.com> wrote:
> >On Sep 21, 7:39 pm, Flint <age...@section31.org> wrote:
> >>http://www.philly.com/philly/news/politics/state/20120921_N......
>
> >> Off to the tall weeds with you LookieWookie, TooMassiveTwerp, and Deep
> >> Dipshit!
>
> >> Laugh...Laugh...Laugh...
>
> >> --
> >> MFB
>
> >Obama is leading in the polls that count. Romney "misspoke" at a fund
> >raiser and pretty much flushed his campaign down the toilet. It
> >doesn't matter to me who wins the election in 2012. I want Romney but,
> >if Obama wins I will still make money.
>
> Whoever gets elected, the stock market is going to crash and
> hyperinflation will make paper money nearly worthless.

Doom and gloom DUDU

deep

9/22/2012 5:31:00 PM

0

On Sat, 22 Sep 2012 10:10:04 -0700 (PDT), STEADY EDDY
<steadyeddy4u@yahoo.com> wrote:

>On Sep 22, 8:54?am, deep <d...@dudu.org> wrote:
>> On Sat, 22 Sep 2012 08:47:00 -0700 (PDT), STEADY EDDY
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> <steadyedd...@yahoo.com> wrote:
>> >On Sep 21, 7:39 pm, Flint <age...@section31.org> wrote:
>> >>http://www.philly.com/philly/news/politics/state/20120921_N......
>>
>> >> Off to the tall weeds with you LookieWookie, TooMassiveTwerp, and Deep
>> >> Dipshit!
>>
>> >> Laugh...Laugh...Laugh...
>>
>> >> --
>> >> MFB
>>
>> >Obama is leading in the polls that count. Romney "misspoke" at a fund
>> >raiser and pretty much flushed his campaign down the toilet. It
>> >doesn't matter to me who wins the election in 2012. I want Romney but,
>> >if Obama wins I will still make money.
>>
>> Whoever gets elected, the stock market is going to crash and
>> hyperinflation will make paper money nearly worthless.
>
>Doom and gloom DUDU

"Those that do not learn from history are doomed to repeat it."
George Santayana

Everything right now is exactly like it was in 1930, heading for the
continued stock market decline, hyperinflation, and WWII. There is no
reason whatsoever to think today is going to be any different.

RD Sandman

9/22/2012 7:19:00 PM

0

deep <deep@dudu.org> wrote in
news:e7tr581pkal05hfoaum8gc31u9koljrt08@4ax.com:

> On Sat, 22 Sep 2012 10:10:04 -0700 (PDT), STEADY EDDY
> <steadyeddy4u@yahoo.com> wrote:
>
>>On Sep 22, 8:54?am, deep <d...@dudu.org> wrote:
>>> On Sat, 22 Sep 2012 08:47:00 -0700 (PDT), STEADY EDDY
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> <steadyedd...@yahoo.com> wrote:
>>> >On Sep 21, 7:39 pm, Flint <age...@section31.org> wrote:
>>> >>http://www.philly.com/philly/news/politics/state/20120921...
>>> >>om...
>>>
>>> >> Off to the tall weeds with you LookieWookie, TooMassiveTwerp, and
>>> >> Deep Dipshit!
>>>
>>> >> Laugh...Laugh...Laugh...
>>>
>>> >> --
>>> >> MFB
>>>
>>> >Obama is leading in the polls that count. Romney "misspoke" at a
>>> >fund raiser and pretty much flushed his campaign down the toilet.
>>> >It doesn't matter to me who wins the election in 2012. I want
>>> >Romney but, if Obama wins I will still make money.
>>>
>>> Whoever gets elected, the stock market is going to crash and
>>> hyperinflation will make paper money nearly worthless.
>>
>>Doom and gloom DUDU
>
> "Those that do not learn from history are doomed to repeat it."
> George Santayana
>
> Everything right now is exactly like it was in 1930, heading for the
> continued stock market decline,

I'm doing rather well. Perhaps you should change parts of your portfolio
if you even have one.




--

Democracy means that anyone can grow up to be President,

And anyone who doesn't grow up can be Vice President.


Sleep well, tonight.....

RD (The Sandman)