Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,5 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Date and time
|
||||
- Puzzles
|
||||
note: Five weekends
|
||||
note: Date and time
|
||||
|
|
|
|||
40
Task/Five-weekends/CoffeeScript/five-weekends-1.coffee
Normal file
40
Task/Five-weekends/CoffeeScript/five-weekends-1.coffee
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
startsOnFriday = (month, year) ->
|
||||
# 0 is Sunday, 1 is Monday, ... 5 is Friday, 6 is Saturday
|
||||
new Date(year, month, 1).getDay() == 5
|
||||
|
||||
has31Days = (month, year) ->
|
||||
new Date(year, month, 31).getDate() == 31
|
||||
|
||||
checkMonths = (year) ->
|
||||
month = undefined
|
||||
count = 0
|
||||
month = 0
|
||||
while month < 12
|
||||
if startsOnFriday(month, year) and has31Days(month, year)
|
||||
count += 1
|
||||
console.log year + ' ' + month + ''
|
||||
month += 1
|
||||
count
|
||||
|
||||
fiveWeekends = ->
|
||||
startYear = 1900
|
||||
endYear = 2100
|
||||
year = undefined
|
||||
monthTotal = 0
|
||||
yearsWithoutFiveWeekends = []
|
||||
total = 0
|
||||
year = startYear
|
||||
while year <= endYear
|
||||
monthTotal = checkMonths(year)
|
||||
total += monthTotal
|
||||
# extra credit
|
||||
if monthTotal == 0
|
||||
yearsWithoutFiveWeekends.push year
|
||||
year += 1
|
||||
console.log 'Total number of months: ' + total + ''
|
||||
console.log ''
|
||||
console.log yearsWithoutFiveWeekends + ''
|
||||
console.log 'Years with no five-weekend months: ' + yearsWithoutFiveWeekends.length + ''
|
||||
return
|
||||
|
||||
fiveWeekends()
|
||||
30
Task/Five-weekends/CoffeeScript/five-weekends-2.coffee
Normal file
30
Task/Five-weekends/CoffeeScript/five-weekends-2.coffee
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
1901 2
|
||||
1902 7
|
||||
1903 4
|
||||
1904 0
|
||||
1904 6
|
||||
1905 11
|
||||
1907 2
|
||||
1908 4
|
||||
1909 0
|
||||
1909 9
|
||||
1910 6
|
||||
1911 11
|
||||
1912 2
|
||||
1913 7
|
||||
1914 4
|
||||
1915 0
|
||||
1915 9
|
||||
1916 11
|
||||
1918 2
|
||||
1919 7
|
||||
1920 9
|
||||
1921 6
|
||||
1922 11
|
||||
1924 7
|
||||
..
|
||||
|
||||
Total number of months: 201
|
||||
1900,1906,1917,1923,1928,1934,1945,1951,1956,1962,1973,1979,1984,1990,2001,2007,2012,2018,2029,2035,2040,2046,2057,2063,2068,2074,2085,2091,2096
|
||||
|
||||
Years with no five-weekend months: 29
|
||||
18
Task/Five-weekends/Elixir/five-weekends.elixir
Normal file
18
Task/Five-weekends/Elixir/five-weekends.elixir
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
defmodule Date do
|
||||
@months { "January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December" }
|
||||
|
||||
def five_weekends(year) do
|
||||
for m <-[1,3,5,7,8,10,12], :calendar.day_of_the_week(year, m, 31) == 7, do: elem(@months, m-1)
|
||||
end
|
||||
end
|
||||
|
||||
months = Enum.map(1900..2100, fn year -> {year, Date.five_weekends(year)} end)
|
||||
{none, months5} = Enum.partition(months, fn {_,m} -> Enum.empty?(m) end)
|
||||
count = Enum.reduce(months5, 0, fn {year, months}, acc ->
|
||||
IO.puts "#{year} : #{Enum.join(months, ", ")}"
|
||||
acc + length(months)
|
||||
end)
|
||||
IO.puts "Found #{count} month with 5 weekends."
|
||||
IO.puts "\nFound #{length(none)} years with no month having 5 weekends:"
|
||||
IO.puts "#{inspect Enum.map(none, fn {y,_}-> y end)}"
|
||||
26
Task/Five-weekends/Julia/five-weekends.julia
Normal file
26
Task/Five-weekends/Julia/five-weekends.julia
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
isdefined(:Date) || using Dates
|
||||
|
||||
const wday = Dates.Fri
|
||||
const lo = 1900
|
||||
const hi = 2100
|
||||
const showres = 5
|
||||
|
||||
mfive = recur(Date(lo, 1):Month(1):Date(hi, 12)) do m
|
||||
Dates.daysinmonth(m) == 31 && Dates.dayofweek(m) == wday
|
||||
end
|
||||
|
||||
println("Considering the years from ", lo, " to ", hi, ".\n")
|
||||
println("There are ", length(mfive), " months having 5 3-day weekends.")
|
||||
|
||||
println("The first ", showres, " such months are:")
|
||||
for m in mfive[1:showres]
|
||||
println(" ", Dates.monthname(m), " ", Dates.year(m))
|
||||
end
|
||||
|
||||
println("\nThe last ", showres, " such months are:")
|
||||
for m in mfive[end-showres+1:end]
|
||||
println(" ", Dates.monthname(m), " ", Dates.year(m))
|
||||
end
|
||||
|
||||
print("\nThere are ", length(filter(y -> !(y in year(mfive)), lo:hi)))
|
||||
println(" years that have no such months.")
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
# A month has 5 weekends iff it has 31 days and starts on Friday.
|
||||
|
||||
my @years = 1900 .. 2100;
|
||||
my @ym = @years X 1, 3, 5, 7, 8, 10, 12; # Months with 31 days
|
||||
my @has31 = 1, 3, 5, 7, 8, 10, 12;
|
||||
my @happy = ($_ when *.day-of-week == 5 for (@years X @has31).map(-> ($y, $m) { Date.new: $y, $m, 1 }));
|
||||
|
||||
my @happy = @ym.map({ Date.new: $^a, $^b, 1 }).grep: { .day-of-week == 5 };
|
||||
|
||||
say 'Happy month count: ', +@happy;
|
||||
say 'Happy month count: ', +@happy;
|
||||
say 'First happy months: ' ~ @happy[^5];
|
||||
say 'Last happy months: ' ~ @happy[*-5 .. *];
|
||||
say 'Dreary years count: ', @years - @happy».year.uniq;
|
||||
say 'Dreary years count: ', @years - @happy».year.squish;
|
||||
|
|
|
|||
20
Task/Five-weekends/PowerShell/five-weekends.psh
Normal file
20
Task/Five-weekends/PowerShell/five-weekends.psh
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
$fiveWeekends = @()
|
||||
$yearsWithout = @()
|
||||
foreach ($y in 1900..2100) {
|
||||
$hasFiveWeekendMonth = $FALSE
|
||||
foreach ($m in @("01","03","05","07","08",10,12)) {
|
||||
if ((Get-Date "$y-$m-1").DayOfWeek -eq "Friday") {
|
||||
$fiveWeekends += "$y-$m"
|
||||
$hasFiveWeekendMonth = $TRUE
|
||||
}
|
||||
}
|
||||
if ($hasFiveWeekendMonth -eq $FALSE) {
|
||||
$yearsWithout += $y
|
||||
}
|
||||
}
|
||||
Write-Output "Between the years 1900 and 2100, inclusive, there are $($fiveWeekends.count) months with five full weekends:"
|
||||
Write-Output "$($fiveWeekends[0..4] -join ","),...,$($fiveWeekends[-5..-1] -join ",")"
|
||||
|
||||
Write-Output ""
|
||||
Write-Output "Extra Credit: these $($yearsWithout.count) years have no such month:"
|
||||
Write-Output ($yearsWithout -join ",")
|
||||
35
Task/Five-weekends/Scala/five-weekends.scala
Normal file
35
Task/Five-weekends/Scala/five-weekends.scala
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import java.util.Calendar._
|
||||
import java.util.GregorianCalendar
|
||||
|
||||
import org.scalatest.{FlatSpec, Matchers}
|
||||
|
||||
class FiveWeekends extends FlatSpec with Matchers {
|
||||
|
||||
case class YearMonth[T](year: T, month: T)
|
||||
implicit class CartesianProd[T](val seq: Seq[T]) {
|
||||
def x(other: Seq[T]) = for(s1 <- seq; s2 <- other) yield YearMonth(year=s1,month=s2)
|
||||
def -(other: Seq[T]): Seq[T] = seq diff other
|
||||
}
|
||||
|
||||
def has5weekends(ym: { val year: Int; val month: Int}) = {
|
||||
val date = new GregorianCalendar(ym.year, ym.month-1, 1)
|
||||
date.get(DAY_OF_WEEK) == FRIDAY && date.getActualMaximum(DAY_OF_MONTH) == 31
|
||||
}
|
||||
|
||||
val expectedFirstFive = Seq(
|
||||
YearMonth(1901,3), YearMonth(1902,8), YearMonth(1903,5), YearMonth(1904,1), YearMonth(1904,7))
|
||||
val expectedFinalFive = Seq(
|
||||
YearMonth(2097,3), YearMonth(2098,8), YearMonth(2099,5), YearMonth(2100,1), YearMonth(2100,10))
|
||||
val expectedNon5erYears = Seq(1900, 1906, 1917, 1923, 1928, 1934, 1945, 1951, 1956, 1962,
|
||||
1973, 1979, 1984, 1990, 2001, 2007, 2012, 2018, 2029, 2035,
|
||||
2040, 2046, 2057, 2063, 2068, 2074, 2085, 2091, 2096)
|
||||
|
||||
"Five Weekend Algorithm" should "match specification" in {
|
||||
val months = (1900 to 2100) x (1 to 12) filter has5weekends
|
||||
months.size shouldBe 201
|
||||
months.take(5) shouldBe expectedFirstFive
|
||||
months.takeRight(5) shouldBe expectedFinalFive
|
||||
|
||||
(1900 to 2100) - months.map(_.year) shouldBe expectedNon5erYears
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue