The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Help
Discuss Help in the Python Programming forum on Dev Shed. Help Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 29th, 2013, 12:56 PM
|
|
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).
|

January 29th, 2013, 01:44 PM
|
 |
Contributing User
|
|
|
|
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!
|

January 29th, 2013, 01:52 PM
|
|
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])
|

January 29th, 2013, 02:12 PM
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|