Question
Please make sure to follow all the requirements ( bolded ). USE PYTHON3; DO NOT IMPORT ANY PACKAGES (I would really appreciate it if this
Please make sure to follow all the requirements (bolded).
USE PYTHON3; DO NOT IMPORT ANY PACKAGES
(I would really appreciate it if this can be done as soon as possible. Thank you!)
2.
a)
Write two functions (foo, bar) that take an integer, and make calls to each other or themselves based on the following rules:
Requirement: Use mutual recursion to solve this question. Solve everything using recursion (NO INNER or LAMBDA FUNCTIONS )
def foo(num):
"""
>>> foo(0)
0
>>> foo(3)
1
>>> foo(10)
6
"""
# YOUR CODE FOR foo() GOES HERE #
def bar(num):
"""
>>> bar(2)
3
>>> bar(11)
4
>>> bar(100)
50
"""
# YOUR CODE FOR bar() GOES HERE #
b)
Similar to the previous problem, write a recursive function, this time on i-th character pairs.
The i-th character pair is defined as the pair of the i-th character from the front and the i-th character from the back. For example, the first character pair denotes the pair of the first character and the last character. Note that if the string has odd length, the middle character itself forms a character pair.
Write a recursive function that takes a string, skips the first n_skip character pairs, then swaps the next n_swap character pairs.
You may assume that n_skip and n_swap are non-negative integers, and their sum won't exceed the total number of character pairs available.
Requirement:
You cannot use for, while, map() and filter(). You cannot use a recursive helper function, including an inner function. No lambda functions either
Example:
Input: kkkABXXXXCDkkk, n_skip = 3, n_swap = 2
Output: kkkDCXXXXBAkkk
Explanation: Skip the first 3 character pairs (all k-k), then swap the next 2 character pairs (A-D, B-C).
def skip_then_swap(string, n_skip, n_swap):
"""
>>> skip_then_swap('kkkABXXXXCDkkk', 3, 2)
'kkkDCXXXXBAkkk'
>>> skip_then_swap('DSC20', 1, 2)
'D2CS0'
>>> skip_then_swap('skip_then_swap', 4, 3)
'skip_neht_swap'
"""
# YOUR CODE GOES HERE #
Write two functions (foo, bar) that take an integer, and make calls to each other or themselves based on the following rules: 0 (num 1) bar(num - 1) +2 (num is even) bar(num) = foo(num 4) +1 (num is odd)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started