How to resolve the algorithm Last Friday of each month step by step in the Elm programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Last Friday of each month step by step in the Elm programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the Elm programming language
Source code in the elm programming language
import Html exposing (Html, Attribute, text, div, input)
import Html.App exposing (beginnerProgram)
import Html.Attributes exposing (placeholder, value, style)
import Html.Events exposing (onInput)
import String exposing (toInt)
import Maybe exposing (withDefault)
import List exposing (map, map2)
import List.Extra exposing (scanl1)
type Msg = SetYear String
lastFridays : Int -> List Int
lastFridays year =
let isLeap = (year % 400) == 0 || ( (year % 4) == 0 && (year % 100) /= 0 )
daysInMonth = [31, if isLeap then 29 else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
y = year-1
in scanl1 (+) daysInMonth
|> map2 (\len day -> len - (day + 2 + y + y//4 - y//100 + y//400) % 7) daysInMonth
lastFridayStrings : String -> List String
lastFridayStrings yearString =
let months= ["January ", "February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "November ", "December "]
errString = "Only years after 1752 are valid."
in case toInt yearString of
Ok year ->
if (year < 1753)
then [errString]
else lastFridays year
|> map2 (\m d -> m ++ toString d ++ ", " ++ toString year) months
Err _ ->
[errString]
view : String -> Html Msg
view yearString =
div []
([ input
[ placeholder "Enter a year."
, value yearString
, onInput SetYear
, myStyle
]
[]
] ++ (lastFridayStrings yearString
|> map (\date -> div [ myStyle ] [ text date ]) ))
myStyle : Attribute Msg
myStyle =
style
[ ("width", "100%")
, ("height", "20px")
, ("padding", "5px 0 0 5px")
, ("font-size", "1em")
, ("text-align", "left")
]
update : Msg -> String -> String
update msg _ =
case msg of
SetYear yearString -> yearString
main =
beginnerProgram
{ model = ""
, view = view
, update = update
}
You may also check:How to resolve the algorithm FizzBuzz step by step in the Perl programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the REXX programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Phix programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the XLISP programming language
You may also check:How to resolve the algorithm Tonelli-Shanks algorithm step by step in the jq programming language