diff --git a/Labs/Lab.2/Quiz #1 Hala Muhiar 1001900490.ipynb b/Labs/Lab.2/Quiz #1 Hala Muhiar 1001900490.ipynb new file mode 100644 index 0000000..85e8dde --- /dev/null +++ b/Labs/Lab.2/Quiz #1 Hala Muhiar 1001900490.ipynb @@ -0,0 +1,91 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Quick Quiz\n", + "\n", + "Can you rewrite `create_new_args` as a two lines of code using functional programming, list comprehensions, and shortcuts? How about a single line?" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def create_new_args_0(*args):\n", + " max_len = max(map(len,\n", + " filter(lambda x: isinstance(x,list),\n", + " args)))\n", + "\n", + " # Rewrite this section:\n", + " new_args = [[a]*max_len if isinstance(a, list) else a if len(a)==max_len else None for a in args]\n", + "\n", + " return new_args" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[[[1, 2], [3, 4], 5], [[1, 2], [3, 4], 5], [[1, 2], [3, 4], 5]]]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_new_args_0([[1,2],[3,4],5])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[[[1, 2], [3, 4, 5], 5], [[1, 2], [3, 4, 5], 5], [[1, 2], [3, 4, 5], 5]]]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_new_args_0([[1,2],[3,4,5],5])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}