moduleDailyChallengeopenSystem.Text.RegularExpressionslettuple2Mapf(x,y)=(fx,fy)let(|Greater|Smaller|Equal|)(x,y)=ifx>ythenGreaterelifx<ythenSmallerelseEqualletevenOrOdd(nums:string):string=nums.Split('')|>Array.partition(funn->Regex.Match(n,"[02468]$").Success)|>tuple2Map(Array.sumBy(int))|>function|Greater->"Even is greater than Odd"|Smaller->"Odd is greater than Even"|Equal->"Even and Odd are the same"
Tried to have a bit of fun with this one:
Input strings are assumed to be of the form "1 2 3" etc.
To save a map call for converting the strings to numbers, I used regular expressions for the partitioning.
Implemented a little helper called tuple2Map for applying a function to both elements of a 2-tuple.
Used an active pattern for deciding which of the sums is greater.
Tests:
moduleDailyChallengeTestopenFsUnit.XunitopenXunitopenDailyChallenge[<Fact>]let``evens > odds``()=evenOrOdd"1 2"|>shouldequal"Even is greater than Odd"[<Fact>]let``evens < odds``()=evenOrOdd"1 2 3"|>shouldequal"Odd is greater than Even"[<Fact>]let``evens = odds``()=evenOrOdd"1 3 4"|>shouldequal"Even and Odd are the same"
re: Daily Challenge #81 - Even or Odd VIEW POST
FULL DISCUSSIONF#
Tried to have a bit of fun with this one:
map
call for converting the strings to numbers, I used regular expressions for the partitioning.tuple2Map
for applying a function to both elements of a 2-tuple.Tests: