Question

In: Computer Science

Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...

Solving Problems Using Recursion (Python):

To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables.

1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list.

Ex.

Input = [5555537777721]

Output = [53721]

Solutions

Expert Solution

  • Below is the detailed solution of the above problem in python with code and it output.
  • Please read the comments for better understanding.
  • Using recursion to create a function that takes a positive integer and returns it with neighboring digits removed.
  • CODE:

#function which takes an integer and returns it with neighboring digits removes using recursion
def func(num):
#base case
#check if num is a single digit number
x=num//10
#if num is a single digit number then return it.
if x==0:
return num
#otherwise check if the last two digits are the same
rem1=num%10
num=num//10
rem2=num%10
#if last two digit are same then call func() with last digit removed recursively
if rem1==rem2:
return func(num)
#otherwise call func() with last digit removed but add the last digit in the returned answer
else:
return func(num)*10+rem1

#take input integer
num=int(input())
#print the result returned by the function
print(func(num))

  • INPUT/OUTPUT:
  1. 5555537777721
    53721
  2. 11112226666343453300
    1263434530
  • Below are the screenshots attacehd of the code and it's output for better understanding.

CODE and INPUT/OUTPUT

So if you have any doubt regarding this solution please feel free to ask in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem : The Rectangle class (Filename: TestRectangle.java) Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height....
Solve the following using Polya’s four-step problem-solving process. Use the strategy “Direct Reasoning” to solve the...
Solve the following using Polya’s four-step problem-solving process. Use the strategy “Direct Reasoning” to solve the problem. Show all work and clearly describe your thought process. julia, william, kelly, and mary each entered a dog in a dog jumping contest. there frogs were named hippy, happy, bounce, and pounce. – placed first, second, or third in the contest and earned a blue, red or white ribbon respectively. Use the following clues to determine who entered which frog and the order...
You must use Excel for solving all the problems In problem 13.9 on page 501, an...
You must use Excel for solving all the problems In problem 13.9 on page 501, an agent for real estate company wanted to predict the monthly rent for one-bedroom apartments, based on the size of the apartment. The data are stored in rentsliverspring. Use the results of that problem. a. at the 0.05 level of significance, is there evidence of a linear relationship between the size of the apartment and the monthly rent? b. Construct at 95% confidence interval estimate...
Solve using PYTHON 3 and use functions. You have been hired by a restaurant named "Burger...
Solve using PYTHON 3 and use functions. You have been hired by a restaurant named "Burger Shack" Per the client, you have the following information: the client’s name, burger’s name, time of the day, and the total bill. By the end of the day, your program will provide the following information: 1. Top three best clients (highest bills) 2. Name of the client with the second-to-last lowest bill 3. Busiest hour of the day (number of clients) Assumptions: 1. doesn't...
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
The Problem Below are a series of problems you need to solve using recursive functions. You...
The Problem Below are a series of problems you need to solve using recursive functions. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. Implementation Each recursive function MUST have a wrapper function enclosing it where you will do input/output file processing as well...
The Problem Below are a series of problems you need to solve using recursive methods BY...
The Problem Below are a series of problems you need to solve using recursive methods BY using java . You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck (15 points) Write a recursive method that checks whether an...
Use EXCEL to format this and solve using solve and explain. Biggest problem is once have...
Use EXCEL to format this and solve using solve and explain. Biggest problem is once have variables (which i have half done) is setting up in Excel. A farmer in the Midwst haas 1,000 acres of land on which she intends to plant corn, wheat, and soybeans. Each acre of corn costs $100 for preparation, requires 7 worker-days of labor, and yields a profit of $30. An acre of wheat costs $120 to prepare, requires 10 worker-days, and yields $40...
Objectives: use Scite 1. Use recursion to solve a problem 2. Create classes to model objects...
Objectives: use Scite 1. Use recursion to solve a problem 2. Create classes to model objects Problem 1: Compute greatest common divisor using recursion (filename: TestRecusiveGCD.java) The gcd(m, n) can be defined recursively as follows: If m % n is 0, gcd (m, n) is n. Otherwise, gcd(m, n) is gcd(n, m % n). Write a recursive method to find the GCD of two given integers. Write a test program that prompts the user to enter two integers, calls the...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT