Skip to content

feat: kata/check-whether-a-number-is-valid-in-a-given-numeral-system #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [Check whether a number is valid in a given numeral system](https://www.codewars.com/kata/check-whether-a-number-is-valid-in-a-given-numeral-system "https://www.codewars.com/kata/67757660c552a3a7ef9aaceb")

A numeral system is a way of writing numbers using a specific set of digits: for example, the decimal system (also called base-10), which is
the most commonly used numeral system worldwide, uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 to represent numbers. There is also the binary
system (also called base-2), which uses the digits 0 and 1.

For digits that are bigger than 9, the English alphabet is used: 'A' is used for the number 10 in bases higher than 10. This goes all the
way to 'Z' in base-36.

The largest digit allowed in a certain base is always 1 smaller than this base.

You need to write a function that checks whether all of the digits of a non-negative integer number are a part of the specified base: for
example, the number 17253 is valid for base-8, because this base contains the digits 0, 1, 2, 3, 4, 5, 6, 7, but the number 19823 is not
valid for this base, because it contains the digits 9 and 8 which are not a part of base-8.

Note: numbers will be checked against bases from 2 to 36. For digits > 9 (A, B, etc.) such digits will always be uppercase. The function
should return a boolean: ```true``` for numbers that are valid for a specified numeral system and ```false``` otherwise.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Solution {
static boolean validateBase(String num, int base) {
return num.chars().allMatch(c -> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(c) < base);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class SolutionTest {
@ParameterizedTest
@CsvSource(textBlock = """
61262, 8
ABCDEF, 16
009, 10
0, 5
3C52Q6W8V1, 34
""")
void valid(String num, int base) {
assertTrue(Solution.validateBase(num, base));
}

@ParameterizedTest
@CsvSource(textBlock = """
25172, 5
EG, 16
W, 32
99235, 9
""")
void invalid(String num, int base) {
assertFalse(Solution.validateBase(num, base));
}
}
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
- [Character Counter](character-counter "56786a687e9a88d1cf00005d")
- [Check contained matrix](check-contained-matrix "5a46179ce626c5ef8d000024")
- [Check three and two](check-three-and-two "5a9e86705ee396d6be000091")
- [Check whether a number is valid in a given numeral system](check-whether-a-number-is-valid-in-a-given-numeral-system "67757660c552a3a7ef9aaceb")
- [Chinese Zodiac](chinese-zodiac "57a73e697cb1f31dd70000d2")
- [Circle cipher](circle-cipher "634d0723075de3f97a9eb604")
- [Circular List](circular-list "5b2e60742ae7543f9d00005d")
Expand Down