| Bytes | Lang | Time | Link |
|---|---|---|---|
| 076 | Scala 3 | 240830T082540Z | 138 Aspe |
| 062 | JavaScript Node.js | 240828T022656Z | l4m2 |
| 136 | Python 3 | 160713T030413Z | rebellia |
| 009 | Uiua | 240827T200129Z | nyxbird |
| 006 | Japt x | 240827T181535Z | Shaggy |
| 044 | Ruby | 240827T152526Z | Jordan |
| 014 | J | 160714T162827Z | miles |
| 059 | Haskell | 160714T132849Z | Zeta |
| 124 | C++ | 160713T085839Z | Karl Nap |
| 029 | RETURN | 160313T025954Z | Mama Fun |
| 030 | Perl 6 | 160312T063227Z | Hotkeys |
| 069 | Javascript ES6 | 160312T203037Z | Mama Fun |
| 017 | Mathematica | 160312T033909Z | Calculat |
| 025 | Julia | 160312T064746Z | Alex A. |
| 006 | Seriously | 160312T063834Z | user4594 |
| 011 | CJam | 160312T151202Z | A Simmon |
| 006 | MATL | 160312T092841Z | Adnan |
| 015 | APL | 160312T065531Z | Alex A. |
| 008 | Pyth | 160312T035851Z | Maltysen |
| 006 | Jelly | 160312T050452Z | Dennis |
Scala 3, 76 bytes
(x,y)=>x.sorted(Ordering[Int].reverse).zip(y.sorted).map{case(a,b)=>a*b}.sum
JavaScript (Node.js), 62 bytes
a=>a.map(x=>x.sort((x,y)=>x-y))[i=0].map(x=>i+=a[1].pop()*x)|i
Based on Mama Fun Roll's
-4B by Shaggy by switching Input format
Python 3, 136 bytes
def mdp(n, a, b):
a = list(reversed(sorted(a)))
b = sorted(b)
res = sum([a[i] * b[i] for i in range(len(a))])
return res
J, 14 bytes
+/@(*|.)&(/:~)
Uses the same principle as the others.
Explanation
+/@(*|.)&(/:~) Input: x on LHS and y on RHS
&(/:~) Sort both x and y
|. Reverse the sorted y
* Multiply the sorted x and reversed sorted y elementwise
+/@ Reduce the products using addition and return
Haskell, 59 bytes
import Data.List
v?u=sum$zipWith(*)(sort v)$reverse$sort u
C++, 124 bytes
#include<algorithm>
int m(int*a,int*b,int n){std::sort(a,a+n);std::sort(b,b+n);int r=0;while(--n>=0)r+=a[n]**b++;return r;}
ungolfed:
#include<algorithm>
int m(int*a,int*b,int n){
std::sort(a,a+n);
std::sort(b,b+n);
int r=0;
while(--n>=0)
r+=a[n]*(*b++);
return r;
}
At first i used std::greater<int>() for the sort in b but just reversing the order in the summation is easier.
RETURN, 29 bytes
[{␆␃}\{␆}␄␅[¤¥][×␌]#}␁[¤][+]#]
Replace any ␆␃␄␇ with their unprintable counterparts.
Anonymous lambda that leaves result on stack2. Usage:
""{1 3 0 5-}""{0 2- 4 1}[{␆␃}\{␆}␄␅[¤¥][×␌]#}␁[¤][+]#]!
Explanation
[ ] lambda
{␆␃} sort and reverse first stack
\{␆} sort second stack
␄␅ transpose and flatten
[ ][ ]# while loop
¤¥ check if 2 items exist in stack
× if so, multiply top 2 items
␌ and push to stack2
}␁ switch to stack2
[¤][+]# sum stack2
Perl 6, 33 30 bytes
{sum @^a.sort Z*@^b.sort.reverse}
Javascript ES6, 69 bytes
a=>b=>a.sort((x,y)=>x-y).map((x,y)=>i+=b.sort((x,y)=>y-x)[y]*x,i=0)|i
Wow, this is way too long.
Mathematica, 30 17 bytes
-13 bytes by murphy
Sort@#.-Sort@-#2&
Function, input is vector1(list),vector2(list) Several revisions:
Plus@@(Sort@#*Reverse@Sort@#2)&(*me*)
Total[Sort@#*Reverse@Sort@#2]&
Sort@#.Reverse@Sort@#2& (*alephalpha*)
Sort@#.Sort[#2,#>#2&]& (*murphy*)
Sort@#.SortBy[#2,-#&] (*me*)
Sort@#.-Sort@-#2& (*murphy*)
Julia, 32 25 bytes
x->y->-sort(-x)⋅sort(y)
This is an anonymous function that accepts two arrays and returns an integer. To call it, assign it to a variable and do f(x)(y).
For inputs x and y, we simply compute the dot product of x sorted in reverse order with y sorted. We get x in reverse sorted order by negating all values, sorting, then negating again.
Saved 7 bytes thanks to Dennis!
Seriously, 6 bytes
,SR,S*
Explanation:
,SR,S*
,SR input first vector, sort, reverse
,S input second vector, sort
* dot product
MATL, 6 bytes
Code:
SiSP*s
My first MATL answer :)
Explanation:
S # Sort the first array
iS # Take the second array and sort it
P # Flip the array
* # Multiply both arrays with each other
s # Sum of the result
APL, 15 bytes
{+/⍺[⍒⍺]×⍵[⍋⍵]}
This is a dyadic function that accepts arrays on the left and right and returns an integer. It uses the same approach as my Julia answer: dot product of the sorted arrays, one descending and one ascending.
Jelly, 6 bytes
ṢṚ×Ṣ}S
Using brute force is equally short:
Œ!×S€Ṃ
How it works
ṢṚ×Ṣ}S Main link. Arguments: u (vector), v (vector)
Ṣ Sort the components of u.
Ṛ Reverse.
Ṣ} Sort the components of v.
× Multiply the results, element by element.
S Compute the sum of the products.