Last post Apr 29, 2021 09:59 AM by yij sun
Apr 29, 2021 12:33 AM|ahmedbarbary|LINK
I need to write function by c# that given Integer N return maximum Possibe value obtainable by deleting one '5' digit
from decimal representation of N .
it is guranteed that N will contain at least one '5' digit
Given N=15958, the function should return 1958
Given N=-5859, the function should return -589
Given N=-5000, the function should return 0
because after remove 5 from digit will give me multiple 0 so value will be 0
N is integer within range [-999,995 and 999,995]
N will contain at least one '5' digit on decimal representation
N consist at least two digit on decimal representation
Apr 29, 2021 12:50 AM|PaulTheSmith|LINK
You have told us that you have an assignment to do. You have not told us about a problem.
What code do you have? What is not working? Where are you stuck?
(Please do my job/homework is not a question!)
Apr 29, 2021 01:06 AM|ahmedbarbary|LINK
what i try is :
static int MaximumPossible(int num, int digit)
// edge case
if (num == 0)
return digit * 10;
// -1 if num is negative number else 1
int negative = num / Math.Abs(num);
// get the absolute value of given number
num = Math.Abs(num);
int n = num;
// maximum number obtained after inserting digit.
int maxVal = int.MinValue;
int counter = 0;
int position = 1;
// count the number of digits in the given number.
while (n > 0)
n = n / 10;
// loop to place digit at every possible position in the number,
// and check the obtained value.
for (int i = 0; i <= counter; i++)
int newVal = ((num / position) * (position * 10)) + (digit * position) + (num % position);
// if new value is greater the maxVal
if (newVal * negative > maxVal)
maxVal = newVal * negative;
position = position * 10;
but it not remove 5 from number
Apr 29, 2021 01:22 AM|PaulTheSmith|LINK
1. Place a breakpoint on the first line of the code.
2. Run the code. Execution will stop at the first line.
3. Make a single step.
4. Check the values of all local variables.
5. Are they correct?
6. If they are correct go to 3
7. Change the code to fix the problem in the statement that was just executed
8. Go to 2
Apr 29, 2021 01:49 AM|PaulTheSmith|LINK
I can't help but notice that this thread is called '... remove one digit ...' but the code comment says "... place digit at every possible position ..."
Are you removing or adding?
Apr 29, 2021 02:17 AM|ahmedbarbary|LINK
I need to remove
so if possible can you help me modify function above for remove please
Apr 29, 2021 03:40 AM|PaulTheSmith|LINK
If you want to remove then you should write some code that at least tries to remove rather than add. My advice is not to take a random piece of code which does the opposite of what yo want to achieve.
Get a blank piece of paper and
a rubber duck. Explain to the duck how they could solve the problem without any use of a computer. Develop an algorithm.
Write code that does what you have explained to the duck. Test it. Fix it.
Apr 29, 2021 09:59 AM|yij sun|LINK
As far as I think,you should do this:
1.find the position of '5'.
2.get the value based of the position.
3.do the function again of other possible.
More details,you could refer to below article: