Since I'm not entirely sure what a string of numbers means, I just assumed the function gets a list of integers.

importData.List(partition)fight::[Int]->Stringfight=answer.add.partitionevenwhereadd(evens,odds)=(sumevens,sumodds)answer(evens,odds)|evens==odds="Even and Odd are the same"|evens>odds="Even is greater than Odd"|otherwise="Odd is greater than Even"

Sure! Let's start at partition, since we pass its output to add. Partition takes a predicate function and returns a tuple ([Int], [Int]) with the items of the list for which the predicate is true in the first position and the items for which the predicate is false in the second position. In this case, in the first position we'll have all the even numbers and in the second all the odd numbers, since we're using even as predicate.

Then add receives this tuple, which I destructure for convenience, and applies the function sum to both elements, which sums all the items in each list.

And the function signature for add would look like this.

add::([Int],[Int])->(Int,Int)

Lastly, answer takes the output of add and checks the conditions in the guards (the | <condition> thing) and returns the string for the first condition that matches or the string after otherwise if none matches.

Rather than keeping two sums, we can negate one of the options (I chose to negate the evens) and compare the sum to 0:

usestd::cmp::Ordering;fncompare_even_to_odd(numbers:implIterator<Item=i64>)->String{letcomparison=numbers.map(|number|ifnumber%2==0{-number}else{number}).sum::<i64>().cmp(&0);matchcomparison{Ordering::Less=>"Even is greater than Odd".to_owned(),Ordering::Equal=>"Even and Odd are the same".to_owned(),Ordering::Greater=>"Odd is greater than Even".to_owned(),}}fnmain(){println!("{}",compare_even_to_odd((&[1,3,4]).iter().map(|a|(*a))));}

constevenVsOdd=str=>{constevens=str.split('').filter(x=>x%2==0).reduce((a,b)=>a+parseInt(b),0)constodds=str.split('').filter(x=>x%2!=0).reduce((a,b)=>a+parseInt(b),0)returnevens>odds?"Even is greater than Odd":evens==odds?"Even and Odd are the same":"Odd is greater than Even"}

defmoduleDay81dorequireInteger@speceven_or_odd(String.t())::String.t()defeven_or_odd(string)do{even,odd}=string|>String.graphemes()|>Enum.map(&String.to_integer/1)|>Enum.split_with(&Integer.is_even/1){even,odd}={Enum.sum(even),Enum.sum(odd)}conddoeven>odd->"Even is greater than Odd"odd>even->"Odd is greater than Even"true->"Even and Odd are the same"endendend

moduleEvenOddexposing(evenOdd)compareEvensOdds:(Int,Int)->StringcompareEvensOdds(evens,odds)=casecompareevensoddsofGT->"Even is greater than Odd"LT->"Odd is greater than Even"_->"Even and Odd are the same"evenOdd:String->StringevenOdd=String.split"">>List.map(String.toInt>>Maybe.withDefault0)>>List.partition(modBy2>>(==)0)>>Tuple.mapBothList.sumList.sum>>compareEvensOdds

Tests

moduleEvenOddTestexposing(suite)importEvenOddexposing(evenOdd)importExpectexposing(equal)importTestexposing(Test,describe,test)suite:Testsuite=describe"Even or odd"[test"Evens should be greater than odds with 12"<|\_->evenOdd"12"|>equal"Even is greater than Odd",test"Odds should be greater than evens with 123"<|\_->evenOdd"123"|>equal"Odd is greater than Even",test"Evens & odds should be equal with 112"<|\_->evenOdd"112"|>equal"Even and Odd are the same"]

nums = str(input())
numi = [int(n) for n in nums]
odd = [n for n in numi if n%2 == 1]
if sum(odd) > int(sum(numi)/2):
print("Odd is greater than Even")
elif sum(odd) < int(sum(numi)/2):
print("Even is greater than Odd")
else:
print("Even and Odd are the same")

packagemainimport"fmt"funcevenOrOdd(numbers[10]int){evenSum:=0oddSum:=0fori:=0;i<len(numbers);i++{check:=numbers[i]%2ifcheck==0{evenSum+=evenSum+numbers[i]}else{oddSum+=oddSum+numbers[i]}}ifevenSum>oddSum{fmt.Println("Even is greater than Odd")}elseifevenSum<oddSum{fmt.Println("Odd is greater than Even")}elseifevenSum==oddSum{fmt.Println("Even and Odd are the same")}}funcmain(){numbers:=[10]int{1,2,3,4,5,6,7,8,9,10}evenOrOdd(numbers)}

