Digit balanced numbers
Does a number have balanced numbers of distinct digits? That is, do the number's digits appear with equal frequency?
Input
- A positive integer N.
- N will have no leading zeroes.
Output
- An indication of whether the number has balanced digits.
- This can be a truthy or falsy value (where you can choose whether truthy corresponds to balanced or unbalanced), or one of 2 distinct values.
Examples
- 1234 has 1 of each present digit, so is balanced.
- 1223 has 1 1 and 1 3, but 2 2s, so is not balanced.
- 1212 has 2 1s and 2 2s, so is balanced.
- 1000 has 1 1 and 3 0s, so is not balanced (zeroes are relevant).
- 1100 has 2 1s and 2 0s, so is balanced.
- 5555 has 4 5s, so is balanced.
Test cases
Test cases are in the format input : output.
Note that you may choose any 2 distinct values instead of true and false, or you may use truthy and falsy outputs rather than consistent values.
1 : true
22 : true
98 : true
222 : true
234 : true
3456 : true
3434 : true
3355 : true
4664 : true
123456789 : true
234567890 : true
11223344 : true
111222333 : true
132321213 : true
54554445 : true
999999999 : true
778 : false
565 : false
1223 : false
2123 : false
999999998 : false
999989999 : false
112233445 : false
999999990 : false
101202303 : false
Scoring
This is a code golf challenge. Your score is the number of bytes in your code.
Explanations are optional, but I'm more likely to upvote answers that have one.
[Python 3], 43 bytes …
1y ago
Vyxal, 4 bytes ``` Ċvt≈ …
1y ago
Uiua, 10 bytes (SBCS) Count …
6mo ago
Perl, 45 bytes Takes numeri …
6mo ago
Japt `-g!`, 5 bytes ü ü …
1y ago
Vyxal 3, 2 bytes ``` ⊞≈ ` …
1y ago
6 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
Uiua, 10 bytes (SBCS)
Counts all digit occurrences and checks the count array is all-equal (for which there is no short idiom in Uiua yet except for that deduplicate-length-equal-to-one and similar gymnastics).
=1⧻◴⊕⧻⊸⊛°⋕
Explanation
=1⧻◴⊕⧻⊸⊛°⋕ 112233 example input
°⋕ "112233" unparse: convert to character array
⊸⊛ [0 0 1 1 2 2] "112233" push unique index per element,
keep argument on stack
⊕⧻ [2 2 2] determine per-group length: count array
◴ [2] deduplicate count array
⧻ 1 determine its length
=1 1 equal to 1?
0 comment threads
Vyxal, 4 bytes
Ċvt≈
Outputs 1 for true, 0 for false. The footer is to convert the output to the provided test case format.
Explained
Ċvt≈
Ċ # [x, input.count(x)] for x in uniquify(input)
vt # last item of each item in ^
≈ # are all items the same?
💎
Created with the help of Luminespire.
0 comment threads
Vyxal 3, 2 bytes
⊞≈
Shortest you'll probably get barring fractional bytes.
Explained
⊞≈ ## Input is a number
⊞ ## Counts of all items in the number
≈ ## Are they all the same?
0 comment threads
Perl, 45 bytes
Takes numerical string on stdin. Prints 0 if balanced; non-zero otherwise.
perl -ple'for$i(/./g){$n{()=/$i/g}++}$_=%n-1'
Comments:
-
-p- read line into$_, manipulate, then print -
-l- strip newline from$_, add back when printing -
for $i (/./g) {...}- split$_into individual characters, process each as$i -
()=/$i/g- count$is in$_ -
$n{...}++- store count as key of hash -
%n-1- zero iff exactly one key in hash (ie. input is balanced)

0 comment threads