Epic Systems Interview Question
Software Engineer / Developersimport calendar
def printCal():
year = raw_input(int("Which year?"))
print calendar.calendar(year)
we can make the calender by using the concept of odd days
like for example......
suppose the give year is 1980 now calculate the odd days for the 1 Jan 1980.
Odd days in a year 365mod7 gives 1
Odd days in a year 366mod7 gives 2 leap year.
so by this we can calculate the odd days in
100 year = 5 odd days
200 year = 3 odd days
300 year = 1 odd days
and 400,800,1600 have 0 odd days.
now claculate the odd days for 1 Jan 1980
No. of Year odd days
1600 0
300 1
79 0 ((79/4)*2+(79-(79/4)))mod7=0
-----------------
1979 1
n for 1 Jan 1
-----------------
total 2 odd days
and 0 is Sunday 1 is Monday 2 is Tuesday and so on
so the year 1980 start with Tuesday now we can easily make the calender for the year.
i hope this will help............
//The following program should print the calender given a year
// to preserve whitespace
//============================================================================
// Name : CalendarCPP.cpp
// Author :
// Version :
// Copyright :
//============================================================================
#include <iostream>
#include <string>
using namespace std;
int main() {
int year;
bool leap = false;
char *months[] = {"January","February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
cout << "Enter the year: " << endl;
cin>>year;
// check for leap year
/*
** The logic is that the year is either divisible by both
** 100 and 4 , OR its only divisible by 4 not by hundred
*/
if(year%400 ==0 || (year%100 != 0 && year%4 == 0)) {
cout<<"Year %d is a leap year\n"<<year;
leap = true;
}
else {
cout<<"Year %d is not a leap year\n"<<year;
}
//January to June
for(int i=0; i<=6; i++) { // month
for(int j=1; j<=31; j++) { // days
if(i!=1) { //Not February
if(i%2==0){ //31 days
cout<<months[i]<<" "<<j<<","<<year<<endl;
} else { //30 days
if(j<31) {
cout<<months[i]<<" "<<j<<","<<year<<endl;
}
}
} else { //February
if(j<30) {
if(leap) {
cout<<months[i]<<" "<<j<<","<<year<<endl;
} else {
if(j<29) {
cout<<months[i]<<" "<<j<<","<<year<<endl;
}
}
}
}
}
}
//August to December
for(int i=7;i<31;i++) { //month
for(int j=1;j<=31;j++){ //days
if(i%2!=0){ //31 days
cout<<months[i]<<" "<<j<<","<<year<<endl;
} else { //30 days
if(j<31)
cout<<months[i]<<" "<<j<<","<<year<<endl;
}
}
}
return 0;
}
//================================================================
// to preserve whitespace
Take the current date and day of the week.
Find the difference in days between the current date and 1 Jan <given year>
(diff number) mod 7 should return you the correct day of the week for given year
Start printing the calendar (in which ever way you want!)
#include <iostream>
using namespace std;
void printMonthTitle(int year, int month);
int getTotalNumOfDays(int year, int month);
int getNumOfDaysInMonth(int year, int month);
int getStartDay(int year, int month);
bool isLeapYear(int year);
void printMonthBody(int startDay, int numOfDaysInMonth);
const char* getMonthName(int month);
/** Print the calendar for a month in a year */
void printMonth(int year, int month) {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
// Print headings
printMonthTitle(year, month);
// Print body
printMonthBody(startDay, numOfDaysInMonth);
}
/** Get the start day of the first day in a month */
int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1800 = 3;
int totalNumOfDays = getTotalNumOfDays(year, month);
// Return the start day
return (int)((totalNumOfDays + startDay1800) % 7);
}
/** Get the total number of days since Jan 1, 1800 */
int getTotalNumOfDays(int year, int month) {
static int tillYear=0;
if (!tillYear)
{
// Get the total days from 1800 to year -1
for (int i = 1800; i < year; i++)
{
if (isLeapYear(i))
tillYear = tillYear + 366;
else
tillYear = tillYear + 365;
}
}
int total = tillYear;
// Add days from Jan to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
/** Get the number of days in a month */
int getNumOfDaysInMonth(int year, int month) {
if (month == 1 || month==3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;
return 0; // If month is incorrect.
}
/** Determine if it is a leap year */
bool isLeapYear(int year) {
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
return false;
}
/** Print month body */
void printMonthBody(int startDay, int numOfDaysInMonth) {
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
cout <<" ";
for (i = 1; i <= numOfDaysInMonth; i++) {
if (i < 10)
cout <<" "<<i;
else
cout <<" "<< i;
if ((i + startDay) % 7 == 0)
cout<<endl;
}
cout <<"\n";
}
/** Print the month title, i.e. May, 1999 */
void printMonthTitle(int year, int month) {
cout <<"-----------------------------\n";
cout <<" Sun Mon Tue Wed Thu Fri Sat\n";
cout <<" "<< getMonthName(month) <<", "<< year<<endl;
}
/** Get the English name for the month */
const char* getMonthName(int month) {
switch (month) {
case 1: return "January"; break;
case 2: return "February"; break;
case 3: return "March"; break;
case 4: return "April"; break;
case 5: return "May"; break;
case 6: return "June"; break;
case 7: return "July"; break;
case 8: return "August"; break;
case 9: return "September"; break;
case 10: return "October"; break;
case 11: return "November"; break;
case 12: return "December";
}
return "";
}
int main()
{
// Convert string into integer
int month = 1;
int year = 2015;
// Print calendar for the month of the year
printMonth(year, month);
return 0;
}
- Rock September 08, 2009