``````

In [16]:

def flatten(a):
if a == []:
return a

if isinstance(a[0], list):
return flatten(a[0]) + flatten(a[1:])

return [a[0]] + flatten(a[1:])

print(flatten([1, 2, 3]))
print(flatten([[[1, 2, 3], [4, 5, 6], [7], [8, 9], 10]]))

``````
``````

[1, 2, 3]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

``````
``````

In [25]:

def identical(a, b):
for i in range(len(a)+1):
if a == b:
return True
a = a[1:]+[a[0]]

return False

print(identical([10, 10, 0, 10], [10, 10, 10, 0]))
print(identical([10, 10, 5, 10], [10, 10, 10, 0]))

``````
``````

True
False

``````
``````

In [36]:

def chunks(l, size):
result = []
pos = 0

while pos+size <= len(l):
result.append(l[pos:pos+size])
pos += size

if (pos < len(l)):
result.append(l[pos:])

return result

print(chunks([1,2,3,5,1,3,4,12,3,5,3,2,3,4,5], 6))
print(chunks([1,2,3,5,1,3,4,12,3,5,3,2], 6))

``````
``````

[[1, 2, 3, 5, 1, 3], [4, 12, 3, 5, 3, 2], [3, 4, 5]]
[[1, 2, 3, 5, 1, 3], [4, 12, 3, 5, 3, 2]]

``````
``````

In [39]:

def sortbyfloat(a):
return sorted(a, key=lambda x: -x[1])

print(sortbyfloat([('item', 12.20), ('item2', 15.10), ('item3', 24.5)]))

``````
``````

[('item3', 24.5), ('item2', 15.1), ('item', 12.2)]

``````
``````

In [46]:

return tuple([sum(t) for t in zip(t1, t2)])

``````
``````

(2, 4, 6)
(2, 1, 4)

``````
``````

In [54]:

def myfun(elem):
return elem**2

def applyfunondict(a, myfun):
result = {}
for it in a:
if (isinstance(a[it], dict)):
result[it] = applyfunondict(a[it], myfun)
else:
result[it] = myfun(a[it])
return result

x = {'apple': 6, 'banana': {'hello': 5, 'world': 4}}
print(x)
print(applyfunondict(x, myfun))

``````
``````

{'apple': 6, 'banana': {'hello': 5, 'world': 4}}
{'apple': 36, 'banana': {'hello': 25, 'world': 16}}

``````
``````

In [55]:

def comparedicts(dict1, dict2):
for it in dict1:
if it not in dict2 or dict1[it] != dict2[it]:
return False
return True

print(comparedicts({'a':1, 'b':2}, {'a':1, 'b':2}))
print(comparedicts({'a':1, 'b':3}, {'a':1, 'b':2}))

``````
``````

True
False

``````
``````

In [63]:

def differences(dict1, dict2):
result = set()
for it in dict1:
if dict1[it] != dict2[it]:
return result

print(differences({'a':1, 'b':3}, {'a':1, 'b':2}))
print(differences({'a':1, 'b':3}, {'a':1, 'b':3}))

``````
``````

{('b', 3), ('b', 2)}
set()

``````
``````

In [106]:

# sales = 2500 - 80 * price
# profit = sales * price - 8000

def profitTable(maxPrice):
print("{}   {}   {}".format("Price", "Income", "Profit"))
print("{}   {}   {}".format("-"*5, "-"*6, "-"*6))

for price in range(1, maxPrice+1):
sales = 2500 - 80 * price
income = sales * price
print(" £ {:<3}   {:<5}   {:<5}".format(price, income, income-8000))

profitTable(30)

``````
``````

Price   Income   Profit
-----   ------   ------
£ 1     2420    -5580
£ 2     4680    -3320
£ 3     6780    -1220
£ 4     8720    720
£ 5     10500   2500
£ 6     12120   4120
£ 7     13580   5580
£ 8     14880   6880
£ 9     16020   8020
£ 10    17000   9000
£ 11    17820   9820
£ 12    18480   10480
£ 13    18980   10980
£ 14    19320   11320
£ 15    19500   11500
£ 16    19520   11520
£ 17    19380   11380
£ 18    19080   11080
£ 19    18620   10620
£ 20    18000   10000
£ 21    17220   9220
£ 22    16280   8280
£ 23    15180   7180
£ 24    13920   5920
£ 25    12500   4500
£ 26    10920   2920
£ 27    9180    1180
£ 28    7280    -720
£ 29    5220    -2780
£ 30    3000    -5000

