Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Created July 18, 2019 19:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cibofdevs/b3a02e3e3d9280018e066f0c5acfa637 to your computer and use it in GitHub Desktop.
Save cibofdevs/b3a02e3e3d9280018e066f0c5acfa637 to your computer and use it in GitHub Desktop.
# 1. Create a function called mult that has two parameters, the first is required and should be an integer,
# the second is an optional parameter that can either be a number or a string but whose default is 6.
# The function should return the first parameter multiplied by the second.
def mult(a, b=6):
return a * b
# 2. The following function, greeting, does not work. Please fix the code so that it runs without error.
# This only requires one change in the definition of the function.
def greeting(name, greeting="Hello ", excl="!"):
return greeting + name + excl
print(greeting("Bob"))
print(greeting(""))
print(greeting("Bob", excl="!!!"))
# 3. Below is a function, sum, that does not work. Change the function definition so the code works.
# The function should still have a required parameter, intx, and an optional parameter, intz with a defualt value of 5.
def sum(intx, intz=5):
return intz + intx
# 4. Write a function, test, that takes in three parameters: a required integer,
# an optional boolean whose default value is True, and an optional dictionary,
# called dict1, whose default value is {2:3, 4:5, 6:8}. If the boolean parameter is True,
# the function should test to see if the integer is a key in the dictionary.
# The value of that key should then be returned. If the boolean parameter is False, return the boolean value “False”.
def test(x, abool = True, dict1 = {2:3, 4:5, 6:8}):
return abool and dict1.get(x, False)
# 5. Write a function called checkingIfIn that takes three parameters.
# The first is a required parameter, which should be a string.
# The second is an optional parameter called direction with a default value of True.
# The third is an optional parameter called d that has a default value of
# {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}.
# Write the function checkingIfIn so that when the second parameter is True,
# it checks to see if the first parameter is a key in the third parameter;
# if it is, return True, otherwise return False.
# But if the second paramter is False, then the function should check to see if the first parameter is not a key of the third.'
# If it’s not, the function should return True in this case, and if it is, it should return False.
def checkingIfIn(a, direction = True, d = {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}):
if direction == True:
if a in d:
return True
else:
return False
else:
if a not in d:
return True
else:
return False
# 6. We have provided the function checkingIfIn such that if the first input parameter is in the third,
# dictionary, input parameter, then the function returns that value, and otherwise, it returns False.
# Follow the instructions in the active code window for specific variable assignmemts.
def checkingIfIn(a, direction = True, d = {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}):
if direction == True:
if a in d:
return d[a]
else:
return False
else:
if a not in d:
return True
else:
return d[a]
# Call the function so that it returns False and assign that function call to the variable c_false
c_false = checkingIfIn('peas')
# Call the fucntion so that it returns True and assign it to the variable c_true
c_true = checkingIfIn('apples', False, {'carrots': 1, 'peas': 9, 'potatos': 8, 'corn': 32, 'beans': 1})
# Call the function so that the value of fruit is assigned to the variable fruit_ans
fruit_ans= checkingIfIn('fruit')
# Call the function using the first and third parameter so that the value 8 is assigned to the variable param_check
param_check = checkingIfIn('potatos', False, {'carrots': 1, 'peas': 9, 'potatos': 8, 'corn': 32, 'beans': 1})
@raviprakashram
Copy link

1.

def mult(a, b=6):
return a * b

@sadmansakib37
Copy link

sadmansakib37 commented Jun 21, 2020

4.

def test(x,y=True,dict1={2:3,4:5,6:8}):
if y is True:
if x in dict1.keys():
return dict1[x]
else:
print('not present')
else:
return y
print(test(6))

@Joel598
Copy link

Joel598 commented Jul 30, 2020

Write a function, test, that takes in three parameters: a required integer, an optional boolean whose default value is True, and an optional dictionary, called dict1, whose default value is {2:3, 4:5, 6:8}. If the boolean parameter is True, the function should test to see if the integer is a key in the dictionary. The value of that key should then be returned. If the boolean parameter is False, return the boolean value “False”.

this can be also written as:

def test(x, y= True, dict1={2:3, 4:5, 6:8}):
if y== True:
return dict1[x]
return False

@sauravsagar19
Copy link

5 def checkingIfIn (str1,direction=True,d={'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}):
if direction is True:
if str1 in d.keys():
return True
else :
return False
else:
if str1 not in d.keys():
return True
else:
return False

print(checkingIfIn("saurav"))
print(checkingIfIn("apple"))

@hadrocodium
Copy link

hadrocodium commented Oct 23, 2021

5.

"""
Write a function called checkingIfIn that takes three parameters.
The first is a required parameter, which should be a string.
The second is an optional parameter called direction with a default value of True.
The third is an optional parameter called d that has a default value of {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}.

Write the function checkingIfIn so that when the second parameter is True, it checks to see if the first parameter is a key in the third parameter; if it is, return True, otherwise return False.

But if the second paramter is False, then the function should check to see if the first parameter is not a key of the third. If it’s not, the function should return True in this case, and if it is, it should return False.
"""

def checkingIfIn(a, direction = True, d = {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}):
    if direction:
        return bool(d.get(a, False)) and bool(d[a])
    elif not direction:
        return not bool(d.get(a, False))

@nadim-ha
Copy link

def checkingIfIn(a,direction = True,d = {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}):
    if direction == True:
        if a in d :
            return(True)
        else:
            return(False)
    else:
        if a not in d:
            return(True)
        else:
            return(False)
         

@anubhav47
Copy link

Write a function that takes the following arguments: (1) a name, (2) a value, and (3) and optional dictionary. The function adds the value to the dictionary using the name as a key in the dictionary

@nkendra90
Copy link

Create a function named get_count that:

Has two parameters:

data_list: a list of numeric values.
update_count: an optional parameter with a default value of False.
If update_count is True, this function should set the global count variable to the count of the input list data_list.

Returns the number of items of the data_list parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment