Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Five-weekends/Erlang/five-weekends-1.erl
Normal file
29
Task/Five-weekends/Erlang/five-weekends-1.erl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env escript
|
||||
%%
|
||||
%% Calculate number of months with five weekends between years 1900-2100
|
||||
%%
|
||||
main(_) ->
|
||||
Years = [ [{Y,M} || M <- lists:seq(1,12)] || Y <- lists:seq(1900,2100) ],
|
||||
{CountedYears, {Has5W, TotM5W}} = lists:mapfoldl(
|
||||
fun(Months, {Has5W, Tot}) ->
|
||||
WithFive = [M || M <- Months, has_five(M)],
|
||||
CountM5W = length(WithFive),
|
||||
{{Months,CountM5W}, {Has5W++WithFive, Tot+CountM5W}}
|
||||
end, {[], 0}, Years),
|
||||
io:format("There are ~p months with five full weekends.~n"
|
||||
"Showing top and bottom 5:~n",
|
||||
[TotM5W]),
|
||||
lists:map(fun({Y,M}) -> io:format("~p-~p~n", [Y,M]) end,
|
||||
lists:sublist(Has5W,1,5) ++ lists:nthtail(TotM5W-5, Has5W)),
|
||||
No5W = [Y || {[{Y,_M}|_], 0} <- CountedYears],
|
||||
io:format("The following ~p years do NOT have any five-weekend months:~n",
|
||||
[length(No5W)]),
|
||||
lists:map(fun(Y) -> io:format("~p~n", [Y]) end, No5W).
|
||||
|
||||
has_five({Year, Month}) ->
|
||||
has_five({Year, Month}, calendar:last_day_of_the_month(Year, Month)).
|
||||
|
||||
has_five({Year, Month}, Days) when Days =:= 31 ->
|
||||
calendar:day_of_the_week({Year, Month, 1}) =:= 5;
|
||||
has_five({_Year, _Month}, _DaysNot31) ->
|
||||
false.
|
||||
51
Task/Five-weekends/Erlang/five-weekends-2.erl
Normal file
51
Task/Five-weekends/Erlang/five-weekends-2.erl
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
-module(five_weekends).
|
||||
|
||||
-export([report/0, print_5w_month/1, print_year_with_no_5w_month/1]).
|
||||
|
||||
report() ->
|
||||
Years = make_nested_period_list(1900, 2100),
|
||||
{CountedYears, {All5WMonths, CountOf5WMonths}} = lists:mapfoldl(
|
||||
fun(SingleYearSublist, {All5WMonths, CountOf5WMonths}) ->
|
||||
MonthsWith5W = [Month || Month <- SingleYearSublist, if_has_5w(Month)],
|
||||
CountOf5WMonthsFor1Year = length(MonthsWith5W),
|
||||
{ % Result of map for this year sublist:
|
||||
{SingleYearSublist,CountOf5WMonthsFor1Year},
|
||||
% Accumulate total result for our fold:
|
||||
{All5WMonths ++ MonthsWith5W, CountOf5WMonths + CountOf5WMonthsFor1Year}
|
||||
}
|
||||
end, {[], 0}, Years),
|
||||
io:format("There are ~p months with five full weekends.~n"
|
||||
"Showing top and bottom 5:~n",
|
||||
[CountOf5WMonths]),
|
||||
lists:map(fun print_5w_month/1, take_nth_first_and_last(5, All5WMonths)),
|
||||
YearsWithout5WMonths = find_years_without_5w_months(CountedYears),
|
||||
io:format("The following ~p years do NOT have any five-weekend months:~n",
|
||||
[length(YearsWithout5WMonths)]),
|
||||
lists:map(fun print_year_with_no_5w_month/1, YearsWithout5WMonths).
|
||||
|
||||
make_nested_period_list(FromYear, ToYear) ->
|
||||
[ make_monthtuple_sublist_for_year(Year) || Year <- lists:seq(FromYear, ToYear) ].
|
||||
|
||||
make_monthtuple_sublist_for_year(Year) ->
|
||||
[ {Year, Month} || Month <- lists:seq(1,12) ].
|
||||
|
||||
if_has_5w({Year, Month}) ->
|
||||
if_has_5w({Year, Month}, calendar:last_day_of_the_month(Year, Month)).
|
||||
|
||||
if_has_5w({Year, Month}, Days) when Days =:= 31 ->
|
||||
calendar:day_of_the_week({Year, Month, 1}) =:= 5;
|
||||
if_has_5w({_Year, _Month}, _DaysNot31) ->
|
||||
false.
|
||||
|
||||
print_5w_month({Year, Month}) ->
|
||||
io:format("~p-~p~n", [Year, Month]).
|
||||
|
||||
print_year_with_no_5w_month(Year) ->
|
||||
io:format("~p~n", [Year]).
|
||||
|
||||
take_nth_first_and_last(N, List) ->
|
||||
Len = length(List),
|
||||
lists:sublist(List, 1, N) ++ lists:nthtail(Len - N, List).
|
||||
|
||||
find_years_without_5w_months(List) ->
|
||||
[Y || {[{Y,_M}|_], 0} <- List].
|
||||
Loading…
Add table
Add a link
Reference in a new issue