Question

In: Computer Science

1.Write a function div7(lst) which takes in a list of integers, and returns a list of...

1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long.

Here is some examples:

>>> div7([14, 5, 7, 3, 29, 28, 10])

[True, False, True, False, False, True, False]

2. Write a invert function.This function performs a color inversion, as though you were producing a negative of a photo. Recall that each color component in every pixel is an integer between 0 and 255, inclusive. In every pixel, for each of the three color components, if the original value for the component was x, to invert the color you must set it to 255-x.
Examples:
>>> invert([[[127, 127, 127], [0, 0, 0]],
[[255, 255, 0], [50, 128, 255]],
[[0, 0, 255], [0, 255, 0]],
[[255, 0, 0], [255, 255, 255]]])

[[[[128, 128, 128], [255, 255, 255]],
[[0, 0, 255], [205, 127, 0]],
[[255, 255, 0], [255, 0, 255]],
[0, 255, 255], [0, 0, 0]]]

(note that the highlighting and the new line for each row are for clarity purposes only and will not be present in the actual console output, but the numbers should be correct)

Both need to use python.

Solutions

Expert Solution

(1) Code -

def div7(lst):
res=[True if i%7==0 else False for i in lst]
  
return res

div7([14, 5, 7, 3, 29, 28, 10])

Explanation -

1. Define a function named div7 which accepts lst as parameter.

2.Use list comprehension which displays True if the element i in the list lst returns remainder 0 on division with 7 else display false.

Perfom above action for every element in the list using for loop(for i in lst)

Use mod operator(%) to get the remainder when element in list is divided by 7.

4.This function will return res when called by pasisng the argument as the list of numbers.

(2) Code -

import numpy as np
def invert(lst):
res=[[[255-k for i in lst for j in i for k in j]]]
res1=np.reshape(res,(4,2,3))
return res1

invert([[[127, 127, 127], [0, 0, 0]],
[[255, 255, 0], [50, 128, 255]],
[[0, 0, 255], [0, 255, 0]],
[[255, 0, 0], [255, 255, 255]]])

1.import numpy module.

2.define a function name invert which accepts single paramter lst

3.Since its a 3d list, we need to Iterate thrice to access element in the nested lists.

Hence used below 3 for loops to iterate through the lst using list comprehension

for i in lst , we get 4 items(i)

[[127, 127, 127], [0, 0, 0]]
[[255, 255, 0], [50, 128, 255]]
[[0, 0, 255], [0, 255, 0]]
[[255, 0, 0], [255, 255, 255]]

for j in i ,we get 8  j -

[127, 127, 127]
[0, 0, 0]
[255, 255, 0]
[50, 128, 255]
[0, 0, 255]
[0, 255, 0]
[255, 0, 0]
[255, 255, 255]

for k in j , we get below k values -

127
127
127
0
0
0
255
255
0
50
128
255
0
0
255
0
255
0
255
0
0
255
255
255

Then display 255-k , After using list comprehension -

[[[128,
   128,
   128,
   255,
   255,
   255,
   0,
   0,
   255,
   205,
   127,
   0,
   255,
   255,
   0,
   255,
   0,
   255,
   0,
   255,
   255,
   0,
   0,
   0]]]

4.Now convert this 1-d list into 3-d list, Use numpy.reshape function and pass col1,col2,row value .

Here col1=4,col2=3,row=3.

Hence use res1=np.reshape(res,(4,2,3))

Finally this function will return res1 when called by passing the arguments to the function.


Related Solutions

Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
SML Complete the following programs. 1. Write a function listify that takes a list and returns...
SML Complete the following programs. 1. Write a function listify that takes a list and returns a list of lists where each element of first list becoming its own single-element list (Fill in the code here) fun main() = let val lst = (explode "rad"); in print (PolyML.makestring ( listify lst )) end; 2. Write a function splitlist that takes a list of pairs and returns a pair of lists that has the firsts of the pairs in one list...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT