Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Set 3 Note: Use the variable names defined in the instructions for each problem. Failure to do so will result in a loss of

Problem Set 3
Note:
Use the variable names defined in the instructions for each problem. Failure to do so will result in a loss of points.
You need to use code to arrive at a solution. Hard coding a solution results in 0 points for the problem.
Use only syntax covered in the Units so far. Using any other syntax results in 0 points for the problem.
Problem 1
In the cell below, write code that iterates over the string myStr and does the following:
Print each character.
Count the number of characters and stores the result the variable cntChars. Do not use any functions to count.
Print the variable myStr and cntChars with an appropriate label as shown below in Expected Output.
myStr = "MICHIGAN"
# Write your code below.
myStr = "MICHIGAN"
# Write your code below.
Expected Output:
M
I
C
H
I
G
A
N
The number of characters in MICHIGAN is 8
Problem 2
In the cell below, write one loop that iterates over the list mixedList and does the following:
Prints each element and the element type.
Count the number of elements and store the result in the variable cntList. Do not use any function to count.
Print cntList preceded by an appropriate label as shown below.
mixedList =["hello",2,4,6.0,7.5,234352354, "the end", "",99]
# Write your code below.
Expected Output:
hello
2< class 'int'>
4< class 'int'>
6.0< class 'float'>
7.5< class 'float'>
234352354< class 'int'>
the end < class 'str'>
< class 'str'>
99< class 'int'>
The number of elements in the list is 9
Problem 3
Write code that iterates over to the string sentence and does the following:
Count the number of characters using the variable numChars.
Count the number of vowels using the variable numVowels. A character is a vowel if it is a member of the tuple vowels.
Print the variables numChars and numVowels preceded by appropriate labels as shown below.
Do NOT use the a function to solve the problem. You may use the len function on the string to confirm the your answer is correct.
sentence = "The quick brown rhino jumped over the extremely lazy fox."
vowels =('a','e','i','o','u')
# Write your code below.
Expected Output:
The number of characters is 57
The number of vowels is 16
Problem 4
Write code in the cell below that does the following:
Create a list called words from the words in the string sentence.
Iterate over words
Add to a list called wordLens which contains the length of the current word.
Count the current word using the variable numWords. Do not use a function.
Print the word and the word length.
Print numWords as shown in expected output.
Print the list wordLens.
You need one loop to solve this problem. You may use the len and split functions.
sentence = "The quick brown rhino jumped over the extremely lazy fox"
# Write your code below.
Expected Output:
The 3
quick 5
brown 5
rhino 5
jumped 6
over 4
the 3
extremely 9
lazy 4
fox 3
Number of words is 10
[3,5,5,5,6,4,3,9,4,3]
Problem 5
The string addStr contains list of numbers separated by the plus sign. Write code that uses an accumulation pattern to take the sum of all numbers and assigns it to the variable sumVal. Print the variable sumVal prefixed by an appropriate label.
You may use the split function to split by "+" and int function to cast to an integer.
addStr ="2+5+10+20+15+37"
# Write your code below.
Expected Output:
The total of all numbers is 89
Problem 6
The string weekTemps contains a comma-separated list of fahrenheit temperatures. Write code that does the following:
Finds the minimum temperature in fahrenheit and stores the result in minTempF.
Computes the average temperature in fahrenheit and stores the result in avgTempF.
Convert the minimum and average temperature in fahrenheit to celsius and stores the results in minTempC and avgTempC respectively. Do not hard code your answer. This means that your code must compute both the total of all temperatures and the number of temperatures in weekTemps.
Do the following:
Split the string on ',' and store the resultant list in a variable
Iterate over that variable:
Apply a count pattern to count the number of temps.
Apply an accumulation pattern to total the temperature values.
if the current temperature is smaller than minTempF:
Replace the contents of minTempF with the current temperature.
Compute the average temperature and store the result in avgTempF.
Print avgTempF and minTempF as shown in the expected output. Note, you will do some additional work to produce the expected output.
Convert avgTempF to celsius and store the result in avgTempC.
Convert minTempF to celsius and store the result in minTempC.
Print both variables as shown in the expected output.
The average temperature is defined as the total of all temperatures divided by the number of temperatures.
To convert from fahrenheit: temperature in celsius =(temperature in fahrenheit -32)*5/9.
You

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

f. How do you apply for the position?

Answered: 1 week ago

Question

=+Explain the skills needed to create a sustainable personal bran

Answered: 1 week ago