Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eaed625

Browse files
Merge pull request TheAlgorithms#560 from raklaptudirm/master
Algorithm to print a month's calendar.
2 parents e376870 + f7819ed commit eaed625

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* [CoinChange](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/CoinChange.js)
5959
* [EditDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/EditDistance.js)
6060
* [FibonacciNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/FibonacciNumber.js)
61+
* [FindMonthCalendar](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/FindMonthCalendar.js)
6162
* [KadaneAlgo](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/KadaneAlgo.js)
6263
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LevenshteinDistance.js)
6364
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestCommonSubsequence.js)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* This algorithm accepts a month in the format mm/yyyy.
3+
* And prints out the month's calendar.
4+
* It uses an epoch of 1/1/1900, Monday.
5+
*/
6+
7+
class Month {
8+
constructor () {
9+
this.Days = ['M', 'T', 'W', 'Th', 'F', 'S', 'Su']
10+
this.BDays = ['M', 'Su', 'S', 'F', 'Th', 'W', 'T']
11+
this.epoch = { month: 1, year: 1900 }
12+
this.monthDays = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
13+
this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
14+
}
15+
16+
printCal (days, startDay) {
17+
console.log('M T W Th F S Su')
18+
const dates = []; let i
19+
for (i = 1; i <= days; i++) {
20+
dates.push(i)
21+
}
22+
for (i = 0; i < this.Days.indexOf(startDay); i++) {
23+
dates.unshift(' ')
24+
}
25+
while (true) {
26+
let row = ''
27+
for (i = 0; (i < 7) && (dates.length !== 0); i++) {
28+
row += dates.shift()
29+
while ((row.length % 4) !== 0) {
30+
row += ' '
31+
}
32+
}
33+
console.log(row)
34+
if (dates.length === 0) break
35+
}
36+
}
37+
38+
parseDate (date) {
39+
const dateAr = []; let block = ''; let i
40+
for (i = 0; i < date.length; i++) {
41+
if (date[i] === '/') {
42+
dateAr.push(parseInt(block))
43+
block = ''
44+
continue
45+
}
46+
block += date[i]
47+
}
48+
dateAr.push(parseInt(block))
49+
if (dateAr.length !== 2) throw new Error('Improper string encoding')
50+
const dateOb = { month: dateAr[0], year: dateAr[1] }
51+
return dateOb
52+
}
53+
54+
isLeapYear (year) {
55+
if (((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))) return true
56+
return false
57+
}
58+
59+
isGreater (startDate, endDate) {
60+
if (startDate.year > endDate.year) {
61+
return true
62+
} else if (startDate.year < endDate.year) {
63+
return false
64+
} else if (startDate.month > endDate.month) {
65+
return true
66+
} else if (startDate.month < endDate.month) {
67+
return false
68+
}
69+
return true
70+
}
71+
72+
getDayDiff (startDate, endDate) {
73+
if (this.isGreater(startDate, endDate) === null) {
74+
return 0
75+
} else if ((this.isGreater(startDate, endDate) === true)) {
76+
const midDate = startDate
77+
startDate = endDate
78+
endDate = midDate
79+
}
80+
let diff = 0
81+
while (startDate.year !== endDate.year) {
82+
diff += (this.isLeapYear(startDate.year)) ? 366 : 365
83+
startDate.year = startDate.year + 1
84+
}
85+
while (startDate.month !== endDate.month) {
86+
if (startDate.month < endDate.month) {
87+
if (this.isLeapYear(startDate.year)) diff += this.monthDaysLeap[startDate.month]
88+
else diff += this.monthDays[startDate.month]
89+
startDate.month = startDate.month + 1
90+
} else {
91+
if (this.isLeapYear(startDate.year)) diff -= this.monthDaysLeap[startDate.month - 1]
92+
else diff -= this.monthDays[startDate.month - 1]
93+
startDate.month = startDate.month - 1
94+
}
95+
}
96+
return diff
97+
}
98+
99+
generateMonthCal (date) {
100+
const Month = this.parseDate(date); let day = ''
101+
let difference = this.getDayDiff(this.epoch, Month)
102+
difference = difference % 7
103+
let Month2 = this.parseDate(date)
104+
day = (this.isGreater(Month2, this.epoch)) ? this.Days[difference] : this.BDays[difference]
105+
Month2 = this.parseDate(date)
106+
if (this.isLeapYear(Month2.year)) this.printCal(this.monthDaysLeap[Month2.month], day)
107+
else this.printCal(this.monthDays[Month2.month], day)
108+
}
109+
}
110+
111+
// testing
112+
const x = new Month()
113+
x.generateMonthCal('1/2021')

‎Dynamic-Programming/KadaneAlgo.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function KadaneAlgo (array) {
22
let cummulativeSum = 0
33
let maxSum = 0
4-
for (var i = 0; i < array.length; i++) {
4+
for (let i = 0; i < array.length; i++) {
55
cummulativeSum = cummulativeSum + array[i]
66
if (cummulativeSum < 0) {
77
cummulativeSum = 0
@@ -13,8 +13,8 @@ function KadaneAlgo (array) {
1313
// This function returns largest sum contigous sum in a array
1414
}
1515
function main () {
16-
var myArray = [1, 2, 3, 4, -6]
17-
var result = KadaneAlgo(myArray)
16+
const myArray = [1, 2, 3, 4, -6]
17+
const result = KadaneAlgo(myArray)
1818
console.log(result)
1919
}
2020
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /