Week 4 All Grpa
Week 4 All Grpa
Grpa 1 ~~~~
# Note this prefix code is to verify that you are not using any for loops in this
exercise. This won't affect any other functionality of the program.
with open(__file__) as f:
content = f.read().split("# <nofor>")[2]
if "for " in content:
print("You should not use for loop or the word for anywhere in this exercise")
some_iterable = (1,2,3)
another_iterable = {"apple", "banana", "cherry"} # can be any iterable
yet_another_iterable = range(1,10)
# <nofor>
# <eoi>
empty_list = []
empty_set = set() # be carefull here you might end up creating something called as
an empty dict
empty_tuple = ()
a_falsy_list = [] # list: a list but when passed to bool function should return
False.
a_falsy_set = set() # set: a list but when passed to bool function should return
False.
a_truthy_tuple = (1,) # tuple: a tuple but when passed to bool function should
return True
Grpa 2 ~~~
# Note this prefix code is to verify that you are not using any for loops in this
exercise. This won't affect any other functionality of the program.
with open(__file__) as f:
content = f.read().split("# <noloop>")[2]
if "for " in content or "while " in content:
print("You should not use for loop, while loop or the word for and while
anywhere in this exercise")
# note that apart from the print statements inside the functions, the evaluator
will also print what is returned by the function at last
# <noloop>
# print a list with the first three elements in items added to the end of the
items again
print("extend:", items+items[:3])
# print a list with the fifth element from items removed
print("pop:",items[:4]+items[5:])
return items
# print the common elements in both set2 and set3 as a sorted list.
print(sorted(set2.intersection(set3)))
# print all unique elements present in set1, set2 an set3 as a sorted list
print(sorted(set1.union(set2, set3)))
# print all unique elements that are in set2 but not in set3 as a sorted list
print(sorted(set2.difference(set3)))
# print all the non common elements from both set2 and set3
print(sorted(set2.symmetric_difference(set3)))
return set1,sorted(set1),sorted(set2),sorted(set3)
Grpa 3 ~~~
min = None
min = None
del min #I have use it to del given min in question, otherwise it will not run
min_val = None
def find_min(items:list):
min_val = min(items)
return min_val
def more_than_two_unique_vowels(sentence):
vowels = 'aeiou'
split_sentence = sentence.split(',')
result = set()
for word in split_sentence:
unique_vowels = set(char for char in word.lower() if char in vowels)
if (len(unique_vowels) > 2):
result.add(word)
return result
def sum_of_list_of_lists(lol):
total_sum = 0
for sublist in lol:
total_sum += sum(sublist)
return total_sum
def flatten(lol):
result = []
for item in lol:
if isinstance(item, list):
result.extend(flatten(item))
else:
result.append(item)
return result
def all_common(strings):
common_chars = set(strings[0])
for string in strings[1:]:
common_chars &= set(string)
return ''.join(sorted(common_chars))
def vocabulary(sentences):
vocab = set()
for sentence in sentences:
words = sentence.lower().split()
vocab.update(words)
return vocab
Grpa 4 ~~~
def swap_halves(items):
mid = len(items) // 2
return items[mid:] + items[:mid]
def reverse_first_and_last_halves(items):
mid = len(items) // 2
items[:mid] = reversed(items[:mid])
items[mid:] = reversed(items[mid:])
Grpa 5 ~~~
def sum_of_squares(lst):
return sum(x**2 for x in lst)
def total_cost(items):
return sum(q * p for q, p in items)
def abbreviation(s):
return '.'.join(word[0].upper() for word in s.split()) + '.'
def palindromes(words):
return [word for word in words if word == word[::-1]]
def all_chars_from_big_words(sentence):
return set(char.lower() for word in sentence.split() if len(word) > 5 for char
in word)
def flatten(nested_list):
return [item for sublist in nested_list for item in sublist]
def make_identity_matrix(size):
return [[1 if i == j else 0 for j in range(size)] for i in range(size)]
def make_lower_triangular_matrix(m):
return [[j + 1 if j <= i else 0 for j in range(m)] for i in range(m)]