In: Computer Science
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:
[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:
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
So, the end result should be:
Hetay onlyway hingtay eway avehay otay earfay isway earfay itselfway
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------------------