Question

Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained...

Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained by inserting one '5' digit inside the decimal representation of integer N.

Examples:

1. Given N = 268, the function should return 5268.

2. Given N = 670, the function should return 6750.

3. Given N = 0, the function should return 50.

4. Given N = −999, the function should return −5999.

Assume that:

N is an integer within the range [−8,000..8,000].

Homework Answers

Answer #1

According to the problem statement,

With all due respect, you haven't mentioned any particular programming language to write a function for this problem statement.

So, I have coded the function using the PYTHON programming language.

Code Snippet:

Function Code in Text Format:

def MaximumPossibleValue(self, N):

        if N >= 0:
            value = str(N)
            for i in range(len(value)):
                if '5' > value[i]:
                    return int(value[:i] + '5' + value[i:])
          
        else:
            value = str(N)
            for i in range(1, len(value)):
                if '5' < value[i]:
                    return int(value[:i] + '5' + value[i:])
          
        return int(value + '5')

Explanation

for example if you take 670,

maximum possible value is 6750

it will start looping from 0th index

5 is not greater than 6 so it will move to the next index which 1

5 is not greater than 7 so it will move to the next index which 2

5 is greater than 0 so it will print 67 and append 5 to index number 2 and then it will append the remaining string 0

So the final output is 6750

I hope the function snippet and output explanation will help you out!

Thank you!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of frogs were sitting together on one block when they had a terrible quarrel. Now they want to jump away from one another so that the distance between them will be as large as possible. The distance between blocks numbered J and K, where JK, is computed as K-J+1. The frogs can only jump up, meaning that they can move from one block to another...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT