{ "cells": [ { "cell_type": "markdown", "id": "cceb0c53-86e0-416b-b626-c8ba5e2f3695", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak

**Assignment #4: List Practice**

Assigned: February 22, 2023
Due: March 1 at 5:30 PM

100 points maximum
Individual work only!
" ] }, { "cell_type": "markdown", "id": "bdacc22c-884f-4db0-8e4f-d5f8552900ec", "metadata": {}, "source": [ "#### Solve each of the following Python programming problems using only list operations and list methods. Do not use any other Python data structures such as dictionaries or sets." ] }, { "cell_type": "markdown", "id": "54960783-b919-4b54-8ddc-aa03414fff57", "metadata": {}, "source": [ "#### **PROBLEM 1:** [20 points] Define a function `swap_halves()` that has one parameter whose value you may assume is a list. If the list has an even length, the function should swap the first half of the list with the second half, and return the modified list. If the list has an odd number of elements, the function should first append a element with value 0 at the end of the list, and then perform the swap and return.\n", "\n", "#### For example, if your function is passed the value `[1, 2, 3, 4, 5, 6]`, it should return the list `[4, 5, 6, 1, 2, 3]`.\n", "\n", "#### Test your function with at least the following lists:\n", "- `[1, 2, 3, 4, 5, 6]`\n", "- `[12, 56, 33, 81, 33, 94, 56]`\n", "- `['Hello', ', ', 'world!']`\n", "- `[1]`\n", "- `[]`" ] }, { "cell_type": "markdown", "id": "c60f47db-016b-4486-bee5-0da45178b230", "metadata": {}, "source": [ "#### **PROBLEM 2:** [20 points] Define a function `similar_lists()` that has two parameters whose values you may assume are lists of integer values. The function should check whether the two lists are similar -- they contain the same element values ignoring order and duplicates -- and return `True` if they are similar or `False` otherwise. Your function should **not** modify the lists that are passed to it.\n", "\n", "#### For example, the two lists\n", "- `[1, 4, 9, 16, 9, 7, 4, 9, 11]`\n", "- `[11, 11, 7, 9, 16, 4, 1]`\n", "#### are similar. But\n", "- `[11, 11, 7, 9, 16, 4, 1]`\n", "- `[9, 7, 11, 1, 8, 4, 16]`\n", "#### are not.\n", "\n", "#### Test your function with at least the above two pairs of lists." ] }, { "cell_type": "markdown", "id": "5c39e9d3-8f05-49cd-b686-fda230ce6987", "metadata": {}, "source": [ "#### **PROBLEM 3:** [20 points] Write Python code that simulates rolling a fair die 20 times and looking for runs. A run is a sequence of adjacent repeated values. Your program should generate 20 random die rolls and store the values into a list. Then it should print the values from the list, enclosing the runs in parentheses.\n", "\n", "#### Example: `1 2 ( 5 5 ) 3 1 2 4 3 ( 2 2 2 2 ) 3 6 ( 1 1 ) 6 3 2`" ] }, { "cell_type": "markdown", "id": "369282a2-7af6-473a-a10d-29e68686a9aa", "metadata": {}, "source": [ "#### **PROBLEM 4.a:** [20 points] Consider the following population data. \n", "####
**Population Per Continent (in millions)**
\n", "| Year | 1750 | 1800 | 1850 | 1900 | 1950 | 2000 | 2050 |\n", "| --- | --: | --: | --: | --: | --: | --: | --: |\n", "| **Africa** | 106 | 107 | 111 | 133 | 221 | 767 | 1766 |\n", "| **Asia** | 502 | 635 | 809 | 947 | 1402 | 3634 | 5268 |\n", "| **Australia** | 2 | 2 | 2 | 6 | 13 | 30 | 46 |\n", "| **Europe** | 163 | 203 | 276 | 408 | 547 | 729 | 628 |\n", "| **North America** | 2 | 7 | 26 | 82 | 172 | 307 | 392 |\n", "| **South America** | 16 | 24 | 38 | 74 | 167 | 511 | 809 |\n", "#### Represent the data as a two-dimensional list. Include the column headers and the continent names, but not the table title." ] }, { "cell_type": "markdown", "id": "78296d12-00ee-420b-8fce-8b75e8dfd77c", "metadata": {}, "source": [ "#### **PROBLEM 4.b:** [20 points] Print the two-dimensional list as a neatly formatted table. Add column totals as a new bottom row that show the world population in the given years." ] }, { "cell_type": "code", "execution_count": null, "id": "2e966378-ede2-41fd-8198-1a18f3a81e1b", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }