Showing posts with label date comparison. Show all posts
Showing posts with label date comparison. Show all posts

14 August 2011

Blocks in Ruby

Blocks are one of the coolest things to have when you are coming from a language like C++ or Java. Say you are writing a function and you want to have some functionality within it which should be handled totally by the caller. Well, blocks come for the rescue.

Say I have a method like this.

There is a sweet keyword in Ruby to replace the second line of the test_method called yield.


And you can now make a call like,


Would output,
start
From block
end

Nothing great if you already are a ruby nerd. But for Java blokes, blocks are something to think for.

Cheers!
Braga

23 August 2010

Project Euler : How many Sundays fell on the first of the month during the twentieth century?

Problem 19

You are given the following information, but you may prefer to do some research for yourself.
  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?


Solution (in Ruby)

This particular solution does not involve any hard calculations, just run through the dates starting from the first sunday step by 7 days and check if the sunday lies on the first day of the month.

require 'date'
d1, d2, total_sundays = [Date::civil(1901, 1, 1), Date::civil(2000, 12, 31), 0]
d1 +=1 while (d1.wday != 0)
d1.step(d2, 7){|date| total_sundays+=1 if date.day == 1}
puts "Total number of Sundays : #{total_sundays}"

Hover here to see the solution

06 February 2010

Difference between two days implemented in C

Although Java is my forte, I started my early days by coding with C. The below program is to get the difference in days between two dates. The program is simple and has well defined comments to explain for itself. Thought I would post it here.

#include<stdio.h>
#include<conio.h>
#include<dos.h>
int datecmp(struct date,struct date);
int leap(int);
void main()
{
 struct date a,b;
 int leep;
 clrscr();
 b.da_year=2000;
 b.da_mon=7;
 b.da_day=1;
 getdate(&a);
 printf("%u\t%u\t%u\t",b.da_day,b.da_mon,b.da_year);
 printf("\n%u\t%u\t%u\t",a.da_day,a.da_mon,a.da_year);
 datecmp(a,b);
 /*if((leep=leap(b.da_year))==1)
  printf("\nThe year is leep year");
 else
  printf("\nThe year is an ordinary year");*/
 getch();
}


int datecmp(struct date a,struct date b)
{
 struct date c;
 char days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 int y=365;
 int i,j;
 int td=0;
 c.da_year=a.da_year-b.da_year;
 c.da_mon=a.da_mon-b.da_mon;
 if(c.da_year==0)//SAME YEAR BUT DIFFERENT MONTH OR SAME MONTH
 {
  if(c.da_mon==0)
   td=a.da_day-b.da_day;
  else
  {
   for(i=0,j=b.da_mon;i<(c.da_mon-1);i++,j++)
    td+=days[j];
   td+=days[b.da_mon-1]-b.da_day;
   td+=a.da_day-1;
  }
 }
 else if(c.da_year>1 && c.da_mon>1)//YEAR DIFF B/W TWO INPUTS IS >THAN 1 YEAR
 {
  if(b.da_mon<3 && leap(b.da_year)==1)
   td=1;
  else
   td=0;
  for(i=0,j=b.da_year+1;i<(c.da_year);i++,j++)
    td=td+365+leap(j);
  if(c.da_mon==0)
   td+=a.da_day-b.da_day;
  else
  {
   for(i=0,j=b.da_mon;i<(c.da_mon-1);i++,j++)
    td+=days[j];
   td+=days[b.da_mon-1]-b.da_day;
   td+=a.da_day-1+leap(a.da_year);
  }
 }
 else if(c.da_year==1)//YEAR DIFF B/W TWO INPUTS IS==1 YEAR
 {
  if(c.da_mon>0)
  {
   if(b.da_mon>1 && leap(b.da_year)==1)
    td=1;
   else
    td=0;
   for(i=0,j=b.da_year+1;i<(c.da_year);i++,j++)
     td=td+365+leap(j);
   if(c.da_mon==0)
    td+=a.da_day-b.da_day;
   else
   {
    for(i=0,j=b.da_mon;i<(c.da_mon-1);i++,j++)
     td+=days[j];
    td+=days[b.da_mon-1]-b.da_day;
    td+=a.da_day-1+leap(a.da_year);
   }
  }
  else if(c.da_mon==0 && c.da_day>0)
  {
    if(b.da_mon>1)
    td=0;
   else
    td=1;
   for(i=0,j=b.da_year+1;i<(c.da_year);i++,j++)
     td=td+365+leap(j);
    td+=a.da_day-b.da_day;
  }
 }
 else if(c.da_year==1)
 {

 }

 printf("\n The days are %d",td);
 getch();
}
int leap(int year)//LEAP YEAR FUNCTION
{
 if(year%4==0)
 {
  if(year%100==0)
  {
   if(year%400==0)
    return(1);
   else
    return(0);
  }
 else
  return(1);
 }
 else
  return(0);
}

Cheers,
Bragaadeesh.