Given a base \$k\$ as well as two indices (also passed in base \$k\$, call them \$s\$ and \$e\$) return a list containing a frequency count of all base-\$k\$ digits occurring between \$s\$ and \$e\$ inclusive. For example, upon input \$k = 10, s = 1, e = 10\$, we would return the list \$[0: 1, 1: 2, 2: 1, 3: 1, 4:1, 5:1, 6:1 , 7:1, 8:1, 9:1]\$. Note that the values in the list should be base \$k\$ as well.
-
4\$\begingroup\$ Hello, and welcome to Code Golf! I like the challenge, but have a few questions: (1) What is the set of characters which should be used for bases larger than 36? (Or is \$k \leq 36\$?) \$\endgroup\$– 97.100.97.109Nov 2 at 17:17
-
4\$\begingroup\$ Hi, welcome to Code Golf Stack Exchange. Your challenge is quite good, but it could do with a few clarifications, like the comment made by @97.100.97.109 . It is reccomended to post challenges in the Sandbox first. \$\endgroup\$– The Empty String PhotographerNov 2 at 17:43
-
\$\begingroup\$ Let's assume \$k \leq 36\$. I will modify my challenge if you can provide me with a good base system for \$k > 36\$. \$\endgroup\$– Michael KlyachmanNov 2 at 17:48
-
3\$\begingroup\$ I think this needs to be much clearer about the strict I/O requirements. For what it's worth I don't think the "characters" question really addresses things, it's a question from someone who planned to use a string representation for their I/O. Sure, IF one is going to use a string of characters there is a limit, but really a base-k number is a list of integers from \$[0,k)\$. Would also be a good idea to show an example in a base other than ten (probably a higher base would show things best). \$\endgroup\$– Jonathan AllanNov 2 at 18:48
-
5\$\begingroup\$ I didn't notice until now (...), but this challenge doesn't have a scoring criteria (like code-golf). Please consider adding one (otherwise it would be off-topic :p) \$\endgroup\$– math scatNov 2 at 20:02
5 Answers
Vyxal, 75 bitsv2, 9.375 bytes
vβ÷ṡ⁰τfĊ⁰τ
Accepts input as \$[s, e], k\$. Takes in indices as a list of digits (to be able to take numbers of bases greater than 10).
Try it Online! | Another example
Bitstring:
100011000100010100000110110111110010011010110100010011110011110110000110010
vβ÷ṡ⁰τfĊ⁰τ
vβ Convert each index to base 10
÷ṡ Split to stack and push range
⁰τ Convert range to base k
fĊ Flatten list and push counts
⁰τ Convert counts list to base k
-
\$\begingroup\$ Hi- your code doesn't fully satisfy the end "values in base k" property. The example you've provided outputs ⟨ ⟨ 2 | 10 ⟩ | ⟨ 1 | 19 ⟩ | ⟨ 0 | 8 ⟩ ⟩ while I want the values (i.e. 10, 19, 8) to be in base 3 as well. \$\endgroup\$ Nov 2 at 17:51
-
-
1\$\begingroup\$ I realised I could take all numbers as a list of digits to make the input format more strict. (and, shaving off some bits in the process :d) \$\endgroup\$ Nov 2 at 19:01
Python, 163 bytes
def f(k,s,e):b=lambda n:n and b(n//k).lstrip("0")+[str(n%k),chr(n%k+55)][n%k>9]or"0";a="".join(map(b,range(int(s,k),int(e,k)+1)));return{c:b(a.count(c))for c in a}
Basic solution; a majority of the code is taken up by the helper function converting from base 10 to base \$k\$. I'm sure you could do something clever with a function which directly counts from \$s\$ to \$e\$ in base \$k\$. Only works for bases between 2 and 36.
Ruby, 80 bytes
->k,s,e{(s.to_i(k)..e.to_i(k)).flat_map{_1.digits k}.tally.map{[_1,_2.to_s(k)]}}
Takes k
as integer, s
and e
as strings in base k
. Test cases taken from math scat's Vyxal answer.
Haskell, 148 bytes
import Data.List
k#n|n<k=[n]
k#n|(d,m)<-divMod n k=m:k#d
f k s e=[(last(show x:[[toEnum$x+55]|x>9]),1+length y)|x:y<-group$sort$concatMap(k#)[s..e]]
Google Sheets, 228 bytes
=let(_,lambda(v,base(v,A1)),r,query(tocol(sort(to_text(split(regexreplace(join(,map(sequence(A3-A2+1,1,A2),_)),"(.)","$1 ")," ")))),"select count(Col1),Col1 group by Col1 label count(Col1)''",0),{map(index(r,,1),_),index(r,,2)})
Put the base in cell A1
, start in A2
and end in A3
, and the formula in C1
.
The same ungolfed:
=let(
_, lambda(v, base(v, A1)),
values, sequence(A3 - A2 + 1, 1, A2),
string, join(, map(values, _)),
digits, tocol(sort(to_text(split(regexreplace(string, "(.)", "$1 "), " ")))),
result, query(
digits,
"select count(Col1), Col1 group by Col1 label count(Col1)''",
0
),
hstack(
map(index(result, , 1), _),
index(result, , 2)
)
)
parameters | results | ||
---|---|---|---|
16 | base | C | 0 |
1 | start | 1D | 1 |
200 | end | 1D | 2 |
1D | 3 | ||
1D | 4 | ||
1D | 5 | ||
1D | 6 | ||
1D | 7 | ||
1D | 8 | ||
1C | 9 | ||
1C | A | ||
1C | B | ||
15 | C | ||
C | D | ||
C | E | ||
C | F |