Answer To: • Go to the Anaconda Website and select your computer’s operating system. • Locate your systems...
Dhairya answered on May 07 2021
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions\n",
"\n",
"* Fill in the code and markdown cells. Please don't change the order of the questions, or move the question cells.\n",
"* Feel free to add code and markdown cells if you need them.\n",
"\n",
"* **KEEP SAVING YOUR WORK**\n",
"* Submit to Canvas at the end.\n",
"* Good Luck !\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 1 : \n",
"### 5 pts \n",
"Store all of the words with more than 4 letters in a list. Use the **append** method for lists and the **strip** method for strings to get rid of punctuation. "
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"sentence = \"\"\"Snow White likes to eat apples. So, she went to the forest to acquire some. \n",
"She got attacked by a wolf and almost died of food poisoning because of an evil witch. She however declared: I love apples.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['White',\n",
" 'likes',\n",
" 'apples',\n",
" 'forest',\n",
" 'acquire',\n",
" 'attacked',\n",
" 'almost',\n",
" 'poisoning',\n",
" 'because',\n",
" 'witch',\n",
" 'however',\n",
" 'declared',\n",
" 'apples']"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# write your code here\n",
"list_all_words = sentence.split(' ')\n",
"list_words_length_greater_than_4 = []\n",
"for word in list_all_words:\n",
" stripped_word = word.strip(',.:')\n",
" if len(stripped_word) > 4:\n",
" list_words_length_greater_than_4.append(stripped_word)\n",
"list_words_length_greater_than_4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 2A : \n",
"### 5 pts \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a program that asks the user to enter a message that they would like to code, and then output the coded message.\n",
"The coding process is the following: insert the characters 'ay' between every letter of the given message.\n",
"\n",
"For example: \n",
"\n",
"Input: hello \n",
"Output: hayeaylaylayoay\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a message that you would like to code: hello\n"
]
},
{
"data": {
"text/plain": [
"'hayeaylaylayoay'"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# write your code here\n",
"message = input('Enter a message that you would like to code: ')\n",
"output = ''\n",
"for char in message:\n",
" output += char + 'ay'\n",
"output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 2B: \n",
"### 10 pts \n",
"\n",
"Write a coder function that takes a string input and adds 'ay' after the first char and then every other two. Or in other words adds 'ay' after every even-indexed character of the given string. \n",
"\n",
"Include a docstring to the function and illustrate its use by calling help on it.\n",
"\n",
"For example: \n",
"\n",
"Input: hello \n",
"Output: hayelayloay "
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a message that you would like to code: hello\n",
"Help on function coder_function in module __main__:\n",
"\n",
"coder_function(input_str: str)\n",
" Returns a coded message for your input message by adding 'ay' after every even-indexed character of the given string.\n",
"\n"
]
}
],
"source": [
"# write your code here\n",
"def coder_function(input_str:str):\n",
" '''Returns a coded message for your input message by adding 'ay' after every even-indexed character of the given string.'''\n",
" output = ''\n",
" for i in range(len(input_str)): # looping for each character in message\n",
" if i % 2 == 0: # if even index\n",
" output += input_str[i] + 'ay'\n",
" else: # else odd index\n",
" output += input_str[i] \n",
" return output \n",
" \n",
"message = input('Enter a message that you would like to code: ')\n",
"help(coder_function) # calling help\n",
"\n",
"ouptut = coder_function(message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 3: \n",
"### 20 pts \n",
"Write a function that takes three arguments: a principal amount, annual rate of return, and number of years and displays the year on year growth of an initial investment.\n",
"\n",
"Input1: \n",
"invest(100,0.04,1) \n",
"\n",
"Desired output1: \n",
"year 1: 104.00 dollars \n",
"\n",
"Input2: invest(100,0.04,3) \n",
"\n",
"Desired output1: \n",
"year 1: 104.00 dollars \n",
"year 2: 108.16 dollars \n",
"year 3: 112.49 dollars \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Year 1: 104.00 dollars\n",
"Year 2: 108.16 dollars\n",
"Year 3: 112.49 dollars\n"
]
}
],
"source": [
"# write your code here\n",
"def invest(principal, rate_return, years):\n",
" '''Returns the year on year growth of the initial investment.'''\n",
" \n",
" for year in range(years):\n",
" principal += principal * rate_return \n",
" print('Year {}: {:.2f} dollars'.format(year + 1, principal))\n",
"\n",
"invest(100, 0.04, 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 4A : \n",
"### 20 pts \n",
"\n",
"To answer this question you will be required to read with Python the given text file 'us_state_info.txt' and perform manipulations to extract the data and answer the following questions. \n",
"\n",
"The given file contains in order, for each US state,its name, its two letter abbreviation and its capital city.\n",
"\n",
"1) Extract the State name and the state abbreviation from the provided text file and create a dictionary that will have for keys the state names and for values the respective state abbreviation.\n",
"\n",
"2) Extract the State name and the state capital from the provided text file and create another dictionary that will have for keys the state names and for values the respective state capital."
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ', 'Arkansas': 'AR', 'California': 'CA', 'Colorado': 'CO', 'Connecticut': 'CT', 'Delaware': 'DE', 'Florida': 'FL', 'Georgia': 'GA', 'Hawaii': 'HI', 'Idaho': 'ID', 'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA', 'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA', 'Maine': 'ME', 'Maryland': 'MD', 'Massachusetts': 'MA', 'Michigan': 'MI', 'Minnesota': 'MN', 'Mississippi': 'MS', 'Missouri': 'MO', 'Montana': 'MT', 'Nebraska': 'NE', 'Nevada': 'NV', 'New Hampshire': 'NH', 'New Jersey': 'NJ', 'New Mexico': 'NM', 'New York': 'NY', 'North Carolina': 'NC', 'North Dakota': 'ND', 'Ohio': 'OH', 'Oklahoma': 'OK', 'Oregon': 'OR', 'Pennsylvania': 'PA', 'Rhode Island': 'RI', 'South Carolina': 'SC', 'South Dakota': 'SD', 'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT', 'Vermont': 'VT', 'Virginia': 'VA', 'Washington': 'WA', 'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY'}\n",
"{'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois':...