Question

In: Computer Science

Please break down this python code into steps on how to do it. def membership(sequence, value):...

Please break down this python code into steps on how to do it.

def membership(sequence, value):

if value in sequence:
return value
if [value] in sequence:
return [value]
if [[value]] in sequence:
return [[value]]

a_list = ['a', ['a'], [['a']]]
print(membership(a_list, 'a'))

Solutions

Expert Solution

Hi, I hope you are doing well.

This is the original code:

def membership(sequence, value): #declaring function
    if value in sequence: #checking condition and if value found return value
        return value
    if [value] in sequence:#checking condition and if value found return [value]
        return [value]
    if [[value]] in sequence:#checking condition and if value found return [[value]]
        return [[value]]

a_list = ['a', ['a'], [['a']]] #creating a list
print(membership(a_list, 'a')) #calling and printing the value of the function

By breaking the code we can check the value that will be printed.

Step 1:

def membership(sequence, value): #declaring function
    if value in sequence: #checking condition and if value found return value
        return value

a_list = ['a', ['a'], [['a']]] #creating a list
print(membership(a_list, 'a')) #calling and printing the value of the function

This is how thw program will execute and gives tha output the value will store 'a'and than check if 'a' is present in sequence where sequence is a_list which is passed as a parameter in membership function.

Step 2:

def membership(sequence, value): #declaring function
    if [value] in sequence:#checking condition and if value found return [value]
        return [value]

a_list = ['a', ['a'], [['a']]] #creating a list
print(membership(a_list, 'a')) #calling and printing the value of the function

This is how thw program will execute and gives tha output the value will store 'a'and than check if ['a'] is present in sequence where sequence is a_list which is passed as a parameter in membership function.

Step 3:

def membership(sequence, value): #declaring function
    if [[value]] in sequence:#checking condition and if value found return [[value]]
        return [[value]]

a_list = ['a', ['a'], [['a']]] #creating a list
print(membership(a_list, 'a')) #calling and printing the value of the function

This is how thw program will execute and gives tha output the value will store 'a'and than check if [['a']] is present in sequence where sequence is a_list which is passed as a parameter in membership function.

This is how the final program will execute and print the output:

NOTE: Here only a is printed because it just checks the first if statement and skips other if statements but if we want to execute the below statements to than we need to change if into elif than it will be executed.


Related Solutions

how do I find the p-value? please break it down barney style
how do I find the p-value? please break it down barney style
Can someone show me how to break this down by steps please and thank you. You...
Can someone show me how to break this down by steps please and thank you. You sell short 200 shares of a stock at $95 on a 60% initial margin requirement with a 30% maintenance margin. In 3 months, the stock is $79. What is your margin at this time? When would a margin call occur? If you cover your position when the stock is $79, what would be your HPR? Assume you did the same trade as before but...
Please based on my python code. do the following steps: thank you Develop a method build_bst...
Please based on my python code. do the following steps: thank you Develop a method build_bst which takes a list of data (example: [6, 2, 4, 22, 34, 9, 6, 67, 42, 55, 70, 120, 99, 200]) and builds a binary search tree full of Node objects appropriately arranged. Test your build_bst with different sets of inputs similar to the example provided above. Show 3 different examples. Choose 1 example data.  Develop a function to remove a node from a bst...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID points method: add points display points Main: create a membership object (a client) give initial points (600) increase the points by (try both 200 and 500)
Please provide Python code that does the following: 2) A palindrome is any sequence of characters...
Please provide Python code that does the following: 2) A palindrome is any sequence of characters that reads the same backwards as forwards. One-word examples include: Dad madam rotor Kayak redder noon For the sake of this exercise, the following may also be considered one-word palindromes: 1881 zap4554paz That is, numeric strings and “nonsense” strings that read the same backwards as forwards should be classified as palindromes. Write a program that takes as input a string and determines if it’s...
how do you code a psuedo-random probability(%) in python? please provide an example.
how do you code a psuedo-random probability(%) in python? please provide an example.
Python class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self,...
Python class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self, value, next=None): """ DO NOT EDIT Initialize a node :param value: value of the node :param next: pointer to the next node in the LinkedList, default is None """ self.value = value # element at the node self.next = next # reference to next node in the LinkedList def __repr__(self): """ DO NOT EDIT String representation of a node :return: string of value """...
python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value):...
python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value): pass    def pop(self): return None    def peek(self): pass    def is_empty(self): pass def stack(self): pass    def exch(self): pass    def index(self): pass    def clear(self): pass    def dup(self): pass    def equal(self): pass    def depth(self): pass    stack = PostScript() def decode(command_list): for object in command_list: if object == '=': stack.equal() elif object == 'count': stack.depth() elif object ==...
please give complete code in python using def function thank you! Validate Credit Card Numbers Credit...
please give complete code in python using def function thank you! Validate Credit Card Numbers Credit card numbers can be quickly validated by the Luhn checksum algorithm. Start at the rightmost digit save one (the last being the checksum digit). Moving left, double the value of every second digit. If the doubled value is greater than nine, then subtract 9 from the doubled value to renormalize within the range 0–9. Sum all the digits including the checksum digit. If this...
In Python The following code is intentionally done in poor style but runs as intended. def...
In Python The following code is intentionally done in poor style but runs as intended. def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False while not e: x = input("Would you like some chikkin?") if x == "Y" or x == "y": c = f(c) else: e = True if c == 0: e = True print("I hope you enjoyed your chikkin!") def f(c): if c > 0: print("Got you some chikkin! Enjoy") return c-1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT