将两个数组相互添加,将数组的第一个值添加到第二个数组的第一个值100次?

时间:2021-09-12 12:18:04

I cannot wrap my head around the problem I'm trying to solve. I want to make an array to collect all the days (in numbers) that are the beginning of the month (called starting_days_of_month for 100 years. To do this I have two arrays as follows for normal years and leap years:

我无法解决我正试图解决的问题。我想创建一个数组来收集本月初的所有日期(数字)(称为starting_days_of_month 100年。为此,我有两个数组如下,正常年份和闰年:

beginnings= [0,31,59,90,120,151,181,212,243,273,304,334] # jan = 31, feb =jan + 28, mar = jan + feb + 31, etc.
leap_beginnings = beginnings.collect { |i| i > 32 ? i += 1 : i }

here, beginnings are the beginnings of the month, and leap_beginnings are the beginnings of the month during a leap year (i.e., for leap_beginnings after the second month 1 day is added to each month of beginnings).

这里,开头是月份的开始,并且leap_beginnings是闰年期间的月份的开始(即,对于第二个月1天之后的leap_beginnings被添加到每个月的开始)。

Now I have the years from the beginning of 1900 till the end of 2000. What I want to do is adding the values of the beginnings area from first to last to the starting_days_of_month array, but when the year is divisible by 4 (leap year), I want to add the values of leap_beginnings to the array.

现在我有从1900年开始到2000年底的年份。我想要做的是将起始区域的值从头到尾添加到starting_days_of_month数组,但是当年份可以被4整除时(闰年) ,我想将leap_beginnings的值添加到数组中。

How do I add two arrays to each other where the first value of the array is added to the first value of the second array and a new array is the output (starting_days_of_month) which I can then use as the first value again? (Repeat for 100 years so the output is an array of numbers that contains all the starting days of the month in a 100 year period.)

如何将数组的第一个值添加到第二个数组的第一个值,并将新数组作为输出(starting_days_of_month)再添加两个数组,然后我可以再次使用它作为第一个值? (重复100年,因此输出是一个数字数组,包含100年内该月的所有起始日期。)

1 个解决方案

#1


1  

beginnings = [0,31,59,90,120,151,181,212,243,273,304,334] # jan = 31, feb =jan + 28, mar = jan + feb + 31, etc.
leap_beginnings = beginnings.map.with_index {|n,i| (i > 1) ? n+1 : n }

def leap_year?(n)
  n % 4 == 0 && (n % 400 == 0 || n % 100 != 0)
end

def offset(array)
  if array.size == 0
    0
  else
    array.max + 31    # because december
  end
end

starting_days_of_month = []

(1900..2000).each_with_index do |year, index|
  days = leap_year?(year) ? leap_beginnings : beginnings

  starting_days_of_month += days.map { |day| day + offset(starting_days_of_month) }
end

puts starting_days_of_month

#1


1  

beginnings = [0,31,59,90,120,151,181,212,243,273,304,334] # jan = 31, feb =jan + 28, mar = jan + feb + 31, etc.
leap_beginnings = beginnings.map.with_index {|n,i| (i > 1) ? n+1 : n }

def leap_year?(n)
  n % 4 == 0 && (n % 400 == 0 || n % 100 != 0)
end

def offset(array)
  if array.size == 0
    0
  else
    array.max + 31    # because december
  end
end

starting_days_of_month = []

(1900..2000).each_with_index do |year, index|
  days = leap_year?(year) ? leap_beginnings : beginnings

  starting_days_of_month += days.map { |day| day + offset(starting_days_of_month) }
end

puts starting_days_of_month