``````
``````

In [113]:

# sales = 2500 - 80 * price
# profit = sales * price - 8000

def profitTable_50(maxPrice):
print("{}     {}     {}".format("Price", "Income", "Profit"))
print("{}     {}     {}".format("-"*5, "-"*6, "-"*6))

for price in [x/2.0 for x in range(1, (maxPrice)*2+1)]:
sales = 2500 - 80 * price
income = sales * price
print("£ {:<5}   {:<7}   {:<7}".format(price, income, income-8000))

profitTable_50(30)

``````
``````

Price     Income     Profit
-----     ------     ------
£ 0.5     1230.0    -6770.0
£ 1.0     2420.0    -5580.0
£ 1.5     3570.0    -4430.0
£ 2.0     4680.0    -3320.0
£ 2.5     5750.0    -2250.0
£ 3.0     6780.0    -1220.0
£ 3.5     7770.0    -230.0
£ 4.0     8720.0    720.0
£ 4.5     9630.0    1630.0
£ 5.0     10500.0   2500.0
£ 5.5     11330.0   3330.0
£ 6.0     12120.0   4120.0
£ 6.5     12870.0   4870.0
£ 7.0     13580.0   5580.0
£ 7.5     14250.0   6250.0
£ 8.0     14880.0   6880.0
£ 8.5     15470.0   7470.0
£ 9.0     16020.0   8020.0
£ 9.5     16530.0   8530.0
£ 10.0    17000.0   9000.0
£ 10.5    17430.0   9430.0
£ 11.0    17820.0   9820.0
£ 11.5    18170.0   10170.0
£ 12.0    18480.0   10480.0
£ 12.5    18750.0   10750.0
£ 13.0    18980.0   10980.0
£ 13.5    19170.0   11170.0
£ 14.0    19320.0   11320.0
£ 14.5    19430.0   11430.0
£ 15.0    19500.0   11500.0
£ 15.5    19530.0   11530.0
£ 16.0    19520.0   11520.0
£ 16.5    19470.0   11470.0
£ 17.0    19380.0   11380.0
£ 17.5    19250.0   11250.0
£ 18.0    19080.0   11080.0
£ 18.5    18870.0   10870.0
£ 19.0    18620.0   10620.0
£ 19.5    18330.0   10330.0
£ 20.0    18000.0   10000.0
£ 20.5    17630.0   9630.0
£ 21.0    17220.0   9220.0
£ 21.5    16770.0   8770.0
£ 22.0    16280.0   8280.0
£ 22.5    15750.0   7750.0
£ 23.0    15180.0   7180.0
£ 23.5    14570.0   6570.0
£ 24.0    13920.0   5920.0
£ 24.5    13230.0   5230.0
£ 25.0    12500.0   4500.0
£ 25.5    11730.0   3730.0
£ 26.0    10920.0   2920.0
£ 26.5    10070.0   2070.0
£ 27.0    9180.0    1180.0
£ 27.5    8250.0    250.0
£ 28.0    7280.0    -720.0
£ 28.5    6270.0    -1730.0
£ 29.0    5220.0    -2780.0
£ 29.5    4130.0    -3870.0
£ 30.0    3000.0    -5000.0

``````
``````

In [122]:

import matplotlib.pyplot as plt

def profitList(maxPrice):
result = [], []
for price in [x/2.0 for x in range(1, (maxPrice)*2+1)]:
sales = 2500 - 80 * price
income = sales * price
result[0].append(price)
result[1].append(income-8000)
return result

lists = profitList(30)
plt.plot(lists[0], lists[1])
plt.ylabel('Profit')
plt.xlabel('Price')
plt.show()

``````
``````

``````
``````

In [ ]:

``````