-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "3308bf7d-8866-4516-8c7b-c8bf78dac36e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Robert Cocker\n", | ||
"# Dr. Farbin\n", | ||
"# DATA-3402\n", | ||
"# Quiz\n", | ||
"# 2/15/2024" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "058ad468-d4aa-4ee0-a8c7-28935f054bad", | ||
"metadata": {}, | ||
"source": [ | ||
"## Quick Quiz" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2c962e7f-644c-4b66-9ff8-18ccfaef5989", | ||
"metadata": {}, | ||
"source": [ | ||
"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, | ||
"id": "2456eff4-dd07-4094-abe5-d4e70aea3d57", | ||
"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=list()\n", | ||
"\n", | ||
" for a in args:\n", | ||
" if not isinstance(a,list):\n", | ||
" a0=[a]*max_len\n", | ||
" elif len(a)!=max_len:\n", | ||
" print(\"Error: all list arguments must have same length.\")\n", | ||
" return\n", | ||
" else:\n", | ||
" a0=a\n", | ||
" new_args.append(a0)\n", | ||
"\n", | ||
" return new_args" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "4026f851-2114-4b3c-8860-b34b60d8503b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def create_new_args(args):\n", | ||
" max_len = max(map(len, filter(lambda x: isinstance(x, list), args)))\n", | ||
" return [a if isinstance(a, list) and len(a) == max_len else [a] * max_len for a in args]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "bc1c9754-c602-4fd2-a266-62a67207b16a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"create_new_args2 = lambda args: [[a] * max(map(len, filter(lambda x: isinstance(x, list), args))) if not isinstance(a, list) else a if len(a) == max(map(len, filter(lambda x: isinstance(x, list), args))) else print(\"Error: all list arguments must have same length.\") for a in args]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "93837073-e31c-4a24-a3ab-17b3d941c479", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[[1, 2], [3, 4], [5, 5]]" | ||
] | ||
}, | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"create_new_args_0([[1,2],[3,4],5])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "6618fddb-b839-4d78-b0f5-06d23f20162e", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Error: all list arguments must have same length.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"create_new_args_0([[1,2],[3,4,5],5])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "71ff2bd8-dd64-4940-8c5b-4f16b786e5d8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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": 5 | ||
} |