January 29th, 2013, 01:56 PM

Help
i've been asked to make this program and i have no idea where to even start
This lab is based on the same idea as lab 1: We have a list of integers, representing the yearly profit (in kilo euro) of n businesses (shops) along the high street in the City centre. This time we are being given 4 (four) businesses ("for free"), that neighbor one another. The problem is to calculate which 4 we should take to maximize our yearly profit.
Given these numbers, write a PYTHON program to calculate which 4 we should take. As examples:
If the profits were 52, 67, 8, 43, 20 we should take the first 4 businesses.
If the profits were 20, 36, 10, 30, 3, 21 we would take no business (this is also allowed).
January 29th, 2013, 02:44 PM

In general you want to maximize the sum of m consecutive numbers from a list of n, m <= n . Executable Iverson notation has an infix adverb that simplifies this task. 4&(+/\) means
4 \ apply the verb to length 4 infixes
/ insert the verb
+ add
Thus, insert add to prefixes of length 4.
Code:
A NB. data
52 67 _8 43 _20
4 +/\ A NB. sum of length 4 infixes
154 82
>./ 4 +/\ A NB. maximum of these sums
154
(i. >./) 4 +/\ A NB. index of maximum (starting position)
0
((] , {~) (i. >./)) 4 +/\ A NB. index and maximum
0 154
B
_20 36 _10 _30 3 21
((] , {~) (i. >./)) 4 +/\ B NB. index and maximum
1 _1
[code]
Code tags[/code] are essential for python code and Makefiles!
January 29th, 2013, 02:52 PM

Your question is not very clear to me. If I understand it correctly you need to calculate which 4 adjacent numbers give the biggest sum. If you want to do it easy, just add every value in a list, then use a to calculate every possibility.
Here's how you could do it:
Code:
List[52, 67, 8, 43, 20]
A = len(list)
x = 0
While x < A3:
B = List[x]+List[x+1]+List[x+2]+List[x+3]
If D < B:
D = B
E = X
x = x + 1
print (List[e], List[e+1], List[e+2], List[e+3])
January 29th, 2013, 03:12 PM

Originally Posted by leeuw01
Your question is not very clear to me. If I understand it correctly you need to calculate which 4 adjacent numbers give the biggest sum. If you want to do it easy, just add every value in a list, then use a to calculate every possibility.
Here's how you could do it:
Code:
List[52, 67, 8, 43, 20]
A = len(list)
x = 0
While x < A3:
B = List[x]+List[x+1]+List[x+2]+List[x+3]
If D < B:
D = B
E = X
x = x + 1
print (List[e], List[e+1], List[e+2], List[e+3])
Thanks