t = (17, 21)
(a,b) = t
type(t)
(a,b) = (27, 31)
print(a)
print(b)
a, b = 37, 41
print(a)
print(b)
type(a)
type((a,b))
Problem 1 (not involving lambda): • You’re given an array a of random integers from 1 to 1000 • Let b be a copy of a, but sorted in ascending order • Print b (edited)
# Let c be a copy of a,
# but sorted by how many of the digit 5 each number has (it gets listed first if it has more 5s
import random
a = random.sample(range(1, 1000), 100)
b = sorted(a)
get_fives = lambda x: [int(i) for i in str(x) if i == '5']
three_fives = []
two_fives = []
one_five = []
zero_fives = []
for num in a:
if len(get_fives(num)) == 3:
three_fives.append(num)
elif len(get_fives(num)) == 2:
two_fives.append(num)
elif len(get_fives(num)) == 1:
one_five.append(num)
else:
zero_fives.append(num)
c = sorted(three_fives) + sorted(two_fives) + sorted(one_five) + sorted(zero_fives)
get_fives = lambda x: [{x: 1} for i in str(x) if i == '5']
get_fives(365)
# c
# sum(1 for y in x if y > 2)
import random
a = random.sample(range(1, 1000), 100)
b = sorted(a)
get_f = lambda x: {x: sum(1 for i in str(x) if i == '5')}
get_f(356)
c = [get_f(num) for num in a]
sorted(a, key=lambda x: sum(1 for i in str(x) if i == '5'), reverse=True)