Coding challenge: Solution debugging - java

A picture of code and test cases:
This is the problem:
Given a non-negative number "num", return true if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2. See also: Introduction to Mod
My code fails nearTen(1) for some reason. please open the picture for details.
public boolean nearTen(int num) {
if (num < 8)
return false;
if (num % 10 == 0)
return true;
while (num / 10 != 0) {
num = num - 10;
}
if (num == 8 || num == 9 || num == 1 || num == 2)
return true;
return false;
}

public boolean nearTen(int num) {
if(num < 8) {
return false;
} else if(num % 10 == 0) {
return true;
}
while(num / 10 != 0) {
num = num - 10;
}
if(num == 8 || num == 9 || num == 1 || num == 2) {
return true;
}
return false;
}
this is your code, but in first if condition you are doing
if(num < 8)
return false
//so 100% you will fail case => input is 1
And your logic has some issues. Instead of using "/", we can use "%". Reason is you only consider number multiplied by 10.
for example:
1 / 11 / 21 / 111 / 555551 / 1000001 => last digit is 1
2 / 12 / 122 / 12342 / 5124512 / 1000002 => last digit is 2
for all those numbers we can ignore previous digits, and only consider last digit. Because previous digits are numbers used to multiply 10.
so this answer can be simplified to following code
public boolean nearTen(int num) {
if(num < 0) {
return false;
}
num %= 10;
return num == 8 || num == 9 || (num >= 0 && num <= 2);
}

Related

In If condition explanation

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
In this code I want to know about (rev == Integer.MAX_VALUE / 10 && pop > 7) and (rev == Integer.MIN_VALUE / 10 && pop < -8)
Why do they use pop>7 and pop<-8?
class Solution {
public int reverse(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return rev;
}
}
class Solution {
public int reverse(int x) {
int ans=0;
while(x!=0)
{
int digit=x%10;
if( (ans>Integer.MAX_VALUE/10) || (ans<Integer.MIN_VALUE/10) )
{
return 0;
}
ans=(ans*10)+digit;
x=x/10;
}
return ans;
}
public void main(String[] args)
{
int x=120;
System.out.print("Reversed Number is "+ reverse(x));
}
}
-2147483648 to 2147483647 signed integer range if condition true when the rev integer is equal to maximum integer value ,rev==(2147483647) and the last digit of given integer is greater then 7 than only enters in the if condition return 0.
same as above next if also do check the same condition with (-2147483648).

