PYTHON WEEK-4 GRADED ASSIGNMENT

preview_player
Показать описание
PYTHON WEEK-4 GRADED ASSIGNMENT
Рекомендации по теме
Комментарии
Автор

GRPA-1
import collections
empty_list = []
empty_set = set() # be carefull here you might end up creating something called as an empty dict
empty_tuple = ()

singleton_list = ["a"] # list: A list with only one element
singleton_set = set('a') # set: A set with only one element
singleton_tuple = tuple('a') # tuple: A tuple with only one element

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 = ("a", ) # tuple: a tuple but when passed to bool function should return True

int_iterable_min = min(int_iterable) # int: find the minimum of int_iterable. Hint: use min function
int_iterable_max = max(int_iterable) # int: find the maximum of int_iterable. Hint: use max function
int_iterable_sum = sum(int_iterable) # int: you know what to do
int_iterable_len = len(int_iterable) # int: really... you need hint?
int_iterable_sorted = sorted(list(int_iterable)) # list: the int_iterable sorted in ascending order
int_iterable_sorted_desc = sorted(list(int_iterable), reverse=True) # list: the int_iterable sorted in desc order

if hasattr(int_iterable, '__reversed__') : # some iterables are not reversible why?
int_iterable_reversed = list(reversed(int_iterable)) # list: the int_iterable reversed use the reversed function
else: # in that case sort it in ascending order and reverse it
int_iterable_reversed = #list

if hasattr(some_collection, '__getitem__') : # some collections are not indexable why?
third_last_element = some_collection[-3] # the third last element of `some_collection`
else: # in that case set third_last_element to None
third_last_element = None

if hasattr(some_collection, '__getitem__') : # some collections are not slicable
odd_index_elements = some_collection[1::2] # type(some_collection): the elements at odd indices of `some_collection`
else: # in that case set odd_index_elements to None
odd_index_elements = None

= some_value in some_collection # bool: True if `some_value` is present in `some_collection`

if hasattr(some_collection, '__getitem__') : # some collections are not ordered
is_some_value_in_even_indices = some_value in some_collection[0::2] # bool: True if `some_value` is present in even indices of `some_collection`
else: # in that case set is_some_value_in_even_indices to None
is_some_value_in_even_indices = None

all_iterables = # list: concatenate `some_iterable`, `another_iterable` and `yet_another_iterable` into a list.
if isinstance(string_iterable, (list, tuple)):

else:
all_concat =

if type(string_iterable)==list : # some iterables are not ordered
all_concat = '-'.join(string_iterable) # str: concatenate all the strings in string_iterable with '-' in between
else: # in that case sort them and concatenate
all_concat =

GRPA-2
def list_mutating_operations(items:list, item1, item2):
# sort the `items` inplace
items.sort()
print("sorted:", items)

# add item1 to the `items` at the end
items.append(item1)
print("append:", items)

# add item2 at index 3
items.insert(3, item2)
print("insert:", items)

# extend `items` with the first three elements in `items`
items.extend(items[:3])
print("extend:", items)

# pop the fifth element and store it in variable `popped_item`
popped_item = items.pop(4)
print("pop:", items)

# remove first occurance of `item2` from the list
items.remove(item2)
print("remove:", items)

# make the element at index 4 None
items[4] = None
print("modify_index:", items)

# make the even indices None
items[::2] = [None]*len(items[::2])
print("modify_slice:", items)

# delete the third last element
del items[-3]
print("delete_index:", items)

# delete the even indices
del items[::2]
print("delete_slice:", items)

return items, popped_item

def list_non_mutating_operations(items:list, item1, item2):

# print the sorted version of items
print("sorted:", sorted(items))

# print a lsit with item1 appended to the `items` at the end
print("append:", items+[item1])

# print a list with item2 added to items at index 3
print("insert:", items[:3]+[item2]+items[3:])

# 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:])

# print a list with first occurance of `item2` removed from `items`
index = items.index(item2)
print("remove:", items[:index]+items[index+1:]) # hint: you may want to use index

# print a list with the fourth element of `items` changed to None
print("modify_index:", items[:3]+[None]+items[4:])

# print a list with the even indices changed to None
modified = items[::] # make a copy or use items.copy()
modified[::2] = [None]*len(modified[::2])
print("modify_slice:", modified)

# print a list with the even indices removed
print("delete_slice:", modified[1::2])

return items

def do_set_operation(set1, set2, set3, item1, item2):
# add item1 to set1
set1.add(item1)
print(sorted(set1))
# remove item2 from set1. What if item2 is not in set1?
set1.discard(item2)
print(sorted(set1))

# add elements from set2 to set1
set1.update(set2)
print(sorted(set1))

# remove all elements from set1 that are in set3
set1.difference_update(set3)
print(sorted(set1))

# print the common elements in both set2 and set3 as a sorted list.
print(sorted(set2 & set3))

# print all unique elements present in set1, set2 an set3 as a sorted list
print(sorted(set1 | set2 | set3))

# print all unique elements that are in set2 but not in set3 as a sorted list
print(sorted(set2 - set3))

# print all the non common elements from both set2 and set3


return set1, sorted(set1), sorted(set2), sorted(set3)

GRPA-3
def find_min(items:list):

minimum = items[0]
for item in items[1:]:
if item<minimum:
minimum = item
return minimum

def -> list:

new_items = []
for item in items:
if item%2!=0:
new_items.append(item+1)
else:
new_items.append(item-1)
return new_items

def -> list:

for i in range(len(items)):
if items[i]%2!=0:
items[i]**=2
else:
items[i]*=2
return items

def

vowels = set("aeiou")
words = set()
for word in sentence.split(", "):
if len(set(word) & vowels)>2:
words.add(word)
return words

def sum_of_list_of_lists(lol):

total = 0
for row in lol:
for n in row:
total+=n
return total

def flatten(lol):

flat = []
for row in lol:
for item in row:
flat.append(item)
return flat

def all_common(strings):

common_chars = set(strings[0])
for string in strings[1:]:
common_chars &= set(string)
return

def vocabulary(sentences):

vocab = set()
for sentence in sentences:
for word in sentence.split(" "):
vocab.add(word.lower())
return vocab

GRPA-4
def swap_halves(items):

m = len(items)//2
return items[m:]+items[:m]

def swap_at_index(items, k):

return items[k+1:] + items[:k+1]

def rotate_k(items, k=1):

k = k % len(items)
return items[-k:]+items[:-k]

def first_and_last_index(items, elem):

return items.index(elem),

def

m = len(items)//2
items[:m], items[m:] = items[:m][::-1], items[m:][::-1]

GRPA-5
def sum_of_squares(numbers):

return sum([num**2 for num in numbers])

def total_cost(cart):

return sum([quantity*price for quantity, price in cart])

def abbreviation(sentence):

return ".".join([word[0].upper() for word in sentence.split()])+"."

def palindromes(words):

return [word for word in words if word == word[::-1]]

def

return {
letter.lower()
for word in sentence.split()
for letter in word
if len(word)>5
}

def flatten(lol):

return [elem for row in lol for elem in row]

def unflatten(items, n_rows):

n_cols = len(items)//n_rows
return [
[items[n_cols*j+i] for i in range(n_cols)]
for j in range(n_rows)
]

def make_identity_matrix(m):

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

def

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

Omya_
Автор

4rth is wrong bro i think may be i am confused

shuryansmishra