14
\$\begingroup\$

Image of Hex Conversion Table w/ counter

Hexadecimal is a base 16 counting system that goes from 0 to f. Your job is to make a counter that will display these numbers.

Example:

$ python counter.py
1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30

Rules:

  • The numbers may be separated by spaces, tabs, or new lines.
  • The minimum number you must go to is 30 (48 in decimal).
    • You may also make the program print numbers forever until it is stopped.
  • Letters may be in uppercase or lowercase (A or a).
  • No built in functions allowed (that directly affect hexadecimal conversions/counting).
  • Leading zeros are allowed
  • It may start from 1 or 0
  • Shortest code wins!
\$\endgroup\$
19
  • \$\begingroup\$ @Sp3000 How built in are they? Converting decimal to hex? \$\endgroup\$ – phase Jul 12 '15 at 16:05
  • 4
    \$\begingroup\$ How about general base conversion functions then? \$\endgroup\$ – Sp3000 Jul 12 '15 at 16:11
  • 1
    \$\begingroup\$ @Sp3000 Sure (ignore this, 15 character limit) \$\endgroup\$ – phase Jul 12 '15 at 16:13
  • 1
    \$\begingroup\$ @Mauris Yes! That sure is going to be interesting... \$\endgroup\$ – phase Jul 13 '15 at 17:29
  • 1
    \$\begingroup\$ I was looking at the previous comments and it looked like you agreed to general base conversion functions when Sp3000 asked. Also does that include printf type functions? \$\endgroup\$ – Brad Gilbert b2gills Jan 17 '16 at 14:55

39 Answers 39

1
2
0
\$\begingroup\$

Haskell, 52 bytes

a="0123456789abcdef";main=mapM putStrLn$mapM id[a,a]
\$\endgroup\$
0
\$\begingroup\$

Python 2 - 57 bytes

h='0123456789ABCDEF'
' '.join([i+j for i in h for j in h])

This outputs 00 to FF, with spaces between.

\$\endgroup\$
0
\$\begingroup\$

Python 3, 157 bytes

As far as I can tell, this is the only submission so far that can keep counting upwards forever. (Of course, I can't read Haskell or Mumps, but they all seem to be doing a two-character loop...) EDIT: Okay, not the only one, but perhaps the only one in a non-golfy language?

n=bytearray(b'0')
while 1:
 print(repr(n)[12:-2]);p,o=len(n),1
 while o:
  p-=1;m=n[p];n[p]={57:65,70:48}.get(m,m+1);o=m==70
  if o and p==0:p=1;n[:0]+=b'0'

Proof of correctness (wraps the above in a function, changing only print to yield, and then loops as high as you like):

def golfed_code():
 n=bytearray(b'0')
 while 1:
  yield(repr(n)[12:-2]);p,o=len(n),1
  while o:
   p-=1;m=n[p];n[p]={57:65,70:48}.get(m,m+1);o=m==70
   if o and p==0:p=1;n[:0]+=b'0'

TEST_LIMIT = 100000

for n, h in enumerate(golfed_code()):
 assert hex(n).upper()=='0X'+h,'{} != {}'.format(hex(n).upper(),'0X'+h)
 if n == TEST_LIMIT:
  break

(Thanks to Sp3000 for saving me 3 bytes.)

\$\endgroup\$
5
  • \$\begingroup\$ @Sp3000: Not that I could find (though that doesn't mean one doesn't exist). Everything I tried took more characters, usually because I was constructing a new bytearray (and that's a big word). Also: fine, it's the only endless counter in a non-golfy language. ;-) \$\endgroup\$ – Tim Pederick Jul 16 '15 at 9:55
  • \$\begingroup\$ @Sp3000: I missed that first one (everything else I tried with b'' literals left me with a bytes, not a bytearray). But the second one means a is modified right along with n. \$\endgroup\$ – Tim Pederick Jul 16 '15 at 10:10
  • \$\begingroup\$ Right, thought they were immutable for some reason. I'm not sure why you're using bytearrays though, surely division/modulo + string building is shorter? \$\endgroup\$ – Sp3000 Jul 16 '15 at 10:15
  • \$\begingroup\$ @Sp3000: Because their string representation automatically ASCIIfies them. I think if I found an approach other than bytearray, I'd make it a new answer anyway; it'd be too much like "chuck it out and start over" to edit into this one. \$\endgroup\$ – Tim Pederick Jul 16 '15 at 10:18
  • \$\begingroup\$ It's now not the only example of an endless counter from a non-golf language put [R~] (|(0..9),|('A'..'F'))[.polymod: 16 xx*]for 0..* in Perl 6 \$\endgroup\$ – Brad Gilbert b2gills Jan 17 '16 at 15:57
0
\$\begingroup\$

Matlab: 47 bytes

q=[48:57,97:102,''];fliplr(char(combvec(q,q)'))

I don't know if it would be considered as counter. The code generates vector of hex numbers from 00 to ff. q is a string '0123456789abcdef' in double format. Combvec generates all possible pairs between q and q. Char converts it to string format and fliplr flips the columns to get:

00
01
...
08
09
0a
0b
0c
0d
0e
0f
10
11
...
fd
fe
ff
\$\endgroup\$
0
\$\begingroup\$

Ruby, 64 bytes

h='0123456789abcdef'.split //;h.each{|i|h.each{|j|print i,j,?\s}}

Explanation:

h = '0123456789abcdef'.split // # Split string of hex chars at
                                # matches of //, returns array
                                # splits array by characters 
h.each { |i|                    # loop 1
  h.each { |j|                  # loop 2
    print i,j,?\s               # print() can take multiple params,
                                # and it is the same as printing
                                # each one using a separate print().
                                # ?x is the same as 'x', so ?\s is
                                # the same as "\s", which is " ".
  }
}                              
\$\endgroup\$
0
0
\$\begingroup\$

Ahead, 42 bytes

Prints hex numbers starting at 0 forever. Will eventually overflow, though.

>t>:16;r
Cvn:/61\<
 >$>:10/7*v
^oNndo++0'<

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Scala, 84 bytes

val l=(0 to 9)++"A,B,C,D,E,F".split(",")
l.foreach(x=>l.foreach(y=>println(x+""+y)))

Slightly better built version, using what you could call a cartesian product. Try it online! Output goes with leading 0s and a trailing newline. Starts at 00 and counts up to FF (255 in base 10).

Scala, 89 bytes

for(x<-1 to 48)println(if(x%16>9)x/16+"a,b,c,d,e,f".split(",")(x%16-10)else x/16+""+x%16)

First answer, because why not, I like its hacks. Try it online! Output goes with leading 0s and a trailing newline. Starts at 01 and counts up to 30 (48 in base 10).

\$\endgroup\$
0
\$\begingroup\$

Julia - 58 bytes

a="0123456789abcdef"
for i in a
        for j in a
                print(i*j*" ")
        end
end
\$\endgroup\$
0
\$\begingroup\$

GFortran, 93 83 bytes

Prints 00 through ff. Saved 10 bytes using print loops instead of do.. enddo.

Try it Online!

character(16)H;H='0123456789abcdef'
print*,((H(i:i)//H(j:j),' ',j=1,16),i=1,16)
end

Old version: 93 bytes

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.