-1
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ What is the winning criterion? Is it code golf? \$\endgroup\$ – bcsb1001 Apr 6 '15 at 14:56
  • \$\begingroup\$ Yes its code-golf forgot to add it \$\endgroup\$ – andyra42 Apr 6 '15 at 15:10
  • \$\begingroup\$ Well, add the code-golf tag then. \$\endgroup\$ – bcsb1001 Apr 6 '15 at 15:12
  • \$\begingroup\$ I'm a little confused, can you clarify what you mean? \$\endgroup\$ – ASCIIThenANSI Apr 6 '15 at 15:30
  • \$\begingroup\$ @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? \$\endgroup\$ – randomra Apr 6 '15 at 15:40
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ You can shorten the FractionalPart by using Mod[n,1]. \$\endgroup\$ – LegionMammal978 Apr 6 '15 at 16:42
  • \$\begingroup\$ @LegionMammal978 You're right! Thanks. \$\endgroup\$ – DavidC Apr 6 '15 at 17:05
  • \$\begingroup\$ 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]=={})& \$\endgroup\$ – numbermaniac Jul 18 '17 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.