The purpose of this lab is to practice working with Python's higher order functions and ternary operator. Write all of your code in one file named lab2.py. You can write the code for items 1 through 6 directly inside the main function.
Let a be the list of values produced by list(range(1, 11)).
Explanation::
Code in PYTHON is given below
Output is given at the end of the code
Code in PYTHON:
import functools
def evenFilter(data):
values = []
for key in data.keys():
if key%2==0:
values.append(data[key])
return values
def findMin(x,y):
min = x if x < y else y
return min
def main():
a = list(range(1,11))
print(a)
#Part 1
m = list(map(lambda x: x**3 , a))
print(m)
#Part 2
f1 = list(filter(lambda x: (x%3 == 0) , a))
print(f1)
#Part 3
r1 = functools.reduce(lambda x, y: '{0}{1}'.format(x, y),a)
print(r1)
#Part 4
listComp = [x**3 for x in a ]
print(listComp)
#Part 5
divisible = [x for x in a if x%3==0]
print(divisible)
#Part 6
divisibleCube = [x for x in listComp if x%3==0]
print(divisibleCube)
print("Testing evenFilter method:")
data = {1:"one",3:"three",4:"four",5:"five",8:"eight",10:"ten"}
print(evenFilter(data))
print("\nTest findMin() method on 10 and 45")
print(findMin(10,45))
main()
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[3, 6, 9]
12345678910
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[3, 6, 9]
[27, 216, 729]
Testing evenFilter method:
['four', 'eight', 'ten']
Test findMin() method on 10 and 45
10
Please provide the feedback!!
Thank You!!
The purpose of this lab is to practice working with Python's higher order functions and ternary...
1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...
In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...
python 3
Lab Activity 4.4: Using Lambda Functions Using Lambda Functions الا </> Write a lambda function that takes in two numerical values and returns the first value, raised to the power of the second value: Steps for Completion 1. Create a lambda function that takes in number and power and returns the value of the number raised to power. 2. Assign it to a variable called exponential. 3. Print the exponential variable. Using the numberical values 2 and 6,...
C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases. Your function should be named calculator Your function takes three input parameter: two double numbers and one char operator Your function does not return anything Your function prints answer in the format specified below Your function should set precision point to 2 Note: You must use...
NOTE: USE PYTHON CS160 Computer Science Lab 14 Working with lists, functions, and files Objective: Work with lists Work with lists in functions Work with files Assignment: Part 1: Write a program to create a text file which contains a sequence of test scores. Ask for scores until the user enters an empty value. This will require you to have the scores until the user enters -1. After the scores have been entered, ask the user to enter a file...
Functionality to build (4 functions) read_csv_header Define a function named read_csv_header with one parameter. This parameter will be a csv reader object (e.g., the value returned when calling the function csv.reader). The first row read using the parameter is the file's header row. The header row contains the keys for the data stored in the CSV file. This function must return a list containing the strings from that header row. The function parameter will be a csv reader object and...
ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a value and print a result to the console, to test you must call the function, run it and visually inspect the result for correctness. With functions that return a value, use the print_test function to provide feedback of the test results at the command line. The print_test function is implemented for you at the bottom of this file. RECALL: floating point arithmetic can lose...
40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try: print(int(answer) ) answer input ( " Enter age: ") except Value E rror : print ("Invalid answer 'q' age") Show what is printed if the user enters the following values at the prompt, one at a time. Print output User input 20 17 2a => 29 q 9. The following main function works with a class called Student that you will write. def main()...
write Java statements that perform the
following functions ?
21 If x is greater than or equal zero, then assign the square root of x to variable sqrtx and print out the result Otherwise, print out an error message about the argument of the square root function and set sqrtx to zero (2) 22 A variable fun is calculated by dividing variable numerator by variable denominator If the absolute value of denominator is less than 1, write "Divide by zero...
Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...