Question

def comp (n, pred): Uses a one line list comprehension to return list of the first n integers (0...n-1) which satisfy the

Can you also explain the functions and what they do please? Thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def comp(n, pred):
    # Initializing result list with empty list
    result = []

    # loop through values from 0 to n-1
    for i in range(n):
        # Checking i is satisfied for pred
        if(pred(i)):
            # If not satisfied then adding it to result list
            result.append(i)

    # Returning result
    return result


# Testing
print(comp(7, lambda x:x%2==0))

Output

[0, 2, 4, 6]

Note Please comment below if you have any doubts. upvote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Can you also explain the functions and what they do please? Thanks! def comp (n, pred):...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you also explain the functions and what they do please? def flatten(1st): ""Takes a nested...

    Can you also explain the functions and what they do please? def flatten(1st): ""Takes a nested list and "flattens" it. >flatten([1, 2, 3]) [1, 2, 3] >>> x = [1, [2, 3], 4] >>> flatten (x) [1, 2, 3, 4] > x = [[1, [1, 1]], 1, [1, 1]] >>> flatten(x) [1, 1, 1, 1, 1, 1] >>> lst = [1, [[2], 3], 4, [5, 6]] >>> flatten (1st) [1, 2, 3, 4, 5, 6] пии "*** YOUR CODE HERE...

  • PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give...

    PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define new functions or helper...

  • can you finish implementing the functions below ********************************************************************************************************** import urllib.request from typing import List, TextIO #...

    can you finish implementing the functions below ********************************************************************************************************** import urllib.request from typing import List, TextIO # Precondition for all functions in this module: Each line of the url # file contains the average monthly temperatures for a year (separated # by spaces) starting with January. The file must also have 3 header # lines. DATA_URL = 'http://robjhyndman.com/tsdldata/data/cryer2.dat' def open_temperature_file(url: str) -> TextIO: '''Open the specified url, read past the three-line header, and return the open file. ''' ... def avg_temp_march(f:...

  • can someone please tell me where did I do wrong and how I can fix this...

    can someone please tell me where did I do wrong and how I can fix this TU. . IVIL main.py Load default template... 1 # Read a list of strings from the user' 3 def square(x): return x*x nmin on input_string = input() 7 numbers= 11 8 for i in (str(input_string): numbers.append(i*2) 10 # Use list comprehension to cast them to ints 11 numbers = [int(i) for i in input_string.split()] 12 result = list (map(lambda x: x*x, numbers)) 13 for...

  • PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP!...

    PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP! THANK YOU! RECURSIVE! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define...

  • Can someone please help me with these questions? There is one example given. The solution should...

    Can someone please help me with these questions? There is one example given. The solution should be like that. Necessary lines of codes which can run in dr.Racket. thank you!! Here is an example of using the llisting package to display code. Check the preamble to see where this is imported and set up. After that you will see a sample interaction from the DrRacket console to show how this function could be tested. You should provide similar listings and...

  • Can someone explain these three questions for me? Thanks. Answers are provided Use the code segment...

    Can someone explain these three questions for me? Thanks. Answers are provided Use the code segment below for problems 6-7 int x = 1; int y - 0 int z 1: if(! (x && y) ) x 0; İf ( ! (x 11 y) ) y+= (x + y) % 2 == 0 ? 1 : 0; printf ("Total #1 : %d\n", x + y + z); printf ("Total #2: %d\n", !x + !y + !2); 6. Which of the...

  • Could you also please leave notes so I can follow what you did. Thanks With the...

    Could you also please leave notes so I can follow what you did. Thanks With the same DLLNode definition as in the previous problem, implement a method to reverse the sequence of items in a OLL, bur code should NOT creste any new nodes- it should simply the original nodes. The method should return the front of the resulting list. public static DLLNode reverse(DLLNode front) COMPLETE THIS METHOD

  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree:    class treeNode: def __init__(self, value, lchild, rchild):...

    COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree:    class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild def __init__(self): self.treeRoot = None    #utility functions for constructTree def mask(self, s): nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked) def isNumber(self, expr): mys=s.strip() if len(mys)==0 or not isinstance(mys, str): print("type mismatch error: isNumber")...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT