15.6. Exercises¶
-
Fill in the parameter list for g so that the invocations of g yield the return values specified
-
Define a function called
nums
that has three parameters. The first parameter, an integer, should be required. A second parameter namedmult_int
should be optional with a default value of 5. The final parameter,switch
, should also be optional with a default value of False. The function should multiply the two integers together, and if switch is True, should change the sign of the product before returning it.
-
Write a function called
together
that takes three parameters, the first is a required parameter that is a number (integer or float), the second is a required parameter that is a string, and the third is an optional parameter whose default is ” “. What is returned is the first parameter, concatenated with the second, using the third.
15.6.1. Contributed Exercises¶
Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?
Write a function named add_wvu(). It should accept one parameter, an optional parameter named colleges designed to take a list. If an existing list is not provided for colleges when calling your function, your function should create a new list.
Your function must append an entry containing “WVU” to the colleges list, and return the updated list.
Be careful that if add_wvu() is called multiple times, any lists it generates are new and independent from other previously returned lists.
Create a lambda function that is designed to manipulate a dictionary, such as by returning the keys or values. Store the lambda function as lambda_func.
Create a function intadd
that accepts two integers x
and y
. The first integer x
should have a default value of 7 and the second integer y
should have a default value of 3. The function should add the two integers and return this value.
- 'x'
- You may want to check out the docs for isEmpty
- the stack is empty
- There is an odd number of things on the stack but each time through the loop 2 things are popped.
- an error will occur
- Good Job.
- 'z'
- You may want to check out the docs for isEmpty
Q-1: Given the following sequence of stack operations, what is the top item on the stack when the sequence is complete?
m = Stack()
m.push('x')
m.push('y')
m.push('z')
while not m.isEmpty():
m.pop()
m.pop()
Below is a program that simulates the rolling of dice. Modify this code and use it to simulate the 100 rolls of 5, 10 sided dice. Store the average roll in a variable called solution
rounded to the nearest integer.
Problem 1: Write a recursive function count(num, n)
where num
is a positive integer and n
is a integer from 0 to 9.
count
returns the number of times the digit n
is found in num
. You should not have for or while loops
in your code.
Hint: The mod and the integer divide are your friends…
count(9983, 9) # returns 2
count(9983, 0) # returns 0
Write a recursive function factorial(num)
that
given a number num
, returns the factorial num!
.
For example, factorial(5)
should return 120.
Hint: num! = num * (num - 1)!
Write a recursive function palindrome(s)
which takes a string s
and returns True if the string is a palindrome, False otherwise. Ignore spaces and upper vs. lower case. You should not have for or while loops
in your code.
Write a recursive function reverse(word)
that
given a string word
, returns the reverse of word
.
For example, reverse('python')
should return
nohtyp
.
Hint: Take the first character, reverse the remaining characters in the word, and concatenate the first character to the end of the new reversed string.