Skip to content

Instantly share code, notes, and snippets.

@Vitaliy-Savkin
Created January 26, 2023 08:22
Show Gist options
  • Save Vitaliy-Savkin/9783764e24ec4fd32f90a68aba6352ee to your computer and use it in GitHub Desktop.
Save Vitaliy-Savkin/9783764e24ec4fd32f90a68aba6352ee to your computer and use it in GitHub Desktop.

Q:
Write a function in Java taking string and returning string, which is
“3” if the input string represents a positive number evenly divisible by 3
“5" if the input string represents a positive number evenly divisible by 5
“15” if the input string represents a positive number evenly divisible by 15
and the input string itself otherwise.

A:
Here's one possible implementation of a function that takes a string as input and returns a string as output, based on the conditions you've described:

public static String checkDivisibility(String input) {
    int num;
    try {
        num = Integer.parseInt(input);
    } catch (NumberFormatException e) {
        return input;
    }
    if (num <= 0) {
        return input;
    }
    if (num % 15 == 0) {
        return "15";
    }
    if (num % 5 == 0) {
        return "5";
    }
    if (num % 3 == 0) {
        return "3";
    }
    return input;
}

This function first tries to parse the input string as an integer using the Integer.parseInt() method. If this throws a NumberFormatException, it means the input string doesn't represent a valid integer, so the function returns the input string as is. If the parsing is successful and the integer is greater than 0, the function then checks if it is divisible by 15, 5 and 3 in that order, and returns the appropriate string. If none of the conditions are met it returns the input string as it is.

Q:
What the most computationally complex operation in this function?

A:
The most computationally complex operation in this function is the modulo operation (i.e. % operator) used to check for divisibility by 15, 5 and 3.

The modulo operation calculates the remainder when one number is divided by another. In this case, the function is checking if the input integer is divisible by 15, 5, and 3 by checking if the remainder when the input integer is divided by 15, 5, and 3 is equal to zero. This operation is relatively inexpensive, but it's the most time-consuming operation in the function because it's done multiple times.

It is worth noting that, in this implementation, the program first checks for divisibility by 15, then 5, then 3. This is because the number that is divisible by 15 is also divisible by both 3 and 5, so it's more efficient to check for 15 first.

Q:
That's not correct actually - the most complex operation is parseInt, especially when it throws exception.

A:
You are correct that the most computationally complex operation in this function is the Integer.parseInt(input) method call. This method is used to convert the input string to an integer, and it can be an expensive operation, especially if the input string is large or not in the correct format.

When the input string is not a valid integer representation, Integer.parseInt(input) throws a NumberFormatException. The program then catches this exception and returns the input string as is. Catching an exception also has a computational cost, and it is usually considered an expensive operation.

In general, it's good practice to first check if the input string is a valid integer representation before calling Integer.parseInt(input) to avoid unnecessary exception handling and improve the performance of the function.

Q:
What are divisibility rules for 3 and 5?
Divisibility rules are simple tests that can be used to quickly determine if a number is divisible by a certain factor without performing a full division. Here are the divisibility rules for 3 and 5:

A:
Divisibility by 3: A number is divisible by 3 if the sum of its digits is divisible by 3. For example, the number 123 is divisible by 3 because 1+2+3 = 6 which is divisible by 3. Divisibility by 5: A number is divisible by 5 if its last digit is either 0 or 5. For example, the number 150 is divisible by 5 because its last digit is 0, while the number 25 is divisible by 5 because its last digit is 5. Note that divisibility rules are not always the most efficient way to check divisibility, especially if the number you want to test is large. In most cases, the modulo operation (i.e. % operator) is more efficient and accurate, as it's implemented in machine level.

Q:
Rewrite the function checkDivisibility using divisibility rules to avoid using Integer.parseInt.

A:
Sure, here's one possible implementation of the checkDivisibility function that uses divisibility rules instead of Integer.parseInt:

public static String checkDivisibility(String input) {
    int len = input.length();
    int sum = 0;
    for (int i = 0; i < len; i++) {
        char c = input.charAt(i);
        if (c < '0' || c > '9') {
            return input;
        }
        sum += c - '0';
    }
    if (input.charAt(len - 1) == '0' || input.charAt(len - 1) == '5') {
        return "5";
    }
    if (sum % 3 == 0) {
        return "3";
    }
    return input;
}

This function first checks if the string contains only digits, by iterating over each character in the input string and checking if it is between '0' and '9'. If not, the function returns the input string as is. Then, it checks if the last digit of the input string is 0 or 5 by checking the last char in the input string. If it is, the function returns "5". After that, it uses the divisibility rule for 3 by checking if the sum of the digits of the input string is divisible by 3. If it is, it returns "3". If none of the conditions are met it returns the input string as is.

This implementation avoids the use of Integer.parseInt and the exception handling that would be required if the input string is not a valid integer representation. However, this implementation has the trade-off of being less efficient for large numbers, as it sums all digits one by one, and it's not as accurate as % operator for large number.

Q:
The implementation is obviously not correct since it never return "15". Please try again.

A:
I apologize for the oversight. Here is a corrected version of the function that includes the check for divisibility by 15:

public static String checkDivisibility(String input) {
    int len = input.length();
    int sum = 0;
    for (int i = 0; i < len; i++) {
        char c = input.charAt(i);
        if (c < '0' || c > '9') {
            return input;
        }
        sum += c - '0';
    }
    if (input.charAt(len - 1) == '0' || input.charAt(len - 1) == '5') {
        if(input.charAt(len-1)=='0') return "15";
        else return "5";
    }
    if (sum % 3 == 0) {
        return "3";
    }
    return input;
}

The function first checks if the last digit is 0, in that case it returns "15" since a number divisible by 15 is also divisible by 3 and 5. Then, it uses the divisibility rule for 5 and 3 in the same way as before. If none of the conditions are met it returns the input string as is.

Please let me know if you have any other question or if there's anything I can help with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment