Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
3
Task/Five-weekends/Ceylon/five-weekends-1.ceylon
Normal file
3
Task/Five-weekends/Ceylon/five-weekends-1.ceylon
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module rosetta.fiveweekends "1.0.0" {
|
||||
import ceylon.time "1.2.2";
|
||||
}
|
||||
55
Task/Five-weekends/Ceylon/five-weekends-2.ceylon
Normal file
55
Task/Five-weekends/Ceylon/five-weekends-2.ceylon
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import ceylon.time {
|
||||
date,
|
||||
Date
|
||||
}
|
||||
import ceylon.time.base {
|
||||
january,
|
||||
december,
|
||||
friday,
|
||||
Month
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
[Date[],Integer[]] result = fiveWeekendsRecursive();
|
||||
|
||||
value fiveWeekendFirstOfMonths = result[0];
|
||||
Integer[] yearsWithNoFiveWeekendMonths = result[1];
|
||||
|
||||
print("# five weekend months = ``fiveWeekendFirstOfMonths.size``");
|
||||
print("# years without five weekend months = ``yearsWithNoFiveWeekendMonths.size``");
|
||||
yearsWithNoFiveWeekendMonths.each(print);
|
||||
}
|
||||
|
||||
[Date[], Integer[]] fiveWeekendsRecursive()
|
||||
=> fiveWeekendsRecursiveInner{ year = 1900;
|
||||
month = january;
|
||||
fiveWeekendFirstOfMonths = [];
|
||||
yearsWithNoFiveWeekendMonths = []; };
|
||||
|
||||
[Date[], Integer[]] fiveWeekendsRecursiveInner(Integer year,
|
||||
Month month,
|
||||
Date[] fiveWeekendFirstOfMonths,
|
||||
Integer[] yearsWithNoFiveWeekendMonths) {
|
||||
if (year > 2100) {
|
||||
return [fiveWeekendFirstOfMonths,yearsWithNoFiveWeekendMonths];
|
||||
}
|
||||
|
||||
Date firstOfMonth = date{ year = year; month = month; day = 1; };
|
||||
|
||||
Boolean isFiveWeekendMonth =
|
||||
(month.numberOfDays() == 31 && friday == firstOfMonth.dayOfWeek);
|
||||
|
||||
Boolean hasNoFiveWeekends =
|
||||
month == december &&
|
||||
! isFiveWeekendMonth &&
|
||||
fiveWeekendFirstOfMonths.filter((date) => date.year == year).size == 0;
|
||||
|
||||
return fiveWeekendsRecursiveInner(if (month == december) then year+1 else year,
|
||||
if (month == december) then january else month.plusMonths(1),
|
||||
if (isFiveWeekendMonth)
|
||||
then fiveWeekendFirstOfMonths.withTrailing(firstOfMonth)
|
||||
else fiveWeekendFirstOfMonths,
|
||||
if (hasNoFiveWeekends)
|
||||
then yearsWithNoFiveWeekendMonths.withTrailing(year)
|
||||
else yearsWithNoFiveWeekendMonths);
|
||||
}
|
||||
36
Task/Five-weekends/ERRE/five-weekends.erre
Normal file
36
Task/Five-weekends/ERRE/five-weekends.erre
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
PROGRAM FIVE_WEEKENDS
|
||||
|
||||
DIM M$[12]
|
||||
|
||||
PROCEDURE MODULO(X,Y->MD)
|
||||
IF Y=0 THEN
|
||||
MD=X
|
||||
ELSE
|
||||
MD=X-Y*INT(X/Y)
|
||||
END IF
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE WD(M,D,Y->RES%)
|
||||
IF M=1 OR M=2 THEN
|
||||
M+=12
|
||||
Y-=1
|
||||
END IF
|
||||
MODULO(365*Y+INT(Y/4)-INT(Y/100)+INT(Y/400)+D+INT((153*M+8)/5),7->RES)
|
||||
RES%=RES+1.0
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
M$[]=("","JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER")
|
||||
PRINT(CHR$(12);) ! CLS
|
||||
FOR YEAR=1900 TO 2100 DO
|
||||
FOREACH MONTH IN (1,3,5,7,8,10,12) DO ! months with 31 days
|
||||
WD(MONTH,1,YEAR->RES%)
|
||||
IF RES%=6 THEN ! day #6 is Friday
|
||||
PRINT(YEAR;": ";M$[MONTH])
|
||||
CNT%=CNT%+1
|
||||
! IF CNT% MOD 20=0 THEN GET(K$) END IF ! press a key for next page
|
||||
END IF
|
||||
END FOR
|
||||
END FOR
|
||||
PRINT("Total =";CNT%)
|
||||
END PROGRAM
|
||||
61
Task/Five-weekends/FreeBASIC/five-weekends.freebasic
Normal file
61
Task/Five-weekends/FreeBASIC/five-weekends.freebasic
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
' version 23-06-2015
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function wd(m As Integer, d As Integer, y As Integer) As Integer
|
||||
' Zellerish
|
||||
' 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday
|
||||
' 4 = Thursday, 5 = Friday, 6 = Saturday
|
||||
|
||||
If m < 3 Then ' If m = 1 Or m = 2 Then
|
||||
m += 12
|
||||
y -= 1
|
||||
End If
|
||||
Return (y + (y \ 4) - (y \ 100) + (y \ 400) + d + ((153 * m + 8) \ 5)) Mod 7
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
' only months with 31 day can have five weekends
|
||||
' these months are: January, March, May, July, August, October, December
|
||||
' in nr: 1, 3, 5, 7, 8, 10, 12
|
||||
' the 1e day needs to be on a friday (= 5)
|
||||
|
||||
Dim As String month_names(1 To 12) => {"January","February","March",_
|
||||
"April","May","June","July","August",_
|
||||
"September","October","November","December"}
|
||||
Dim As Integer m, yr, total, i, j, yr_without(200)
|
||||
Dim As String answer
|
||||
|
||||
For yr = 1900 To 2100 ' Gregorian calendar
|
||||
answer = ""
|
||||
For m = 1 To 12 Step 2
|
||||
If m = 9 Then m = 8
|
||||
If wd(m , 1 , yr) = 5 Then
|
||||
answer = answer + month_names(m) + ", "
|
||||
total = total + 1
|
||||
End If
|
||||
Next
|
||||
If answer <> "" Then
|
||||
Print Using "#### | "; yr;
|
||||
Print Left(answer, Len(answer) -2) ' get rid of extra " ,"
|
||||
Else
|
||||
i = i + 1
|
||||
yr_without(i) = yr
|
||||
End If
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "nr of month for 1900 to 2100 that has five weekends";total
|
||||
Print
|
||||
Print i;" years don't have months with five weekends"
|
||||
|
||||
For j = 1 To i
|
||||
Print yr_without(j); " ";
|
||||
If j Mod 8 = 0 Then Print
|
||||
Next
|
||||
Print
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
34
Task/Five-weekends/Harbour/five-weekends.harbour
Normal file
34
Task/Five-weekends/Harbour/five-weekends.harbour
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
PROCEDURE Main()
|
||||
LOCAL y, m, d, nFound, cNames, nTot := 0, nNotFives := 0
|
||||
LOCAL aFounds := {}
|
||||
|
||||
SET DATE ANSI
|
||||
|
||||
FOR y := 1900 TO 2100
|
||||
nFound := 0 ; cNames := ""
|
||||
FOR m := 1 TO 12
|
||||
d := CtoD( hb_NtoS( y ) +"/" + hb_NtoS( m ) + "/1" )
|
||||
IF CDoW( d ) == "Friday"
|
||||
IF DaysInMonth( m ) == 31
|
||||
nFound++
|
||||
cNames += CMonth( d ) + " "
|
||||
ENDIF
|
||||
ENDIF
|
||||
NEXT
|
||||
IF nFound > 0
|
||||
AAdd( aFounds, hb_NtoS( y ) + " : " + hb_NtoS( nFound ) + " ( " + Rtrim( cNames ) + " )" )
|
||||
nTot += nFound
|
||||
ELSE
|
||||
nNotFives++
|
||||
ENDIF
|
||||
NEXT
|
||||
? "Total months with five weekends: " + hb_NtoS( nTot )
|
||||
? "(see bellow the first and last five years/months with five weekends)"
|
||||
?
|
||||
AEval( aFounds, { | e, n | Iif( n < 6, Qout( e ), NIL ) } )
|
||||
Qout("...")
|
||||
AEval( aFounds, { | e, n | Iif( n > Len(aFounds)-5, Qout( e ), NIL ) } )
|
||||
?
|
||||
? "Years with no five weekends months: " + hb_NtoS( nNotFives )
|
||||
|
||||
RETURN
|
||||
52
Task/Five-weekends/Lasso/five-weekends.lasso
Normal file
52
Task/Five-weekends/Lasso/five-weekends.lasso
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
local(
|
||||
months = array(1, 3, 5, 7, 8, 10, 12),
|
||||
fivemonths = array,
|
||||
emptyears = array,
|
||||
checkdate = date,
|
||||
countyear
|
||||
)
|
||||
|
||||
#checkdate -> day = 1
|
||||
|
||||
loop(-from = 1900, -to = 2100) => {
|
||||
|
||||
#countyear = false
|
||||
|
||||
#checkdate -> year = loop_count
|
||||
|
||||
with month in #months
|
||||
do {
|
||||
#checkdate -> month = #month
|
||||
if(#checkdate -> dayofweek == 6) => {
|
||||
#countyear = true
|
||||
#fivemonths -> insert(#checkdate -> format(`YYYY MMM`))
|
||||
}
|
||||
}
|
||||
|
||||
if(not #countyear) => {
|
||||
#emptyears -> insert(loop_count)
|
||||
}
|
||||
|
||||
}
|
||||
local(
|
||||
monthcount = #fivemonths -> size,
|
||||
output = 'Total number of months ' + #monthcount + '<br /> Starting five months '
|
||||
)
|
||||
|
||||
loop(5) => {
|
||||
#output -> append(#fivemonths -> get(loop_count) + ', ')
|
||||
}
|
||||
|
||||
#output -> append('<br /> Ending five months ')
|
||||
|
||||
loop(-from = #monthcount - 5, -to = #monthcount) => {
|
||||
#output -> append(#fivemonths -> get(loop_count) + ', ')
|
||||
}
|
||||
|
||||
#output -> append('<br /> Years with no five weekend months ' + #emptyears -> size + '<br />')
|
||||
|
||||
with year in #emptyears do {
|
||||
#output -> append(#year + ', ')
|
||||
}
|
||||
|
||||
#output
|
||||
18
Task/Five-weekends/Nim/five-weekends.nim
Normal file
18
Task/Five-weekends/Nim/five-weekends.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import times
|
||||
|
||||
const LongMonths = {mJan, mMar, mMay, mJul, mAug, mOct, mDec}
|
||||
|
||||
var timeinfo = getLocalTime getTime()
|
||||
timeinfo.monthday = 1
|
||||
|
||||
var sumNone = 0
|
||||
for year in 1900..2100:
|
||||
var none = true
|
||||
for month in LongMonths:
|
||||
timeinfo.year = year
|
||||
timeinfo.month = month
|
||||
if getLocalTime(timeInfoToTime timeinfo).weekday == dFri:
|
||||
echo month," ",year
|
||||
none = false
|
||||
if none: inc sumNone
|
||||
echo "Years without a 5 weekend month: ",sumNone
|
||||
12
Task/Five-weekends/Oforth/five-weekends.oforth
Normal file
12
Task/Five-weekends/Oforth/five-weekends.oforth
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
: fiveWeekEnd(y1, y2)
|
||||
| y m |
|
||||
|
||||
ListBuffer new
|
||||
y1 y2 for: y [
|
||||
Date.JANUARY Date.DECEMBER for: m [
|
||||
Date.DaysInMonth(y, m) 31 ==
|
||||
[ y, m, 01 ] asDate dayOfWeek Date.FRIDAY == and
|
||||
ifTrue: [ [ y, m ] over add ]
|
||||
]
|
||||
]
|
||||
dup size println dup left(5) println right(5) println ;
|
||||
30
Task/Five-weekends/Phix/five-weekends.phix
Normal file
30
Task/Five-weekends/Phix/five-weekends.phix
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
sequence m31 = {"January",0,"March",0,"May",0,"July","August",0,"October",0,"December"}
|
||||
integer y,m,
|
||||
nmonths = 0
|
||||
string months
|
||||
sequence res = {},
|
||||
none = {}
|
||||
|
||||
for y=1900 to 2100 do
|
||||
months = ""
|
||||
for m=1 to 12 do
|
||||
if string(m31[m])
|
||||
and day_of_week(y,m,1)=6 then
|
||||
if length(months)!=0 then months &= ", " end if
|
||||
months &= m31[m]
|
||||
nmonths += 1
|
||||
end if
|
||||
end for
|
||||
if length(months)=0 then
|
||||
none = append(none,y)
|
||||
else
|
||||
res = append(res,sprintf("%d : %s\n",{y,months}))
|
||||
end if
|
||||
end for
|
||||
|
||||
printf(1,"Found %d months with five full weekends\n",nmonths)
|
||||
res[6..-6] = {" ...\n"}
|
||||
puts(1,join(res,""))
|
||||
printf(1,"Found %d years with no month having 5 weekends:\n",{length(none)})
|
||||
none[6..-6] = {".."}
|
||||
?none
|
||||
18
Task/Five-weekends/Ring/five-weekends.ring
Normal file
18
Task/Five-weekends/Ring/five-weekends.ring
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
sum = 0
|
||||
month = list(12)
|
||||
mo = [4,0,0,3,5,1,3,6,2,4,0,2]
|
||||
mon = [31,28,31,30,31,30,31,31,30,31,30,31]
|
||||
mont = ["January","February","March","April","May","June",
|
||||
"July","August","September","October","November","December"]
|
||||
for year = 1900 to 2100
|
||||
if year < 2100 leap = year - 1900 else leap = year - 1904 ok
|
||||
m = ((year-1900)%7) + floor(leap/4) % 7
|
||||
oldsum = sum
|
||||
for n = 1 to 12
|
||||
month[n] = (mo[n] + m) % 7
|
||||
x = (month[n] + 1) % 7
|
||||
if x = 2 and mon[n] = 31 sum += 1 see "" + year + "-" + mont[n] + nl ok
|
||||
next
|
||||
if sum = oldsum see "" + year + "-" + "(none)" + nl ok
|
||||
next
|
||||
see "Total : " + sum + nl
|
||||
34
Task/Five-weekends/Sidef/five-weekends.sidef
Normal file
34
Task/Five-weekends/Sidef/five-weekends.sidef
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
require('DateTime');
|
||||
|
||||
var happymonths = [];
|
||||
var workhardyears = [];
|
||||
var longmonths = [1, 3, 5, 7, 8, 10, 12];
|
||||
|
||||
range(1900, 2100).each { |year|
|
||||
var countmonths = 0;
|
||||
longmonths.each { |month|
|
||||
var dt = %s'DateTime'.new(
|
||||
year => year,
|
||||
month => month,
|
||||
day => 1
|
||||
);
|
||||
|
||||
if (dt.day_of_week == 5) {
|
||||
countmonths++;
|
||||
var yearfound = dt.year;
|
||||
var monthfound = dt.month_name;
|
||||
happymonths.append(join(" ", yearfound, monthfound));
|
||||
}
|
||||
}
|
||||
|
||||
if (countmonths == 0) {
|
||||
workhardyears.append(year);
|
||||
}
|
||||
}
|
||||
|
||||
say "There are #{happymonths.len} months with 5 full weekends!";
|
||||
say "The first 5 and the last 5 of them are:";
|
||||
say happymonths.first(5).join("\n");
|
||||
say happymonths.last(5).join("\n");
|
||||
say "No long weekends in the following #{workhardyears.len} years:";
|
||||
say workhardyears.join(",");
|
||||
15
Task/Five-weekends/jq/five-weekends-1.jq
Normal file
15
Task/Five-weekends/jq/five-weekends-1.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Use Zeller's Congruence to determine the day of the week, given
|
||||
# year, month and day as integers in the conventional way.
|
||||
# Emit 0 for Saturday, 1 for Sunday, etc.
|
||||
#
|
||||
def day_of_week(year; month; day):
|
||||
if month == 1 or month == 2 then
|
||||
[month + 12, year - 1]
|
||||
else
|
||||
[month, year]
|
||||
end
|
||||
| day + (13*(.[0] + 1)/5|floor)
|
||||
+ (.[1]%100) + ((.[1]%100)/4|floor)
|
||||
+ (.[1]/400|floor) - 2*(.[1]/100|floor)
|
||||
| . % 7
|
||||
;
|
||||
25
Task/Five-weekends/jq/five-weekends-2.jq
Normal file
25
Task/Five-weekends/jq/five-weekends-2.jq
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
def weekday_of_last_day_of_month(year; month):
|
||||
def day_before(day): (6+day) % 7;
|
||||
|
||||
if month==12 then day_before( day_of_week(year+1; 1; 1) )
|
||||
else day_before( day_of_week( year; month+1; 1 ) )
|
||||
end
|
||||
;
|
||||
|
||||
# The only case where the month has 5 weekends is when the last day
|
||||
# of the month falls on a Sunday and the month has 31 days.
|
||||
#
|
||||
def five_weekends(from; to):
|
||||
reduce range(from; to) as $year
|
||||
([]; reduce (1,3,5,7,8,10,12) as $month # months with 31 days
|
||||
(.;
|
||||
weekday_of_last_day_of_month($year; $month) as $day
|
||||
| if $day == 1 then . + [[ $year, $month]] else . end ))
|
||||
;
|
||||
|
||||
# Input [year, month] as conventional integers; print e.g. "Jan 2001"
|
||||
def pp:
|
||||
def month:
|
||||
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][.-1];
|
||||
"\(.[1] | month) \(.[0])"
|
||||
;
|
||||
7
Task/Five-weekends/jq/five-weekends-3.jq
Normal file
7
Task/Five-weekends/jq/five-weekends-3.jq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
five_weekends(1900;2101)
|
||||
| "There are \(length) months with 5 weekends from 1900 to 2100 inclusive;",
|
||||
"the first and last five are as follows:",
|
||||
( .[0: 5][] | pp),
|
||||
"...",
|
||||
( .[length-5: ][] | pp),
|
||||
"In this period, there are \( [range(1900;2101)] - map( .[0] ) | length ) years which have no five-weekend months."
|
||||
15
Task/Five-weekends/jq/five-weekends-4.jq
Normal file
15
Task/Five-weekends/jq/five-weekends-4.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
$ jq -r -n -f Five_Weekends.jq
|
||||
There are 201 months with 5 weekends from 1900 to 2100 inclusive;
|
||||
the first and last five are as follows:
|
||||
Mar 1901
|
||||
Aug 1902
|
||||
May 1903
|
||||
Jan 1904
|
||||
Jul 1904
|
||||
...
|
||||
Mar 2097
|
||||
Aug 2098
|
||||
May 2099
|
||||
Jan 2100
|
||||
Oct 2100
|
||||
In this period, there are 29 years which have no five-weekend months.
|
||||
Loading…
Add table
Add a link
Reference in a new issue