6
\$\begingroup\$

Task

Write a program/function that, given three integers n,a,b prints a regular expression which matches all of the base-n integers from a to b (and no integers outside of that range).

Your algorithm should, in theory, work for arbitrarily large integers. In practice, you may assume that the input can be stored in your data type. (don't abuse native data type, that's a standard loophole)

Input

Three integers, n, a, b where n is in the range 1-32. a and b may be taken as base-n string/character list/digit list.

Output

A single string, represent the regex.

Rules

  • Base-n integers use the first n digits of 0123456789ABCDEFGHIJKLMNOPQRSTUV, you can choose to use upper-case or lower-case.
  • a and b may be negative, denoted by a - in front of the integer
  • You may assume a ≤ b.
  • Any regex flavor is allowed.
  • The behavior of the regex on strings not a base-n integer is undefined.

Winning criteria

Answers will be scored by the length of the code in bytes. (so rules apply)

asked Dec 18, 2017 at 16:05
\$\endgroup\$
6
  • \$\begingroup\$ Should we be able to match integers inside other text, or are we guaranteed they will be on their own? \$\endgroup\$ Commented Dec 18, 2017 at 16:49
  • \$\begingroup\$ Also, should we be able to match negative integers too? If so, how will the sign be represented? \$\endgroup\$ Commented Dec 18, 2017 at 16:51
  • \$\begingroup\$ Yes it should be able to match negative integers. Looks like that got edited out of the question. \$\endgroup\$ Commented Dec 18, 2017 at 17:06
  • \$\begingroup\$ You can assume the integer will be on its own \$\endgroup\$ Commented Dec 18, 2017 at 17:07
  • \$\begingroup\$ So, are negative integers represented with a - in the front or something else? \$\endgroup\$ Commented Dec 18, 2017 at 17:10

3 Answers 3

5
\$\begingroup\$

05AB1E, 17 bytes

×ばつsÄIB«}'|ý

Try it online!

For some reason I don't seem to be able to replace I with 3.

answered Dec 18, 2017 at 19:19
\$\endgroup\$
2
\$\begingroup\$

Jelly, 24 bytes

ƓØBḣḊ;0
rμṠṾṖ$€ż8Aṃ¢¤j"|

Try it online!

Take 2 input from command line argument as decimal number, and base from stdin.

answered Dec 18, 2017 at 16:44
\$\endgroup\$
2
  • \$\begingroup\$ Looks like this doesn't work for negative numbers. \$\endgroup\$ Commented Dec 18, 2017 at 19:19
  • \$\begingroup\$ @EriktheOutgolfer Fixed. \$\endgroup\$ Commented Dec 19, 2017 at 1:21
1
\$\begingroup\$

Python 2, 157 bytes

n,a,b=input();s=o=''
for i in range(a,b+1):
 if i<0:i=abs(i);s='-'
 if i==0:o='0'+o
 while i:i,r=i/n,i%n;o=[`r`,chr(55+r)][r>9]+o
 o='|'+s+o;s=''
print o[1:]

Try it online!

answered Dec 18, 2017 at 17:10
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.