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 function that takes a list of integers as input and returns a list with...
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 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.
Write a Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
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 Scheme function that takes two integers and returns the list of all integer numbers...
Write a Scheme function that takes two integers and returns the list of all integer numbers between these two integers (inclusively) in increasing order. (numbers 10 20) (10 11 12 13 14 15 16 17 18 19 20) Please explain every step.
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
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
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 *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT