• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to insert a digit between an integer?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Please help.
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this post might be better in the "Beginning Java" subforum instead of in the "Threads and Synchronization" one.
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This can be done with just integer division and the % operator
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3519
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3519
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I only used integer operations. No String manipulation or parsing whatsoever.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!