Compare array of decimal sum of Integers and exact fractional part

-1

Develop a program which takes two arrays of decimal numbers, and compare the sum of whole numbers only and the decimal part. If the sums of the whole numbers are the same, and the decimal parts of Array a are a subset of the decimal parts of Array b, return True. Otherwise, return False.

Example-1 :-

Array a ={2.5,3.0,1.3}

Array b = {5.0,1.0,0.5,0.3}

Sum of whole numbers: Array a = 2+3+1 = 6 and Array b = 5+1 =6 --Matched

Individual fraction part: Array a = 2.5 = 0.5 and Array b = 0.5--Matched

Individual fraction part: Array a = 1.3 = 0.3 and Array b = 0.3--Matched

All the above are matched, so it returns true.

Example-2 :-

Array a ={1.7,2.3}

Array b = {1.2,0.5,2.3}

Sum of the whole numbers: Array a = 2+1 = 3 and Array b = 2+1 =3 --Matched

Individual fraction part: Array a = 1.7 = 0.7 and Array b = 0.2 or .5 or .3 --No Exact match

Individual fraction part: Array a = 2.3 = 0.3 and Array b = 2.3 = 0.3 -Matched

One of the conditions is not matched, so it returns false.

andyra42

Posted 2015-04-06T14:44:21.240

Reputation: 25

1What is the winning criterion? Is it code golf? – bcsb1001 – 2015-04-06T14:56:05.093

Yes its code-golf forgot to add it – andyra42 – 2015-04-06T15:10:15.273

Well, add the [tag:code-golf] tag then. – bcsb1001 – 2015-04-06T15:12:11.497

I'm a little confused, can you clarify what you mean? – ASCIIThenANSI – 2015-04-06T15:30:01.003

@ASCIIThenANSI sum(floor(a))==sum(floor(b)) and filter_nonzeros(fraction_part(a))==some_permutation(filter_nonzeros(fraction_part(b))) has to be true if I'm correct. @andyra42 Is this correct? – randomra – 2015-04-06T15:40:28.393

@randomra OK, I interpreted it a little further, and I think it means 'return true if the sum of just the whole numbers and the sum of the decimal parts in a are the same as b. I'm suggesting this as an edit, to clarify. – ASCIIThenANSI – 2015-04-06T15:47:34.060

@randomra My edit is found here.

– ASCIIThenANSI – 2015-04-06T15:53:05.860

@ASCIIThenANSI I don't think you're supposed to sum the decimal parts. I think the sum of the integer parts needs to be equal, and the set of fractional parts of a needs to be a subset of the fractional parts of b. – orlp – 2015-04-06T16:17:36.643

@orip No, I said that, but it needs to be cleaned up a little. On the bright side, we have our first answer. – ASCIIThenANSI – 2015-04-06T16:44:32.977

Please use the sandbox for proposed challenges before posting here: http://meta.codegolf.stackexchange.com/questions/2140/sandbox-for-proposed-challenges

– mbomb007 – 2015-04-06T19:31:37.873

Answers

1

Mathematica 88 81

This should be easy to beat. I was surprised that Rationalize was needed.

r=Rationalize;f=Floor;
(Tr[f@#]==Tr[f@#2] && Complement[r@Mod[#2,1],r@Mod[#, 1]]=={})&

(Tr[f@#]==Tr[f@#2] && Complement[r@Mod[#2,1],r@Mod[#, 1]]=={})& 
@@ {{2.5, 3.0, 1.3}, {5.0, 1.0,0.5, 0.3}}

True


(Tr[f@#]==Tr[f@#2] && Complement[r@Mod[#2,1],r@Mod[#, 1]]=={})& @@ 
{{1.7, 2.3}, {1.2, 0.5, 2.3}}

False

DavidC

Posted 2015-04-06T14:44:21.240

Reputation: 24 524

You can shorten the FractionalPart by using Mod[n,1]. – LegionMammal978 – 2015-04-06T16:42:39.270

@LegionMammal978 You're right! Thanks. – DavidC – 2015-04-06T17:05:02.677

I think you can save some bytes with Infix and Prefix syntax: (Tr@f@#==Tr@f@#2&&r@Mod[#2,1]~Complement~r@Mod[#,1]=={})& – numbermaniac – 2017-07-18T09:46:45.270