-1

This is an add-on of my previous question. The question I am trying to answer is:

I have a list of integers, representing the yearly profit (in kilo euro) of n businesses (shops) along the high street in the City centre. I am given 4 (four) businesses ("for free"), that neighbor one another. The problem is to calculate which 4 I should take to maximize my yearly profit. Given these numbers, write a PYTHON program to calculate which 4 I should take. As examples: If the profits were 52, 67, -8, 43, -20 I should take the first 4 businesses. If the profits were -20, 36, -10, -30, 3, 21 I would take no business.

Second attempt:

groups =      zip(mylist,mylist[1:25,34,-2,-57],mylist[2:-8,47,28,19],mylist[3:47,-57,54,-96],mylist[4:-1,73,38,39],mylist[5:76,-49,6,-3],mylist[6:9,-7,-99,28],mylist[7:37,88,74,-3],mylist[8:23,-2,-5,78],mylist[9:38,-9,31,32],mylist[10:32,-58,66]
     Answer = max(sum(groups))

It then tells me that "Answer" is invalid syntax. I dont know how to finish it off please help?

  • Your zip function doesn't look like anything written in python. I'm not sure what did you want it to be, I don't even think you need `zip` here. – sashkello Feb 14 '14 at 03:35

2 Answers2

1

You're missing a closing ) on the first line.

U2EF1
  • 12,253
  • 3
  • 33
  • 36
  • yeah ok corrected that, it now tells me: SyntaxError: multiple statements found while compiling a single statement – user3307987 Feb 14 '14 at 03:28
1

sum sums up the list. You have a list of lists, so it doesn't really know that you want to sum those lists within the list.

You can do it through list comprehension:

summedlists = [sum(mylist) for mylist in groups]

or map:

summedlists = map(sum, groups)

Now summedlists is a list of totals. Its maximal value is

maxval = max(summedlists)

and the index of this element is

maxi = summedlists.index(maxval)

If you want indices and values of top four of your stores:

top4 = sorted(list(enumerate(summedlists)), key = lambda x: x[1])[:4]
sashkello
  • 16,318
  • 22
  • 78
  • 104
  • Nah bud it says there's a syntax error for 'summedlists' – user3307987 Feb 14 '14 at 03:37
  • There is error in your `groups`, there is no error in my code here because I just ran it :) Check how you create `groups` - it should be a list of lists. – sashkello Feb 14 '14 at 03:38
  • I think it might be an error with the start of the code, Should I open with: groups = (mylist,mylist[1:25,34,-2,-57],mylist[2:-8,47,28,19],mylist[3:47,-57,54,-96],mylist[4:-1,73,38,39],mylist[5:76,-49,6,-3],mylist[6:9,-7,-99,28],mylist[7:37,88,74,-3],mylist[8:23,-2,-5,78],mylist[9:38,-9,31,32],mylist[10:32,-58,66] – user3307987 Feb 14 '14 at 03:48
  • @user3307987 This piece of code doesn't make any sense, that's all. What is it supposed to mean? – sashkello Feb 14 '14 at 03:54
  • I'm given my list of integers and I grouped them up into groups of four (except the last group of 3 as there were 39 numbers). That way if I get the next bit of coding right I can pick which group I want to get maximum profit. – user3307987 Feb 14 '14 at 04:00
  • "I grouped them up into groups of four" - did you? did you print what you get from that piece of code? You need to get it right first. Again, that piece of code is just completely wrong. Print the outputs of each of your steps, see how it looks and then proceed. http://stackoverflow.com/questions/4998427/how-to-group-elements-in-python-by-n-elements – sashkello Feb 14 '14 at 04:07