Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

Students also viewed these Databases questions

Question

Complexity of linear search is O ( n ) . Your answer: True False

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago