Thread: Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep 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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    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!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep 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])
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo