Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 1.97 KB

day3.md

File metadata and controls

64 lines (49 loc) · 1.97 KB

Challenge 3 (12/04/2020)

Given a list of summable things, the maximum segment sum is highest value given by summing all the elements of a contiguous group of items. In this problem, you'll have to find the highest value given by the sum of all the elements of a non-contiguous subsequence of the original list.

Premises

  • There are no non-contiguous subsequences of a list with two or fewer elements (duh!);
  • A non-contiguous subsequence is still a sequence, but which has at least one of the constituting elements originally not contiguous to the others (left or right);
  • As long as the elements are summable, there are no additional constrains;
  • Lists are finite.

Examples

mncss [-4,-3,4] returns 0 (given by [-4,4])
mncss [-4,-3,-7,2,1,-2,-1,-4] returns 2 (given by [2,1,-1])
mncss [1,2,3,4] returns 8 (given by [1,3,4])
mncss [2,4] is an invalid input with non-specified output

Solutions

(57) Haskell by Hugo (based on Restivo)

mncss l=maximum$sum<$>(subsequences l\\(inits l>>=tails))

(58) Haskell by Restivo

mncss l=maximum$map sum(subsequences l\\(inits l>>=tails))

(58) Haskell by Hugo (based on André Silva)

mncss x=maximum[sum y|y<-subsequences x,not$isInfixOf y x]

(60) Haskell by André Silva (based on Restivo)

mncss=liftA2(\\)subsequences(tails>=>inits)>>>maximum.map sum

(62) Haskell by André Silva

mncss x=maximum$map sum[y|y<-subsequences x,not$isInfixOf y x]

(75) Julia by Ferrolho

mncss(x)=maximum(sum(x[s]) for s=subsets(1:length(x)) if any((1),diff(s)))

(125) Python by António

s=lambda l:max(l)if max(l)<0else sum([max(x,0)for x in l])
mncss=lambda l:max([s(l[:i])+s(l[i+1:])for i in range(1,len(l)-1)])

(177) Python by Mafalda

s=lambda l:[l[i:j] for i in range(len(l)+1) for j in range(i,len(l)+1)]
mncss=lambda l:max([sum(x) for x in [list(x) for i in range(len(l)+1) for x in c(l,i)] if x not in s(l)])