Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 29th, 2013, 12:56 PM
FallOutBoy FallOutBoy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 FallOutBoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).

Reply With Quote
  #2  
Old January 29th, 2013, 01:44 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
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!

Reply With Quote
  #3  
Old January 29th, 2013, 01:52 PM
leeuw01 leeuw01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 leeuw01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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])

Reply With Quote
  #4  
Old January 29th, 2013, 02:12 PM
FallOutBoy FallOutBoy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 FallOutBoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap