|
| 1 | +--- |
| 2 | +title: GrPA 1 Basic Collections |
| 3 | +label: Graded |
| 4 | +weight: 1 |
| 5 | +subject: programming |
| 6 | +subtitle: Week 4 GrPA |
| 7 | +categories: |
| 8 | +- Python Graded Assignment |
| 9 | +--- |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## GrPA 1 Basic Collections 👨💻 |
| 14 | + |
| 15 | +{{< border >}} |
| 16 | + |
| 17 | +{{< tabs items="QUESTION,TEST CASES,SOLUTION" >}} |
| 18 | + |
| 19 | +{{< tab >}} |
| 20 | + |
| 21 | +{{< details title="Instructions" closed="true" >}} |
| 22 | + |
| 23 | +{{< /details >}} |
| 24 | + |
| 25 | +{{< /tab >}} |
| 26 | + |
| 27 | +{{< /tabs >}} |
| 28 | + |
| 29 | +{{< /border >}} |
| 30 | + |
| 31 | +## Question ❓ |
| 32 | + |
| 33 | +Implement the below functions as per the docstrings. |
| 34 | + |
| 35 | +## Python Code 🐍 |
| 36 | + |
| 37 | +```python {linenos=table,linenostart=1} |
| 38 | +# 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. |
| 39 | +with open(__file__) as f: |
| 40 | + content = f.read().split("# <nofor>")[2] |
| 41 | +if "for " in content: |
| 42 | + print("You should not use for loop or the word for anywhere in this exercise") |
| 43 | + |
| 44 | +# The values of the below variables will be changed by the evaluator |
| 45 | +int_iterable = range(1,10,3) |
| 46 | +string_iterable = ["Apple","Orange", "Banana"] |
| 47 | +some_value = 4 |
| 48 | +some_collection = [1,2,3] # list | set | tuple |
| 49 | + |
| 50 | +some_iterable = (1,2,3) |
| 51 | +another_iterable = {"apple", "banana", "cherry"} # can be any iterable |
| 52 | +yet_another_iterable = range(1,10) |
| 53 | + |
| 54 | +# <nofor> |
| 55 | +# <eoi> |
| 56 | + |
| 57 | +empty_list = ... |
| 58 | +empty_set = ... # be carefull here you might end up creating something called as an empty dict |
| 59 | +empty_tuple = ... |
| 60 | + |
| 61 | +singleton_list = ... # list: A list with only one element |
| 62 | +singleton_set = ... # set: A set with only one element |
| 63 | +singleton_tuple = ... # tuple: A tuple with only one element |
| 64 | + |
| 65 | +a_falsy_list = ... # list: a list but when passed to bool function should return False. |
| 66 | +a_falsy_set = ... # set: a list but when passed to bool function should return False. |
| 67 | +a_truthy_tuple = ... # tuple: a tuple but when passed to bool function should return True |
| 68 | + |
| 69 | +int_iterable_min = ... # int: find the minimum of int_iterable. Hint: use min function |
| 70 | +int_iterable_max = ... # int: find the maximum of int_iterable. Hint: use max function |
| 71 | +int_iterable_sum = ... # int: you know what to do |
| 72 | +int_iterable_len = ... # int: really... you need hint? |
| 73 | +int_iterable_sorted = ... # list: the int_iterable sorted in ascending order |
| 74 | +int_iterable_sorted_desc = ... # list: the int_iterable sorted in desc order |
| 75 | + |
| 76 | +if ... : # some iterables are not reversible why? |
| 77 | + int_iterable_reversed = ... # list: the int_iterable reversed use the reversed function |
| 78 | +else: # in that case sort it in ascending order and reverse it |
| 79 | + int_iterable_reversed = ... #list |
| 80 | + |
| 81 | +if ...: # some collections are not indexable why? |
| 82 | + third_last_element = ... # the third last element of `some_collection` |
| 83 | +else: # in that case set third_last_element to None |
| 84 | + third_last_element = ... |
| 85 | + |
| 86 | +if ...: # some collections are not slicable |
| 87 | + odd_index_elements = ... # type(some_collection): the elements at odd indices of `some_collection` |
| 88 | +else: # in that case set odd_index_elements to None |
| 89 | + odd_index_elements = ... |
| 90 | + |
| 91 | +is_some_value_in_some_collection = ... # bool: True if `some_value` is present in `some_collection` |
| 92 | + |
| 93 | +if ...: # some collections are not ordered |
| 94 | + is_some_value_in_even_indices = ... # bool: True if `some_value` is present in even indices of `some_collection` |
| 95 | +else: # in that case set is_some_value_in_even_indices to None |
| 96 | + is_some_value_in_even_indices = ... |
| 97 | + |
| 98 | +all_iterables = ... # list: concatenate `some_iterable`, `another_iterable` and `yet_another_iterable` into a list. |
| 99 | + |
| 100 | +if ... : # some iterables are not ordered |
| 101 | + all_concat = ... # str: concatenate all the strings in string_iterable with '-' in between |
| 102 | +else: # in that case sort them and concatenate |
| 103 | + all_concat = ... |
| 104 | + |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +## Python Code Solution ✅ |
| 109 | + |
| 110 | +```python {linenos=table,linenostart=1} |
| 111 | +# ...existing code... |
| 112 | + |
| 113 | +empty_list = [] |
| 114 | +empty_set = set() |
| 115 | +empty_tuple = () |
| 116 | + |
| 117 | +singleton_list = [42] |
| 118 | +singleton_set = {42} |
| 119 | +singleton_tuple = (42,) |
| 120 | + |
| 121 | +a_falsy_list = [] |
| 122 | +a_falsy_set = set() |
| 123 | +a_truthy_tuple = (0,) |
| 124 | + |
| 125 | +int_iterable_min = min(int_iterable) |
| 126 | +int_iterable_max = max(int_iterable) |
| 127 | +int_iterable_sum = sum(int_iterable) |
| 128 | +int_iterable_len = len(int_iterable) |
| 129 | +int_iterable_sorted = sorted(int_iterable) |
| 130 | +int_iterable_sorted_desc = sorted(int_iterable, reverse=True) |
| 131 | + |
| 132 | +if hasattr(int_iterable, '__reversed__'): |
| 133 | + int_iterable_reversed = list(reversed(int_iterable)) |
| 134 | +else: |
| 135 | + int_iterable_reversed = list(reversed(sorted(int_iterable))) |
| 136 | + |
| 137 | +if hasattr(some_collection, '__getitem__') and hasattr(some_collection, '__len__'): |
| 138 | + third_last_element = some_collection[-3] |
| 139 | +else: |
| 140 | + third_last_element = None |
| 141 | + |
| 142 | +if hasattr(some_collection, '__getitem__') and hasattr(some_collection, '__len__'): |
| 143 | + odd_index_elements = type(some_collection)(some_collection[1::2]) |
| 144 | +else: |
| 145 | + odd_index_elements = None |
| 146 | + |
| 147 | +is_some_value_in_some_collection = some_value in some_collection |
| 148 | + |
| 149 | +if isinstance(some_collection, (list, tuple, str)): |
| 150 | + is_some_value_in_even_indices = some_value in some_collection[::2] |
| 151 | +else: |
| 152 | + is_some_value_in_even_indices = None |
| 153 | + |
| 154 | +all_iterables = list(some_iterable) + list(another_iterable) + list(yet_another_iterable) |
| 155 | + |
| 156 | +if isinstance(string_iterable, (list, tuple)): |
| 157 | + all_concat = '-'.join(string_iterable) |
| 158 | +else: |
| 159 | + all_concat = '-'.join(sorted(string_iterable)) |
| 160 | + |
| 161 | +# ...existing code |
| 162 | +``` |
0 commit comments