Skip to content

Commit 9c13377

Browse files
* docs: kata description * feat: kata/check-whether-a-number-is-valid-in-a-given-numeral-system --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent c56781d commit 9c13377

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# [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")
2+
3+
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
4+
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
5+
system (also called base-2), which uses the digits 0 and 1.
6+
7+
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
8+
way to 'Z' in base-36.
9+
10+
The largest digit allowed in a certain base is always 1 smaller than this base.
11+
12+
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
13+
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
14+
valid for this base, because it contains the digits 9 and 8 which are not a part of base-8.
15+
16+
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
17+
should return a boolean: ```true``` for numbers that are valid for a specified numeral system and ```false``` otherwise.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Solution {
2+
static boolean validateBase(String num, int base) {
3+
return num.chars().allMatch(c -> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(c) < base);
4+
}
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import static org.junit.jupiter.api.Assertions.assertFalse;
2+
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class SolutionTest {
8+
@ParameterizedTest
9+
@CsvSource(textBlock = """
10+
61262, 8
11+
ABCDEF, 16
12+
009, 10
13+
0, 5
14+
3C52Q6W8V1, 34
15+
""")
16+
void valid(String num, int base) {
17+
assertTrue(Solution.validateBase(num, base));
18+
}
19+
20+
@ParameterizedTest
21+
@CsvSource(textBlock = """
22+
25172, 5
23+
EG, 16
24+
W, 32
25+
99235, 9
26+
""")
27+
void invalid(String num, int base) {
28+
assertFalse(Solution.validateBase(num, base));
29+
}
30+
}

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
- [Character Counter](character-counter "56786a687e9a88d1cf00005d")
8686
- [Check contained matrix](check-contained-matrix "5a46179ce626c5ef8d000024")
8787
- [Check three and two](check-three-and-two "5a9e86705ee396d6be000091")
88+
- [Check whether a number is valid in a given numeral system](check-whether-a-number-is-valid-in-a-given-numeral-system "67757660c552a3a7ef9aaceb")
8889
- [Chinese Zodiac](chinese-zodiac "57a73e697cb1f31dd70000d2")
8990
- [Circle cipher](circle-cipher "634d0723075de3f97a9eb604")
9091
- [Circular List](circular-list "5b2e60742ae7543f9d00005d")

0 commit comments

Comments
 (0)