3 ms·
Ha, that's basically what I just did with the Haskell version: Prelude> import Data.List Prelude Data.List> sum . take 2 . reverse . sort $ [1..5] 9
by codygman 12y ago
Ha, that's basically what I just did with the Haskell version:
Prelude> import Data.List
Prelude Data.List> sum . take 2 . reverse . sort $ [1..5]
9
- Luyt 12y agoI can do that in Python ;-) a = [7, 9, 2, 8] print sum(sorted(a)[-2:])
- codygman 12y agoOh cool, slices don't return index errors: >>> print sum(sorted([])[-2:]) 0
- Luyt 12y agoReading the original problem, "Write a function that returns the sum of the two largest numbers in an array", I took the liberty of assuming that the input list would always contain at least two items.