GrPA 5 Comprehensions

GrPA 5 Comprehensions


Week 4 GrPA


GrPA 5 Comprehensions 👨‍💻

Instructions

Question ❓

Implement the below functions as per the docstrings.

Python Code 🐍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def sum_of_squares(numbers):
    ...

def total_cost(cart):
    ...

def abbreviation(sentence):
    ...

def palindromes(words):
    ...

def all_chars_from_big_words(sentence):
    ...

def flatten(lol):
    ...

def unflatten(items, n_rows):
    ...

def make_identity_matrix(m):
    ...

def make_lower_triangular_matrix(m):
    ...

Python Code Solution ✅

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def sum_of_squares(numbers):
    return sum([x**2 for x in numbers])

def total_cost(cart):
    return sum([qty * price for qty, price in cart])

def abbreviation(sentence):
    return ''.join([word[0].upper() for word in sentence.split()]) + '.'

def palindromes(words):
    return [w for w in words if w == w[::-1]]

def all_chars_from_big_words(sentence):
    return set(''.join([w.lower() for w in sentence.split() if len(w) > 5]))

def flatten(lol):
    return [item for sublist in lol for item in sublist]

def unflatten(items, n_rows):
    n_cols = len(items) // n_rows
    return [items[i*n_cols:(i+1)*n_cols] for i in range(n_rows)]

def make_identity_matrix(m):
    return [[1 if i == j else 0 for j in range(m)] for i in range(m)]

def make_lower_triangular_matrix(m):
    return [[i+1 if j <= i else 0 for j in range(m)] for i in range(m)]