importFoundation/*
evenOrOdds takes a list of numbers and outputs which total is greater, all odds, or all evens.
@param numList: [Int] list of integers of any length
@return String describing which value is greater or even they are equal.
*/funcevenOrOdds(_numList:[Int])->String{letevenWin="Even is greater than Odd"letoddWin="Odd is greater than Even"letdraw="Even and Odd are the same"varaccum=0fornuminnumList{ifnum%2==0{accum+=num}else{accum-=num}}ifaccum>0{returnevenWin}elseifaccum<0{returnoddWin}returndraw}// utility function to generate a list of IntsfuncgenRandoms(){varrandomGen=SystemRandomNumberGenerator.init()varrandomList=[Int](repeating:1,count:100)forindexin0..<randomList.count{randomList[index]=Int(bitPattern:randomGen.next(upperBound:UInt(bitPattern:500)))}print(randomList)}letexample1=[104,258,303,66,310,363,41,180,299,208,355,135,234,214,243,350,494,408,37,438,335,101,489,449,44,136,285,408,432,350,343,271,9,286,304,451,26,388,303,259,212,434,241,20,199,201,55,318,110,48,135,359,345,277,424,23,249,348,419,301,409,316,163,81,319,440,466,185,57,102,230,446,454,369,21,284,7,161,136,113,471,2,217,350,361,334,437,421,29,290,123,326,360,142,330,354,325,140,312,116]letexample2=[407,461,144,458,246,103,5,241,166,75,300,215,430,424,325,283,81,420,405,485,266,23,71,42,290,189,364,382,368,203,89,317,26,465,94,84,133,223,204,189,430,400,273,476,222,246,455,137,258,470,428,242,173,285,428,147,105,306,211,489,383,414,93,433,257,449,485,183,160,302,307,227,5,361,188,18,356,442,16,409,309,152,94,319,264,147,247,359,25,364,58,144,218,16,401,62,391,110,399,24]letexample3=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]// used to generate a draw data set, but just explicitly defined after copying the output//var example3 = [Int](repeating: 1, count: 50)//example3.append(contentsOf: [Int](repeating: 2, count: 25))print(evenOrOdds(example1),"")print(evenOrOdds(example2),"")print(evenOrOdds(example3),"\n")

Outputs:

Even is greater than Odd
Odd is greater than Even
Even and Odd are the same
Program ended with exit code: 0

Went with accumulating by adding evens and subtracting odds so in a case where the list of values being summed up ends up greater than Int.max you don't get a runtime error...... not that these test cases are likely to total greater than 9223372036854775807.... haha

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"

constevenOrOdd=(str)=>{constevenCount=(`${str}`.match(/[02468]/g)||'').length,oddCount=(`${str}`.match(/[13579]/g)||'').length;return(evenCount===oddCount)?'Even and Odd are the same':(evenCount>oddCount)?'Even is greater than Odd':'Odd is greater than Even';};

uncsolution(_numbers:[Int])->String{varodds=0varevens=0fornumberinnumbers{if(number%2==0){evens+=number}else{odds+=number}}if(evens>odds){return"Even is greater than Odd"}elseif(evens<odds){return"Odd is greater than Even"}return"Even and Odd are the same"}

D version, assuming that the string of numbers is a string with integers separated by spaces:

importstd.stdio:writeln;importstd.array:split;importstd.conv:to;importstd.algorithm.iteration:map,sum;stringcompareEvenOdd(stringnumbers){autoresult=numbers.split().map!(a=>to!int(a)).map!(a=>a%2==0?a:-a).sum();returnresult<0?"Odd is greater than Even":(result>0?"Even is greater than Odd":"Even and Odd are the same");}voidmain(){compareEvenOdd("1 2 3 4 5 6 7 8 9").writeln();compareEvenOdd("1 3 6 98 35").writeln();compareEvenOdd("1 1 2 1 1 2 1 1 2").writeln();}

Output:

Odd is greater than Even
Even is greater than Odd
Even and Odd are the same

stringevenOrOdd(stringdigitsOnly){varevens=digitsOnly.Count(c=>Char.GetNumericValue(c)%2==0);varodds=digitsOnly.Length-evens;returnevens>odds?"Even is greater than Odd":evens<odds?"Odd is greater than Even":"Even and Odd are the same";}

I just checked on Codewars the challenge, and the template for the JavaScript language is the following:

functionevenOrOdd(str){}

and the tests are

// node 8.x.xTest.assertEquals(evenOrOdd('12'),'Even is greater than Odd');Test.assertEquals(evenOrOdd('123'),'Odd is greater than Even');Test.assertEquals(evenOrOdd('112'),'Even and Odd are the same');

If it can help you understand a little bit more the challenge (yes, the instructions in this article were confusing).

Log in to continue

We're a place where coders share, stay up-to-date and grow their careers.

## Haskell

Since I'm not entirely sure what

a string of numbersmeans, I just assumed the function gets a list of integers.Could you explain what

`add (evens, odd)`

is (data type) and what does this piece do please?Sure! Let's start at

`partition`

, since we pass its output to`add`

. Partition takes a predicate function and returns a tuple`([Int], [Int])`

with the items of the list for which the predicate is true in the first position and the items for which the predicate is false in the second position. In this case, in the first position we'll have all the even numbers and in the second all the odd numbers, since we're using`even`

as predicate.Then

`add`

receives this tuple, which I destructure for convenience, and applies the function`sum`

to both elements, which sums all the items in each list.And the function signature for

`add`

would look like this.Lastly,

`answer`

takes the output of`add`

and checks the conditions in the guards (the`| <condition>`

thing) and returns the string for the first condition that matches or the string after`otherwise`

if none matches.Awesome, thanks for your answer! I didn't know the

`add`

function could take a tuple. Good to know!RustRather than keeping two sums, we can negate one of the options (I chose to negate the evens) and compare the sum to 0:

Some JavaScript

Elixir:

I read

`graphmemes`

instead of`graphemes`

, I should probably stop browsing the internet for today...Graph memes would be too... Edgy.

Sorry :)

ElmTestsGolangI assumed

string of numberswas an array.A Swift solution:

Outputs:

Went with accumulating by adding evens and subtracting odds so in a case where the list of values being summed up ends up greater than Int.max you don't get a runtime error...... not that these test cases are likely to total greater than 9223372036854775807.... haha

F#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:

My solution in js

A

Swiftsolution:D version, assuming that the string of numbers is a string with integers separated by spaces:

Output:

c#, using linq, input validations omitted:

I just checked on Codewars the challenge, and the template for the JavaScript language is the following:

and the tests are

If it can help you understand a little bit more the challenge (yes, the instructions in this article were confusing).