Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
45
Task/Five-weekends/JavaScript/five-weekends-1.js
Normal file
45
Task/Five-weekends/JavaScript/five-weekends-1.js
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
function startsOnFriday(month, year)
|
||||
{
|
||||
// 0 is Sunday, 1 is Monday, ... 5 is Friday, 6 is Saturday
|
||||
return new Date(year, month, 1).getDay() === 5;
|
||||
}
|
||||
function has31Days(month, year)
|
||||
{
|
||||
return new Date(year, month, 31).getDate() === 31;
|
||||
}
|
||||
function checkMonths(year)
|
||||
{
|
||||
var month, count = 0;
|
||||
for (month = 0; month < 12; month += 1)
|
||||
{
|
||||
if (startsOnFriday(month, year) && has31Days(month, year))
|
||||
{
|
||||
count += 1;
|
||||
document.write(year + ' ' + month + '<br>');
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
function fiveWeekends()
|
||||
{
|
||||
var
|
||||
startYear = 1900,
|
||||
endYear = 2100,
|
||||
year,
|
||||
monthTotal = 0,
|
||||
yearsWithoutFiveWeekends = [],
|
||||
total = 0;
|
||||
for (year = startYear; year <= endYear; year += 1)
|
||||
{
|
||||
monthTotal = checkMonths(year);
|
||||
total += monthTotal;
|
||||
// extra credit
|
||||
if (monthTotal === 0)
|
||||
yearsWithoutFiveWeekends.push(year);
|
||||
}
|
||||
document.write('Total number of months: ' + total + '<br>');
|
||||
document.write('<br>');
|
||||
document.write(yearsWithoutFiveWeekends + '<br>');
|
||||
document.write('Years with no five-weekend months: ' + yearsWithoutFiveWeekends.length + '<br>');
|
||||
}
|
||||
fiveWeekends();
|
||||
61
Task/Five-weekends/JavaScript/five-weekends-2.js
Normal file
61
Task/Five-weekends/JavaScript/five-weekends-2.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
var Months = [
|
||||
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
||||
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
|
||||
];
|
||||
|
||||
var leap = 0,
|
||||
// Relative offsets between first day of each month
|
||||
offset = [3,0,3,2,3,2,3,3,2,3,2,3],
|
||||
|
||||
// Months that contain 31 days
|
||||
longMonths = [1,3,5,7,8,10,12],
|
||||
|
||||
startYear = 1900,
|
||||
year = startYear,
|
||||
endYear = 2100,
|
||||
|
||||
// Jan 1, 1900 starts on a Monday
|
||||
day = 1,
|
||||
|
||||
totalPerYear = 0,
|
||||
total = 0,
|
||||
without = 0;
|
||||
|
||||
for (; year < endYear + 1; year++) {
|
||||
leap = totalPerYear = 0;
|
||||
|
||||
if (year % 4 === 0) {
|
||||
if (year % 100 === 0) {
|
||||
if (year % 400 === 0) {
|
||||
leap = 1;
|
||||
}
|
||||
} else {
|
||||
leap = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < offset.length; i++) {
|
||||
for (var j = 0; day === 5 && j < longMonths.length; j++) {
|
||||
if (i + 1 === longMonths[j]) {
|
||||
console.log(year + '-' + Months[i]);
|
||||
totalPerYear++;
|
||||
total++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// February -- if leap year, then +1 day
|
||||
if (i == 1) {
|
||||
day = (day + leap) % 7;
|
||||
} else {
|
||||
day = (day + offset[i]) % 7;
|
||||
}
|
||||
}
|
||||
|
||||
if (totalPerYear === 0) {
|
||||
without++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Number of months that have five full weekends from 1900 to 2100: ' + total);
|
||||
console.log('Number of years without any five full weekend months: ' + without);
|
||||
54
Task/Five-weekends/JavaScript/five-weekends-3.js
Normal file
54
Task/Five-weekends/JavaScript/five-weekends-3.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// longMonthsStartingFriday :: Int -> Int
|
||||
function longMonthsStartingFriday(y) {
|
||||
return [0, 2, 4, 6, 7, 9, 11]
|
||||
.filter(function (m) {
|
||||
return (new Date(Date.UTC(y, m, 1)))
|
||||
.getDay() === 5;
|
||||
});
|
||||
}
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1))
|
||||
.map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
var lstNames = [
|
||||
'January', '', 'March', '', 'May', '',
|
||||
'July', 'August', '', 'October', '', 'December'
|
||||
],
|
||||
|
||||
lstYears = range(1900, 2100),
|
||||
|
||||
lstFullMonths = lstYears
|
||||
.reduce(function (a, y) {
|
||||
var strYear = y.toString();
|
||||
|
||||
return a.concat(
|
||||
longMonthsStartingFriday(y)
|
||||
.map(function (m) {
|
||||
return strYear + ' ' + lstNames[m];
|
||||
})
|
||||
);
|
||||
}, []),
|
||||
|
||||
lstLeanYears = lstYears
|
||||
.filter(function (y) {
|
||||
return longMonthsStartingFriday(y)
|
||||
.length === 0;
|
||||
});
|
||||
|
||||
return JSON.stringify({
|
||||
number: lstFullMonths.length,
|
||||
firstFive: lstFullMonths.slice(0, 5),
|
||||
lastFive: lstFullMonths.slice(-5),
|
||||
leanYearCount: lstLeanYears.length
|
||||
},
|
||||
null, 2
|
||||
);
|
||||
})();
|
||||
60
Task/Five-weekends/JavaScript/five-weekends-4.js
Normal file
60
Task/Five-weekends/JavaScript/five-weekends-4.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
(() => {
|
||||
// longMonthsStartingFriday :: Int -> [Int]
|
||||
const longMonthsStartingFriday = y =>
|
||||
filter(m => (new Date(Date.UTC(y, m, 1)))
|
||||
.getDay() === 5, [0, 2, 4, 6, 7, 9, 11]);
|
||||
|
||||
// Years -> YearMonths
|
||||
// fullMonths :: [Int] -> [String]
|
||||
const fullMonths = xs =>
|
||||
foldl((a, y) => a.concat(
|
||||
map(m => `${y.toString()} ${[
|
||||
'January', '', 'March', '', 'May', '',
|
||||
'July', 'August', '', 'October', '', 'December'
|
||||
][m]}`, longMonthsStartingFriday(y))
|
||||
), [], xs);
|
||||
|
||||
// leanYears :: [Int] -> [Int]
|
||||
const leanYears = years =>
|
||||
filter(y => longMonthsStartingFriday(y)
|
||||
.length === 0, years);
|
||||
|
||||
// GENERIC ----------------------------------------------------------------
|
||||
|
||||
// A list of functions applied to a list of arguments
|
||||
// <*> :: [(a -> b)] -> [a] -> [b]
|
||||
const ap = (fs, xs) => //
|
||||
[].concat.apply([], fs.map(f => //
|
||||
[].concat.apply([], xs.map(x => [f(x)]))));
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// filter :: (a -> Bool) -> [a] -> [a]
|
||||
const filter = (f, xs) => xs.filter(f);
|
||||
|
||||
// foldl :: (b -> a -> b) -> b -> [a] -> b
|
||||
const foldl = (f, a, xs) => xs.reduce(f, a);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x, null, 2);
|
||||
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
const [lstFullMonths, lstLeanYears] = ap(
|
||||
[fullMonths, leanYears], [enumFromTo(1900, 2100)]
|
||||
);
|
||||
|
||||
return show({
|
||||
number: lstFullMonths.length,
|
||||
firstFive: lstFullMonths.slice(0, 5),
|
||||
lastFive: lstFullMonths.slice(-5),
|
||||
leanYearCount: lstLeanYears.length
|
||||
});
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue