programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

# How to insert a digit between an integer?

Greenhorn
Posts: 14
I am supposed to write a program which returns the maximum possible value obtained by inserting one '5' digit inside decimal representation of integer N

Example:Given N=268, the function should return 5268
Given N=670, the function should return 6750
Given N=0, the function should return 50

I have written a function as shown below:

This basically does only one computation(5268). But I need this 5 to be inserted between all digits according to the digit count in a number. The computation thus done should be as inserting between all the digits
thereby getting combinations as 2685,2658,2568,5268(Shifting 5, if observed). The same should be performed for any number given as input and the highest number should be found out from this combination.

Rancher
Posts: 260
12

Jan Rad wrote:I am supposed to write a program which returns the maximum possible value obtained by inserting one '5' digit inside decimal representation of integer N

Example:Given N=268, the function should return 5268
Given N=670, the function should return 6750

I guess you meant that if N=670 it should become 5670?
If I understand correctly, the digit 5 should always be added from the left?

Brecht Geeraerts
Rancher
Posts: 260
12
I think this post might be better in the "Beginning Java" subforum instead of in the "Threads and Synchronization" one.

Marshal
Posts: 25198
64
• 1
Agreed, and done. By the way there's a "Report" button on each post which you can use to draw the moderators' attention to issues like that.

Brecht Geeraerts
Rancher
Posts: 260
12
I have been thinking about it a bit more. So if the initial number is 670 and you need to insert the digit 5, possible combinations are 6705, 6750, 6570, 5670 and you need to return the biggest one? Is that the intent of this exercise?

Brecht Geeraerts
Rancher
Posts: 260
12
I like the fact that you have already tried to write some code yourself before reaching out for help. After all, that is where the real learning happens. I have given this a try as well even though I am still not sure that I have gotten the real goal of the exercise. I am sure that better coders can come up with a shorter version but I have tried to keep it as clear as possible.

To insert the digit 5 at a different position (going from right to left) I have used a StringBuffer instead of a String object. That makes is more convenient to insert the digit at a different index. I have tried to explain step by step what is going on through the use of comments (see below). I have taken 670 as example.

Is this what you had in mind? Do you understand the different steps?

PS: I have made the assumption that the starting value is always >= 0

Sheriff
Posts: 15038
251
This can be done with just integer division and the % operator

Greenhorn
Posts: 1
Hi I would tell you a suedo code for the problem:

1. Convert the number to arraylist of integers
2. Loop through the list
3. Find the first occurrence of number less than 5
4. Append 5 to the list at that specified index + 1
5. Break the loop
6. Convert the list to a number.

Junilu Lacar
Sheriff
Posts: 15038
251
The human brain is a pattern recognition machine so one of the best ways to leverage that is to show it many examples. With many examples, your brain can start seeing patterns and thus generalize a solution for a problem.

Given N=268, the function should return 5268

Put 5 in 1s place:      2685 == (2 * 10 * 10 * 10) + (6 * 10 * 10) + (8 * 10) + 5
Put 5 in 10s place:    2658 == (2 * 10 * 10 * 10) + (6 * 10 * 10) + (5 * 10) + 8
Put 5 in 100s place:  2568 == (2 * 10 * 10 * 10) + (5 * 10 * 10) + (6 * 10) + 8
Put 5 in 100s place:  5268 == (5 * 10 * 10 * 10) + (2 * 10 * 10) + (6 * 10) + 8

Try doing the same for these:
Given N=670, the function should return 6750
Given N=0, the function should return 50

See if you can discern a pattern and then try to come up with a generalization that you can code using loops, integer division (/), and the remainder operator (%)

Brecht Geeraerts
Rancher
Posts: 260
12
Hi Shaik,

Welcome to the ranch. Whenever you post a code snippet, please use the code tags (see here how to use them). They will make the code easier to read, for you and for everyone else on the forum.

Bartender
Posts: 3769
154
• 1
Maybe easier than using integer division and remainders is to transform the number into a String, giving access to the characters and then follow Junilu's tips.

Brecht Geeraerts
Rancher
Posts: 260
12
That was my approach as well. I found it easier to use the index to insert the digit. Then I put all combinations in a list and returned the largest one.

I think that using integer division and % is more difficult and more prone to errors (at least for me it is). I often see people make mistakes when using %... I guess it all comes down to personal preference and moreover, experience.

Junilu Lacar
Sheriff
Posts: 15038
251
When something seems difficult to do, I find it's often because too much is being done in one place and the intent is getting obscured by implementation details. Having all that code in main() or findLargest() or whatever is a great example.

This

Is much easier to grok than a few lines of code that uses / and % to put each digit of a number in a list.

And

is easier to manage when you hide the implementation details of inserting 5 into number at the designated place. Then I can easily see that I just need to increase place from 1 to 10 to 100, etc.

I think the main problem is really wanting to keep the number of lines overall to a minimum instead of wanting to keep the number of ideas you have to contend with in one place to a minimum.

Piet Souris
Bartender
Posts: 3769
154
• 1
I suggest letting OP react before we go on, for we have given several solutions and tips now. Not that we tipped OP how to extend/fix his method, though.

Junilu Lacar
Sheriff
Posts: 15038
251
Just wanted to share what I ended up with. It's a fun little exercise for practicing TDD and refactoring in particular. I also got to play around with @ParameterizedTest in JUnit 5.

The largestNumberFrom() method expresses the algorithm I'm using at a very high level (or at least I hope it does).

Here's the JUnit 5 ParameterizedTest I ended up with after some refactoring:

I love how I can customize the display name of each test. The output looks something like this:

new MaxFinder(n)
where n == 5
[1] max(0) == 50
[2] max(1) == 51
[3] max(6) == 65
[4] max(9) == 95
[5] max(10) == 510
[6] max(50) == 550
[7] max(54) == 554
[8] max(55) == 555
[9] max(60) == 650
[10] max(66) == 665
[11] max(100) == 5100
[12] max(268) == 5268
[13] max(670) == 6750
[14] max(709) == 7509
[15] max(819) == 8519

where n == 7
[1] max(6) == 76
[2] max(8) == 87
[3] max(10) == 710
...

Junilu Lacar
Sheriff
Posts: 15038
251
BTW, I only used integer operations. No String manipulation or parsing whatsoever.

Junilu Lacar
Sheriff
Posts: 15038
251
I'm finding that it's also a good exercise in test case analysis and refactoring (eliminating duplication by deleting redundant tests).

I just found a bug in my code by adding this test for when n == 5:

and to verify the bug fix, I added at test with more digits involved:

Finally, I had to refactor the production code to express the correct algorithm:

Junilu Lacar
Sheriff
Posts: 15038
251
More refactoring... I don't know if this expresses the algorithm I'm using more clearly than what I had before but it's certainly shorter.

What I'm trying to say is this: To find the largest number you can make from the digits of N, insert the digit 5 between all the leftmost digits that larger or equal and the rest of the digits.

I want to prove my earlier point:

I wrote:When something seems difficult to do, I find it's often because too much is being done in one place and the intent is getting obscured by implementation details.

The insertDigitBetween() method has exactly 5 lines of code in it and so does the largerOrEqual() method. The digitsOf() method has 8 lines. This shows that when you are relentless in functional decomposition and refactoring, jobs that appear to be big and difficult become just a bunch of small and easy jobs.

Greenhorn
Posts: 1

Marshal
Posts: 68069
258
• 1
Welcome to the Ranch

Saloon Keeper
Posts: 11472
247

Junilu Lacar wrote:BTW, I only used integer operations. No String manipulation or parsing whatsoever.

Why?

We espouse using the correct type to model our problem. A digit is a part of a numeral, and a numeral is a string of symbols, not a number.

Using strings is not only conceptually correct, it also makes the application future-proof if at any point the requirements change to take numeral systems other than decimal into account.

Junilu Lacar
Sheriff
Posts: 15038
251
As I see it, the problem is not dealing with String values and concatenation/insertion but rather with digits and place values. At least that's the model I was using and number operations were more direct vs. operating with strings and then parsing to number values.

You might argue that "2685", "2658", "2568", and "5268" sort the same way as their equivalent numeric values but again, the spirit of the problem is to compare numeric values, not string values. Plus (and to be perfect honest), I suspected the String route would be the road most traveled so I wanted to try the one less traveled instead.

Stephan van Hulst
Saloon Keeper
Posts: 11472
247

Junilu Lacar wrote:As I see it, the problem is not dealing with String values and concatenation/insertion but rather with digits and place values.

The problem quite literally IS dealing with strings and insertion:

Jan Rad wrote:I am supposed to write a program which returns the maximum possible value obtained by inserting one '5' digit inside decimal representation of integer N

A digit IS a character. A decimal representation IS a string of characters.

Junilu Lacar
Sheriff
Posts: 15038
251

Stephan van Hulst wrote:The problem quite literally IS dealing with strings and insertion:

Not my interpretation of "a program which returns the maximum possible value"

Also, I interpret "decimal representation" as base-10 as opposed to base-2 or base-8 or base-16 (binary representation, octal representation, hexadecimal representation, respectively).

I guess we'll just have to disagree on this one.

Junilu Lacar
Sheriff
Posts: 15038
251

I wrote:
I interpret "decimal representation" as base-10 as opposed to base-2 or base-8 or base-16 (binary representation, octal representation, hexadecimal representation, respectively).

I guess we'll just have to disagree on this one.

Or maybe not.

I was thinking about what I said about "decimal representation" and if the problem could be solved with just number operations if the problem were something like "insert A somewhere in 0xC9F7 to produce the largest possible value." The best way to solve that that I can think of is to use String operations. Of course, that could just be a lack of imagination on my part.

Stephan van Hulst
Saloon Keeper
Posts: 11472
247

Junilu Lacar wrote:Also, I interpret "decimal representation" as base-10 as opposed to base-2 or base-8 or base-16

Exactly. Base-10 is a numeral system. Numeral systems deal with numerals, not with numbers.

Junilu Lacar wrote:I was thinking about what I said about "decimal representation" and if the problem could be solved with just number operations if the problem were something like "insert A somewhere in 0xC9F7 to produce the largest possible value." The best way to solve that that I can think of is to use String operations. Of course, that could just be a lack of imagination on my part.

If it is, then we share the same limitation.

 Not looking good. I think this might be the end. Wait! Is that a tiny ad? Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java