Answer To: This is Python Notebook HW. Please check the file and answer all of the questions.
Vicky answered on Nov 15 2021
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## BANA212 Midterm 2020\n",
"\n",
"* Time: 8am PST (Saturday, November 14, 2020) to 8pm PST (Sunday, November 15, 2020)\n",
"* Submission: please upload both .ipynb file and .html file on Canvas under Assignments -> Midterm\n",
"* You are required to answer all questions.\n",
"* This is a “open book, open computer” exam\n",
"* You should solve those problems on your own. You are allowed to \"google\" or do your own research on the internet, but you are not allowed to seek help from others, including but not limited to family members, friends, classmates and so forth.\n",
"* Please be as succinct as possible in answering the questions.\n",
"* The total score is 100 points.\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Please initial here that if you agree to comply to the UCI Academic Honesty Policy:________\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part I. Multiple Choices and Short-Answer Questions (25 pts)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q1. Which of the following has a different number of iterations?"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"101\n"
]
}
],
"source": [
"#a)\n",
"i = 0\n",
"while i <=100:\n",
" i = i+1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n"
]
}
],
"source": [
"#b) \n",
"for i in range(100):\n",
" print(\"Hi\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n"
]
}
],
"source": [
"#c) \n",
"for i in range(100, 0, -1):\n",
" print(\"Hi\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n"
]
}
],
"source": [
"#d) \n",
"i=100\n",
"while (i>0):\n",
" print(\"Hi\")\n",
" i = i-1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your Answer Here: a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q2. Write a list comprehension to take in a list and transform each word into its uppercase and also calculate the length of the word.\n",
"\n",
"str1 =[\"Merage\", \"Python\", \"programming\"]\n",
"\n",
"Expected output: [('MERAGE', 6), ('PYTHON', 6), ('PROGRAMMING', 11)]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Your Answer Here:\n",
"def transform(string):\n",
" output = [(i.upper(),len(i)) for i in str1] # list comprehension\n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('MERAGE', 6), ('PYTHON', 6), ('PROGRAMMING', 11)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str1 =[\"Merage\", \"Python\", \"programming\"]\n",
"transform(str1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q3. How many iterations the following while loop will run (we assume we use Python 3.x)?"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"i = 1000\n",
"while i >1:\n",
" i = i/2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a) 500\n",
"\n",
"b) 10\n",
"\n",
"c) 499\n",
"\n",
"d) 9\n",
"\n",
"e) 1001"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your Answer Here: b) \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q4. Write a program which will find all such numbers which are divisible by 6 but are not a multiple of 5, between 1000 and 2000 (both included).\n",
"\n",
"The numbers obtained should be printed in a comma-separated...