Itertools#

[1]:
from itertools import *
[2]:
x = [1,2,3,4,5,6,7]
[3]:
l = []
for iter in combinations_with_replacement(x,2):
    l.append([ iter[0], iter[1], iter[0]-iter[1] ])
l
[3]:
[[1, 1, 0],
 [1, 2, -1],
 [1, 3, -2],
 [1, 4, -3],
 [1, 5, -4],
 [1, 6, -5],
 [1, 7, -6],
 [2, 2, 0],
 [2, 3, -1],
 [2, 4, -2],
 [2, 5, -3],
 [2, 6, -4],
 [2, 7, -5],
 [3, 3, 0],
 [3, 4, -1],
 [3, 5, -2],
 [3, 6, -3],
 [3, 7, -4],
 [4, 4, 0],
 [4, 5, -1],
 [4, 6, -2],
 [4, 7, -3],
 [5, 5, 0],
 [5, 6, -1],
 [5, 7, -2],
 [6, 6, 0],
 [6, 7, -1],
 [7, 7, 0]]
[4]:
l = []
for iter in combinations(x,2):
    l.append([ iter[0], iter[1], iter[0]-iter[1] ])
l
[4]:
[[1, 2, -1],
 [1, 3, -2],
 [1, 4, -3],
 [1, 5, -4],
 [1, 6, -5],
 [1, 7, -6],
 [2, 3, -1],
 [2, 4, -2],
 [2, 5, -3],
 [2, 6, -4],
 [2, 7, -5],
 [3, 4, -1],
 [3, 5, -2],
 [3, 6, -3],
 [3, 7, -4],
 [4, 5, -1],
 [4, 6, -2],
 [4, 7, -3],
 [5, 6, -1],
 [5, 7, -2],
 [6, 7, -1]]
[5]:
l = []
for iter in permutations(x,2):
    l.append([ iter[0], iter[1], iter[0]-iter[1] ])
l
[5]:
[[1, 2, -1],
 [1, 3, -2],
 [1, 4, -3],
 [1, 5, -4],
 [1, 6, -5],
 [1, 7, -6],
 [2, 1, 1],
 [2, 3, -1],
 [2, 4, -2],
 [2, 5, -3],
 [2, 6, -4],
 [2, 7, -5],
 [3, 1, 2],
 [3, 2, 1],
 [3, 4, -1],
 [3, 5, -2],
 [3, 6, -3],
 [3, 7, -4],
 [4, 1, 3],
 [4, 2, 2],
 [4, 3, 1],
 [4, 5, -1],
 [4, 6, -2],
 [4, 7, -3],
 [5, 1, 4],
 [5, 2, 3],
 [5, 3, 2],
 [5, 4, 1],
 [5, 6, -1],
 [5, 7, -2],
 [6, 1, 5],
 [6, 2, 4],
 [6, 3, 3],
 [6, 4, 2],
 [6, 5, 1],
 [6, 7, -1],
 [7, 1, 6],
 [7, 2, 5],
 [7, 3, 4],
 [7, 4, 3],
 [7, 5, 2],
 [7, 6, 1]]
[6]:
l = [1,2,3,4,5,6,7]
# l = [7,6,5,4,3,2,1]
# l = [1,2,1,2,3,2,1]
# l = [2,3,5,6,7,1,2,4,6,9,7,5]
# l = [0,0,0,1,0,0,0,0,1,0]

max = 0
start_new = True
temp_list = []
for i in range(len(l)-1):
    if start_new:
        total_sum = sum(temp_list)
        if max < total_sum:
            max = total_sum
        temp_list = [l[i]]

    if l[i] < l[i+1]:
        temp_list.append(l[i+1])
        print(temp_list)
        total_sum = sum(temp_list)
        if max < total_sum:
            max = total_sum
        start_new = False

    else:
       start_new = True

print(max)
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
28

question 2 :

You will be given a list of stock prices for a given day and your goal is to return the maximum profit that could have been made by buying a stock at the given price and then selling the stock later on. For example if the input is: [45, 24, 35, 31, 40, 38, 11] then your program should return 16 because if you bought the stock at $24 and sold it at $40, a profit of $16 was made and this is the largest profit that could be made. If no profit could have been made, return -1.

[7]:
# l = [1,2,3,4,5,6,7]
l = [45, 24, 35, 31, 40, 38, 11]
# l = [100, 180, 260, 310, 40, 535, 695]
# l = [7,6,5,4,3,2,1]
selling_combs = []
for iter in combinations(l,2):
    selling_combs.append([iter[1]-iter[0]])

selling_combs.sort(reverse=True)
print("Max profit value of the week :",selling_combs[0])

Max profit value of the week : [16]
[15]:
for i in permutations(range(1,5),2):
    print(i)
(1, 2)
(1, 3)
(1, 4)
(2, 1)
(2, 3)
(2, 4)
(3, 1)
(3, 2)
(3, 4)
(4, 1)
(4, 2)
(4, 3)