Print numbers in Words [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 2 years ago.
I am trying to print numbers in words. The code I have written is pretty much covering a lot of scenarios but not this: 100 (or 10). It should print One Zero Zero but its only printing one. Can someone please help.
public static void numberToWords (int number) {
if (number < 0) {
System.out.println("Invalid Value");
}
if (number==0){
System.out.println("Zero");
}
number = reverse(number);
int modulus = 0;
while (number > 0) {
if (number % 10 == 0) {
System.out.println("Zero"); } else if (number % 10 == 1) { System.out.println("One"); } else if (number % 10 == 2) {
System.out.println("Two"); } else if (number % 10 == 3) { System.out.println("Three");} else if (number % 10 == 4) {
System.out.println("Four");} else if (number % 10 == 5) { System.out.println("Five");} else if (number % 10 == 6) {
System.out.println("Six");} else if (number % 10 == 7) { System.out.println("Seven");} else if (number % 10 == 8) {
System.out.println("Eight");} else if (number % 10 == 9) { System.out.println("Nine");}
number/=10;
}
}
public static int reverse (int number) {
int reverse = 0;
while (number != 0) {
int lastdigit = (number % 10);
reverse*=10;
reverse +=lastdigit;
number/=10;
}
return reverse;
}
public static int getDigitCount (int number) {
if (number < 0) {
return -1;
}
if (number==0){
return 1;
}
int count = 0;
int last = 0;
while (number > 0) {
last = number % 10;
count++;
number/=10;
}
return count;
}
When you reverse 100, it becomes 1. so you output it "one".
Instead of doing all this thing, Just convert the number into a string and then char array. And then loop on this array

count number of digit using recursive method

Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
count8(8) → 1
count8(818) → 2
count8(8818) → 4
my program seems not able to count double '8's. Here's the code.
public int count8(int n) {
boolean flag = false;
if(n<10)
{
if (n==8)
{
if(flag == true)
return 2;
else
{
flag = true;
return 1;
}
}
else
{
flag = false;
return 0;
}
}
else
return count8(n%10)+count8(n/10);
}
I was wondering if the last line goes wrong but I don't know how to check it. Looking forward to your help. Thanks!
Pass the state (is the previous digit eight) to the method:
private static int count8(int n, boolean eight) {
if (n <= 0)
return 0;
else if (n % 10 == 8)
return 1 + (eight ? 1 : 0) + count8(n / 10, true);
else
return count8(n / 10, false);
}
public static int count8(int n) {
return count8(n, false);
}
Your flag variable is only local. There's only one time you read it: if (flag == true) and since you never change it's value before that it will always be false.
You make this a lot more complicated than it has to be though. No need for an additional parameter at all.
public int count8(int n)
{
if (n % 100 == 88) return count8(n/10) + 2;
if (n % 10 == 8) return count8(n/10) + 1;
if (n < 10) return 0;
return count8(n/10);
}
You can try smth like this:
public int count8(int n) {
if (n < 10)
return n == 8: 1 ? 0;
int count = 0;
String num = Integer.toString(n);
int numLength = num.length();
if (numLength % 2 != 0)
num += "0";
if ((num.charAt(numLength / 2) == num.charAt(numLength / 2 - 1)) && (num.charAt(numLength / 2) == "8"))
count++;
String left = num.substring(0, numLength / 2);
int leftInt = Integer.parseInt(left);
String rigth = num.substring(numLength / 2);
int rigthInt = Integer.parseInt(rigth);
return count + count8(leftInt) + count8(rigthInt);
}
C++
int count8(int n) {
return n == 0 ? 0 : (n % 10 == 8) + (n % 100 == 88) + count8(n/10);
}
Java & C#
int count8(int n) {
if (n==0) return 0;
if(n % 100 == 88)
return 2 + count8(n / 10);
if(n % 10 == 8)
return 1 + count8(n / 10);
return count8(n / 10);
}

Checking if number is ugly

I'm doing this problem:
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
Here's my attempt:
public class Solution {
public boolean isUgly(int num) {
if (num == 1) {
return true;
}
for (int i = 7; i <= num / 2; i++) {
if (isPrimeFactor(i, num)) {
return false;
}
}
return true;
}
public boolean isPrimeFactor(int candidate, int num) {
return isPrime(candidate) && isFactor(candidate, num);
}
public boolean isPrime(int num) {
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
for (int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public boolean isFactor(int candidate, int num) {
return (num % candidate == 0);
}
}
Unfortunately, it fails on test input -2147483648. It returns true when it should be false.
Any idea what I've done wrong?
You simply forgot the following emphasized condition:
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Therefore, you just need to add a check for negative numbers inside your isUgly method:
if (num <= 0) {
return false;
}
As a side-note, you could perhaps improve a little the performance by swapping the conditions inside isPrimeFactor and testing isFactor(candidate, num) && isPrime(candidate) instead of isPrime(candidate) && isFactor(candidate, num). This is because it is faster to determine whether a number is a factor of another than determining if a number is a prime number.
I could propose a different but a lot faster solution O(logn) for this problem:
public static boolean isUgly(int num) {
if (num < 1) return false;
int temp;
do {
temp = num;
if (num % 2 == 0) num /= 2;
if (num % 3 == 0) num /= 3;
if (num % 5 == 0) num /= 5;
} while (temp != num);
return num == 1;
}
or an even faster approach in terms of modular checks (by splitting the do while loop):
public static boolean isUgly(int num) {
if (num < 1) return false;
int temp;
do {
temp = num;
if (num % 2 == 0) num /= 2;
} while (temp != num);
do {
temp = num;
if (num % 3 == 0) num /= 3;
} while (temp != num);
do {
temp = num;
if (num % 5 == 0) num /= 5;
} while (temp != num);
return num == 1;
}

Finding integers that are divisible by 6 or 7 but not both

I am trying to write a program that displays the integers between 1 and 100 that are divisible by either 6 or 7 but not both.
Here is my code:
import acm.program.*;
public class Problem4 extends ConsoleProgram
{
public void run()
{
for (int i = 1; i <= 100; i++)
{
boolean num = ((i % 6 == 0) || (i % 7 == 0));
if (num == true)
println(i + " is divisible");
}
}
}
The above code shows the following answers:
6,7,12,14,18,21,24,28,30,35,36,42,48,49,54,56,60,63,66,70,72,77,78,84,90,91,96,98
Now the bold numbers 42 and 84 are both divisbile by 6 and 7. Now If I change the || to && in the above code, the result shows only 42 and 84.
What change should I do to remove these 2 numbers from the final result?
XOR is the way to go.
import acm.program.*;
public class Problem4 extends ConsoleProgram {
public void run() {
for (int i = 1; i <= 100; i++) {
if ( (i % 6 == 0) ^ (i % 7 == 0) ) {
println(i + " is divisible");
}
}
}
}
you have to make your condition look like:
boolean num = (i % 6 == 0 || i % 7 == 0) && !(i % 6 == 0 && i % 7 == 0);
that's basically converting "but not both" to Java code :)
You need an extra check for "but not both". I think it should be:
boolean num = ((i % 6 == 0) || (i % 7 == 0)) && (i % 42 != 0);
you can also try
boolean num = ((i % 6 == 0) != (i % 7 == 0));
Think about what it means to be divisible by 6 and 7... the answer to life the universe and everything.
import acm.program.*;
public class Problem4 extends ConsoleProgram
{
public void run()
{
for (int i = 1; i <= 100; i++)
{
boolean num = ((i % 6 == 0) || (i % 7 == 0));
boolean both = ((i % 6 == 0) && (i % 7 == 0));
if ((num == true) && (both == false))
println(i + " is divisible");
}
}
}
Here is a snippet that should work as well- in C++ but change to boolean...
int value;
if ((value % 6 == 0 && value % 7 != 0) || (value % 6 != 0 && value % 7 == 0))
cout << "Is " << value << " divisible by 6 or 7, but not both? true" << endl;
else
cout << "Is " << value << " divisible by 6 or 7, but not both? false" << endl;
A little simplified version
for(int i=1; i<=100; i++) {
// Either Divisible by 6 or 7 but not both
if((i%6==0 && i%7!=0) ||( i%7==0 && i%6!=0)) {
println(i);

Categories

Resources