Question

In: Computer Science

For Each question the starter code is listed below Make shure complete in simple Python 1)Write...

For Each question the starter code is listed below Make shure complete in simple Python

1)Write a function that removes all occurrences of a given letter from a string.
def remove_letter(theLetter, theStri


2)Write a function that reverses its string argument.
def reverse(astring):

3)Write a function that implements a substitution cipher. In a substitution cipher one letter is substituted for another to garble the message. For example A -> Q, B -> T, C -> G etc. Your function should take two parameters, the message you want to encrypt, and a string that represents the mapping of the 26 letters in the alphabet. Your function should return a string that is the encrypted version of the message.

def encrypt(message, cipher):

4)Write a function sum_of_squares(xs) that computes the sum of the squares of the numbers in the list xs.
For example, sum_of_squares([2, 3, 4]) should return 4 + 9 + 16 which is 29.
def sum_of_squares(xs):
# your code here


5)Create a list containing 100 random integers between 0 and 1000 (use iteration, append, and the random module). Write a function called average that will take the list as a parameter and return the average. Then print the average out, calling your function.


6)The function sumEven should return the sum of only the even numbers contained in the list, lst.
Example
list_of_nums = [1, 5, 4, 8, 5, 3, 2]
x = sum_evens(list_of_nums)
print(x) #prints 14
The function unique_count returns the number of unique items in its parameter, lst, which is a list.
For example, if given the list [3, 3, 3, 5, 3], unique_count would return 2, because there are only two unique items in the list: 3 and 5.
def unique_count(lst):




Solutions

Expert Solution

1)

# Defines a function to remove the parameter letter in theLetter
# from the given parameter string theStri
# returns the result string
def remove_letter(theLetter, theStri):
# To store the result
result = ""

# Loops till end of the string
for c in range(0, len(theStri)):
# Checks if current character of the string is not equals to
# letter stored in theLetter
if(theStri[c] != theLetter):
# Concatenate the current character with result
result += theStri[c]
# Returns the result
return result

# Creates a string
theStri = "This is demo"
# Creates a letter to remove
theLetter = 'i'
# Displays the original string
print("Before remove string: ", theStri)

# Calls the function to remove the character from the string
# and displays the return result string
print("After remove string: ", remove_letter(theLetter, theStri))

Sample Output:

Before remove string: This is demo
After remove string: Ths s demo

---------------------------------------------------------------------------------------------------------

2)

# Defines a function that reverses its string argument.
def reverse(astring):
# To store the reverse string
revStr = ""
# Loops from length minus one position fo the string to
# beginning of the string
for c in range(len(astring)-1, -1, -1):
# Concatenate the current character with result
revStr += astring[c]
# Returns the reverse string
return revStr

# Creates a string
astring = "This is demo."

# Displays the original string
print("Original string: ", astring)

# Calls the function to reverse the string
# and displays the return reverse string
print("After reverse : ", reverse(astring))

Sample Output:

Original string: This is demo.
After reverse : .omed si sihT

-----------------------------------------------------------------------------------------------------

3)

# Defines a function to implement substitution cipher
def encrypt(message, cipher):
# To store encrypted message
encryptStr = ""
# Converts the message to upper case
message = message.upper()
# Loops till end of the original message
for c in range(0, len(message)):
# Loops 26 times for 26 alphabets
for letter in range(0, 26):
# Checks if the current character is between 'A' - 'Z'
if(ord(message[c]) >= 65 and ord(message[c]) <= 90):
# Checks if current character is equals to the
# 9(letter + 65) for character form of 'A' - 'Z')
if(message[c] == chr(letter+65)):
# Concatenates the letter index position of
# cipher text to encrypt string
encryptStr += cipher[letter]
# Come out of the loop
break
# Otherwise current character is not a letter
else:
# Concatenates the current character of message
# to encrypted string
encryptStr += message[c]
# Come out of the loop
break
# Returns the encrypted message
return encryptStr
  
# Creates a string message
message = "This is demo"
# Creates a cipher string
cipher = "cdefghijklmnopqrstuvwxyzab"

# Displays the original string
print("Original string: ", message)

# Calls the function to encrypt the string
# and displays the return encrypted string
print("After remove string: ", encrypt(message, cipher))

Sample Output:

Original string: This is demo
After remove string: vjku ku fgoq

---------------------------------------------------------------------------------------------------------------

4)

# Function to computes the sum of the squares of the numbers in the list xs
def sum_of_squares(xs):
# To store total
total = 0
# Loops till end of the list
for c in range(0, len(xs)):
# Calculates square and then adds it to total
total += (xs[c] * xs[c])
# Displays the total
print("Sum of the square: ", total)

# Calls the function to calculate total
sum_of_squares([2, 3, 4])

Sample Output:

Sum of the square: 29

--------------------------------------------------------------------------------------------------

5)

# Function to calculate sum of only the even numbers contained in the list, lst.
def sum_evens(list_of_nums):
# To store total
total = 0
# Loops till end of the list
for c in range(0, len(list_of_nums)):
# Checks if current index position number is divisible by 2
# then even number
if(list_of_nums[c] % 2 == 0):
# Calculates square and then adds it to total
total += list_of_nums[c]
# Returns total
return total

# Creates a list
list_of_nums = [1, 5, 4, 8, 5, 3, 2]
# Calls the function calculate sum of even numbers
# Stores the return sum in x
x = sum_evens(list_of_nums)

# Displays total
print("Sum of even numbers: ", x)

Sample Output:

Sum of even numbers: 14


Related Solutions

please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
In simple python code 1) Write a function to convert the image to grayscale. 2Write a...
In simple python code 1) Write a function to convert the image to grayscale. 2Write a function to convert an image to black and white 3)Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows: newR = (R × 0.393 + G × 0.769 + B × 0.189) newG = (R × 0.349 + G × 0.686 + B × 0.168) newB = (R ×...
Fill-In-The-Blanks 13. Complete the code by filling in the blanks for each question below. (Length of...
Fill-In-The-Blanks 13. Complete the code by filling in the blanks for each question below. (Length of blank doesn’t matter.) a) Print out the contents of the array airlines. #include #include ________________ using namespace std; int main () { string airlines[] = {"american","pan-am","southwest"}; for (int i = 0;_______________;i++) cout << ____________ << endl; return 0; } b) Read in 5 integers from the user and write them back out in reverse order int numbers[5]; for(int i = 0; ___________; _________) cin...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Write a simple matching coefficient and jaccard similarity code in python. For a example x =...
Write a simple matching coefficient and jaccard similarity code in python. For a example x = 10101 and y = 00101 what is the code to check those similarities in python?
Complete this in simple python code. Sepia Tone images are those brownish colored images that may...
Complete this in simple python code. Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows::: ............................................................................. newR = (R × 0.393 + G × 0.769 + B × 0.189) newG = (R × 0.349 + G × 0.686 + B × 0.168) newB = (R × 0.272 + G × 0.534 + B × 0.131) ............................................................................. Write a function to convert an image to...
This question is about the Balance of Payments Accounts. Record each transaction listed below. Make sure...
This question is about the Balance of Payments Accounts. Record each transaction listed below. Make sure to separate the current and the financial accounts. a. The export of wine from California to France for $100 paid for with US dollars that the French importer holds at home in a box. b. The import of a BWM for $200 (it’s used) from Germany paid for with euros that the American importer held in bank account in Frankfurt. c. What is the...
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”....
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”. It takes one or more files and displays requested number of lines from the ending. If number is not specified, then it print 10 lines by default.
Using steps.py in this repository for a starter file, write a Python program to calculate average...
Using steps.py in this repository for a starter file, write a Python program to calculate average number of steps per month for a year. The input file (which you will read from the command line, see the sample program on how to read command line arguments in this repository) contains the number of steps (integer) a person took each day for 1 year, starting January 1. Each line of input contains a single number. Assume this is NOT a leap...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT