In [1]:
t = (17, 21)
In [2]:
(a,b) = t
In [3]:
type(t)
Out[3]:
tuple
In [4]:
(a,b) = (27, 31)
In [5]:
print(a)
27
In [6]:
print(b)
31
In [7]:
a, b = 37, 41
In [8]:
print(a)
37
In [9]:
print(b)
41
In [10]:
type(a)
Out[10]:
int
In [12]:
type((a,b))
Out[12]:
tuple

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)

In [ ]:
#  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
In [4]:

In [35]:
 
In [56]:
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)
  File "<ipython-input-56-a377cf8efed1>", line 5
    three_fives = for
                    ^
SyntaxError: invalid syntax
In [62]:
get_fives = lambda x: [{x: 1} for i in str(x) if i == '5']
get_fives(365)
Out[62]:
[{365: 1}]
In [58]:
# c
In [71]:
# 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]
In [75]:
sorted(a, key=lambda x: sum(1 for i in str(x) if i == '5'), reverse=True)
Out[75]:
[558,
 505,
 535,
 125,
 257,
 528,
 152,
 510,
 305,
 574,
 758,
 587,
 583,
 145,
 579,
 405,
 560,
 475,
 352,
 523,
 845,
 275,
 495,
 356,
 450,
 539,
 544,
 582,
 189,
 294,
 498,
 486,
 631,
 891,
 969,
 671,
 311,
 196,
 290,
 966,
 300,
 407,
 393,
 781,
 116,
 91,
 26,
 860,
 870,
 636,
 270,
 872,
 148,
 436,
 343,
 419,
 37,
 180,
 734,
 183,
 773,
 240,
 219,
 791,
 617,
 763,
 913,
 403,
 182,
 678,
 106,
 349,
 747,
 723,
 930,
 40,
 788,
 832,
 302,
 607,
 363,
 889,
 766,
 662,
 317,
 646,
 289,
 377,
 871,
 438,
 278,
 12,
 32,
 96,
 318,
 236,
 801,
 479,
 364,
 82]
In [ ]: