Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 29th, 2013, 01:56 PM
 FallOutBoy
Registered User

Join Date: Jan 2013
Posts: 2
Time spent in forums: 33 m 45 sec
Reputation Power: 0
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).

#2
January 29th, 2013, 02:44 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,158
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 8 m
Reputation Power: 455
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

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!

#3
January 29th, 2013, 02:52 PM
 leeuw01
Registered User

Join Date: Dec 2012
Posts: 16
Time spent in forums: 5 h 51 m 50 sec
Reputation Power: 0
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 < A-3:
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])```

#4
January 29th, 2013, 03:12 PM
 FallOutBoy
Registered User

Join Date: Jan 2013
Posts: 2
Time spent in forums: 33 m 45 sec
Reputation Power: 0
Quote:
 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 < A-3: 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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help