In: Computer Science
1-Please complete the below python programs. Use spaces for indentations since the website does not take tabs.
#inputs to the function are 2 numbers x and y. Print "Sum 6!" if the sum of x and y is bigger or equal to 6. Print "Sum too small!" otherwise.
def sum_6(x,y):
#your answer here, can be multiple lines
___
#input to the function is a list of integers. The function will calculate and return a resultlist with the square of each number in the input list saved in it. For example: if mylist has numbers 1,2,3,4,5, the returned value should be a list with 1,4,9,16,25.
def square_num (mylist):
#your answer here, can be multiple lines
___
#in this main function, create a list with 6 numbers, call the second function in this question, print out the returned result.
def main ():
#your answer here, can be multiple lines
___
2-Write down the regular expression that can match: a string with 1 or more digits followed by exactly 2 small case letters. Sample string: 5678yr. (You do not need to consider the " ")
(same as the below question) Make sure you only write down the regex. Please do NOT include other things like / / around it. You do not need to consider any blanks before or after the regex, nor sentence starting/ending symbols.
Answer:
3-Write down the regular expression that can match: a float with 1 or more digits before the decimal point and exactly 2 digits after. Sample: 2345.34
(same as the below question) Make sure you only write down the regex. Please do NOT include other things like / / around it. You do not need to consider any blanks before or after the regex, nor sentence starting/ending symbols.
Answer:
#QUESTION 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----------------
# 1 a. def sum_6(x,y): #Function sum 6 if x+y>=6: #Find if sum is greater than or equal to print("Sum 6!") else: print("Sum too small!") sum_6(5,1) #Function called here #1 b. def square_num(mylist): new_list = [] #New list to store squared numbers for num in mylist: #Loop through the list new_list.append(num**2) #Store squared numbers in new_list return new_list #Return new list #1 c. def main(): #main function li = [1,2,3,4,5,6] #List created with 6 numbers print(square_num(li)) #Call square_num function and print returned result main() #Main function call #CODE ENDS HERE------------------
QUESTION 2:
REGULAR EXP: \d+[a-z]{2}
EXAMPLE:
MATCHES:
DOES NOT MATCH:
QUESTION 3:
REGULAR EXP: \d+\.\d{2}
EXAMPLE:
MATCHES:
DOES NOT MATCH: