Question

In: Computer Science

Using python, please complete these 4 exercises. Please limit your code to these skills: For loop...

Using python, please complete these 4 exercises. Please limit your code to these skills:

For loop

While loop

Input function

F strings

Tuples

Lists

Nested ifs

Elias

Exercise 1

Using the following list, do the following:

  1. Sort it in ascending order
  2. In a print statement, referencing the list and its index numbers, print the lowest value and the highest value.

[15, 70, 15, 38, 49, 98, 62, 89, 2, 21, 40, 74, 36, 36, 65, 1, 55, 16, 24, 56]

Exercise 2 (1 Point):

Using the following list, do the following:

1. Iterate through each element in the list

2. Print out whether the element is positive or negative or zero on a new line for each element.

[-2, 1, -2, 7, -8, -5, 5, 10, -6, 7]

Exercise 3 (2 Points):

Create a new list using the range function, appending the values from the range function's output into the list.

The range function must:

  1. Start at 1
  2. Stop at 50
  3. Skip every other number

Hint: Use either a for loop or a list comprehension

Exercise 4 (6 Points):

In this exercise, you will be required to do the following:

Take the following phrase: The only thing we have to fear is fear itself

  1. Convert it to a list
  2. Inspect each word in the list
    1. If the word in the list starts with a vowel (excluding y), then:
      1. add the letters "way" to the end of the word.
      2. EX: the word "and" would become "andway"
    2. If the word in the list does not start with a vowel (including y), then:
      1. take the first letter from the word and move it to the end of the word
      2. add to the end of the word, the letters "ay"
      3. EX: the word "money" would become "oneymay"
  3. Append each modified word to a new list
  4. convert the list to a string and print that string out
    1. HINT: use join()

So, the end result should be:

Hetay onlyway hingtay eway avehay otay earfay isway earfay itselfway

Solutions

Expert Solution

EXERCISE 1:

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:

Below is the code to copy:
#CODE STARTS HERE----------------
#Exercise 1:
def sort(li): #Sort the list using bubble sort
   li_len = len(li) #Calculate list length
   for x in range(li_len-1): #loop 1
      for y in range(0,li_len-x-1): #loop 2
         if li[y] > li[y + 1]: #Check if second element is greater than 1st
            li[y], li[y + 1] = li[y + 1], li[y] #swap elements

inp_list = [15, 70, 15, 38, 49, 98, 62, 89, 2, 21, 40, 74, 36, 36, 65, 1, 55, 16, 24, 56]
sort(inp_list) #Function call
print("Lowest value: ", inp_list[0])
print("Highest value: ", inp_list[-1]) #-1 references the last element
#CODE ENDS HERE------------------

EXERCISE 2:

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:


Below is the code to copy:
#CODE STARTS HERE----------------
#EXERCISE 2:
li = [-2, 1, -2, 7, -8, -5, 5, 10, -6, 7]
for element in li: #loop through the elements
   if element == 0: #Checks for 0
      print("Zero")
   elif element <0: #Checks for negative number
      print("Negative")
   else: #Positive number
      print("Positive")
#CODE ENDS HERE------------------

EXERCISE 3:

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:

Below is the code to copy:
#CODE STARTS HERE----------------
#EXERCISE 3:
new_list = []
# creates list of number from 1 to 50 with step size of 2
for num in range(1,51,2):
   new_list.append(num) #Add numbers to new list
print(new_list) #Print new list
#CODE ENDS HERE------------------

EXERCISE 4:

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:

Below is the code to copy:
#CODE STARTS HERE----------------
#EXERCISE 4:
phrase = "The only thing we have to fear is fear itself"
phrase_list = phrase.split() #This converts string to list of words
new_list = [] #Used to store new words
for word in phrase_list: #loop through the list
   if word[0].lower() in ['a','e','i','o','u']: #Checks if word starts with vowels
      word+="way" #update 'way' at the end
   else: #not vowel
      word = word[1:]+word[0]+"ay" # move first letter to last and add 'ay' to the end
   new_list.append(word.lower()) #Add every word to a new list
new_phrase = " ".join(new_list) #Convert it into string
print(new_phrase) #print result
#CODE ENDS HERE------------------

Related Solutions

Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Exercises Code of Conduct Exercises Instructions:  Answer the following in complete sentences using the AICPA's revised Code...
Exercises Code of Conduct Exercises Instructions:  Answer the following in complete sentences using the AICPA's revised Code of Conduct, providing the ET references for each of your responses. For questions with multiple parts, include multiple ET references as appropriate. What are the three broad categories of safeguards identified in Part 1 of the Code, in the Conceptual Framework for members in public practice? Which category of safeguard cannot be relied upon, by itself, to reduce threats to an acceptable level?
Create a python code that calculates fixed point iteration method using a for loop.
Create a python code that calculates fixed point iteration method using a for loop.
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
Please complete the following list of assignments and upload using this link. Please limit the number...
Please complete the following list of assignments and upload using this link. Please limit the number of files you create to a max of 2, one excel and one word document. P 1-1 FASB Statement of Financial Accounting Concepts No. 2 indicates several qualitative characteristics of useful accounting information. Following is a list of some of these qualities, as well as a list of statements and phrases describing the qualities. a. Benefits > costs b. Decision usefulness c. Relevance d....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT