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