In: Computer Science
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