21
\$\begingroup\$

Challenge:

Input:

Two integer parameters a and b (where a<b and the difference is at least 2)

Output:

Output or return this text, where a and b are filled in:

(a,b) = ]a,b[ = {a<x<b}   = {a<x&&x<b}   = a+1..b-1
[a,b) = [a,b[ = {a<=x<b}  = {a<=x&&x<b}  = a..b-1
(a,b] = ]a,b] = {a<x<=b}  = {a<x&&x<=b}  = a+1..b
[a,b] = [a,b] = {a<=x<=b} = {a<=x&&x<=b} = a..b

Challenge rules:

  • I/O is flexible. Can be printed to STDOUT, returned as a string/character-array, etc. Can be inputted as two integers, decimals, strings (not sure why since you need to calculate the a+1 and b-1, but be my guest..), etc.
  • Any amount of leading and/or trailing new-lines are allowed, and any amount of trailing and/or leading spaces for each line is allowed.
  • The spaces at the equal signs (including those to align them in the same column) are mandatory, spaces between the other characters are not allowed.
  • a+1 and b-1 are replaced with the correct values after these calculations.
  • You are not allowed to use instead of <=.
  • You are not allowed to use & instead of &&.
  • You are allowed to output the numbers with .0 (as long as it's consistent, and not more than one decimal zero).
  • You can assume a is at least 2 lower than b (in order for (a,b) to be correct).
  • The lines should be output in the order shown.

Example:

Input: a=-5, b=10
Output:

(-5,10) = ]-5,10[ = {-5<x<10}   = {-5<x&&x<10}   = -4..9
[-5,10) = [-5,10[ = {-5<=x<10}  = {-5<=x&&x<10}  = -5..9
(-5,10] = ]-5,10] = {-5<x<=10}  = {-5<x&&x<=10}  = -4..10
[-5,10] = [-5,10] = {-5<=x<=10} = {-5<=x&&x<=10} = -5..10

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

PS: For those who had seen this challenge in the Sandbox when it was still a challenge with a and b hard-coded, I've changed it to an input challenge to prevent boring hard-coded and encoded answers like we usually see with KC challenges.

\$\endgroup\$
  • 2
    \$\begingroup\$ So for a=5, b=6 still output (5,6) = ... = 6...5? \$\endgroup\$ – l4m2 Apr 4 '18 at 11:03
  • 1
    \$\begingroup\$ @l4m2 Ah.. hadn't thought about that. You can assume the a and b will always be at least 2 apart. I'll edit the challenge. \$\endgroup\$ – Kevin Cruijssen Apr 4 '18 at 11:18
  • 2
    \$\begingroup\$ It turned out to be much more fun than I was expecting. Nice challenge! \$\endgroup\$ – Arnauld Apr 4 '18 at 11:54
  • \$\begingroup\$ @DigitalTrauma Sorry, but this exact order. \$\endgroup\$ – Kevin Cruijssen Apr 5 '18 at 7:02

16 Answers 16

2
\$\begingroup\$

Stax, 74 bytes

ÉyU≤₧pΔz▀σ┬`♪•a≤☻Σ╕←k►¬╗Ö)ßâL╫§▐ƒ┼°╚íS3:Y¶7]7♂e╖à╙ô≥;M0h8♦Oún┼ë`←B╠╫║┌♂α▲╚

Run and debug it

This uses stax's string templates heavily. Unpacked, ungolfed, and commented, it looks like this.

Y                   save second input in Y register (first is already in X)
.)].([|*            cross product of ")]" and "(["; this produces [")(", ")[", "](", "]["]
{                   begin block to map over interval delimiters
  E"`x,`y"a++       push delimiters separately, then wrap them around the inputs    e.g. "(-5,10)"
  c"(])["|t         copy last value, then replace parentheses with braces           e.g. "]-5,10["
  ih'=              push half the iteration index and "="                           e.g. 0 "="
  |;'=              push iteration parity (alternating 0 and 1) and "="             e.g. 0 "=" 0 "="
  "{`x<`*x<`*`y}"   multiply each equal sign by its occurrence, and template        e.g. "{-5<x<10}"
  c'x.x&:mR         copy last value, then replace "x" with "x&&x"                   e.g. "{-5<x&&x<10}"
  yvih xi|e         calculate final bounds offsets                                  e.g. -5 1 10 -1
  "`+..`+"          add inputs to offsets, and embed in template                    e.g. "-4..9"
  5l                combine last 5 values into array
m                   map [")(", ")[", "](", "]["] using block
:<                  left-align grid colums to add extra spaces
m" = "*             for each row, join with " = " and output

Run this one

\$\endgroup\$
10
\$\begingroup\$

JavaScript (ES6), 184 182 181 180 bytes

Takes input in currying syntax (a)(b). Returns an array of 4 strings.

a=>b=>[1,2,3,4].map(k=>'31,23 = 31,23 = {10x72}4{10x&&x72}45..6'.replace(/\d/g,(n,i)=>[(+n?k<3:k&1)?'<':'<=',a,b,'][)([[]('[(i*17^k*718)%9],'   = '.slice(k/2),a+k%2,b-(k<3)][n%7]))

Try it online!

How?

For each row k with 1 ≤ k ≤ 4, we start with the following template:

"31,23 = 31,23 = {10x72}4{10x&&x72}45..6"

and replace each decimal digit n at position i according to the following table:

  n  | Replaced with           | Code
-----+-------------------------+------------------------------------------
 0,7 | comparison operator     | (+n ? k < 3 : k & 1) ? '<' : '<='
  1  | a                       | a
  2  | b                       | b
  3  | interval bound          | '][)([[]('[(i * 17 ^ k * 718) % 9]
  4  | a substring of '   = '  | '   = '.slice(k / 2)
  5  | either 'a' or 'a + 1'   | a + k % 2
  6  | either 'b' or 'b - 1'   | b - (k < 3)
\$\endgroup\$
4
\$\begingroup\$

Python 2, 225 203 195 bytes

a,b=input()
for d in 0,1:
 for m in 0,1:k=`a`+','+`b`;o='{'+`a`+'<'+m*'=';c='x<'+d*'='+`b`+'}'+'  '[m+d:];print' = '.join(['(['[m]+k+')]'[d],']['[m]+k+'[]'[d],o+c,o+'x&&'+c,`a+1-m`+'..'+`b-1+d`])

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 2, 187 bytes

t=a,b=input()
for j in 1,0:
 for i in 1,0:print"%%s%d,%d%%s = "%t*2%('[('[i],'])'[j],'[]'[i],']['[j])+"{%d<%sx%%s<%s%d}%s = "%(a,'='[i:],'='[j:],b,' '*(i+j))*2%('','&&x')+`a+i`+'..'+`b-j`

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Java (JDK 10), 251 bytes

a->b->("(a,b)q]a,b[q{a<x<b}  q{a<x&&x<b}  q"+-~a+".."+~-b+"\n[a,b)q[a,b[q{a<=x<b} q{a<=x&&x<b} qa.."+~-b+"\n(a,b]q]a,b]q{a<x<=b} q{a<x&&x<=b} q"+-~a+"..b\n[a,b]q[a,b]q{a<=x<=b}q{a<=x&&x<=b}qa..b").replace("a",a+"").replace("b",b+"").replace("q"," = ")

Try it online!

Credits

\$\endgroup\$
  • \$\begingroup\$ I'm still golfing... \$\endgroup\$ – Olivier Grégoire Apr 4 '18 at 12:14
  • \$\begingroup\$ Almost done with golfing? I got a suggestion to remove 85 bytes. ;) \$\endgroup\$ – Kevin Cruijssen Apr 4 '18 at 12:38
  • \$\begingroup\$ @KevinCruijssen I'm done but I removed far less than 85 bytes... \$\endgroup\$ – Olivier Grégoire Apr 4 '18 at 12:45
  • 1
    \$\begingroup\$ 251 bytes with three boring .replace. \$\endgroup\$ – Kevin Cruijssen Apr 4 '18 at 12:46
  • \$\begingroup\$ Yeah, basically my first solution using replace indeed, instead of formatting. Nice. Boring indeed, but nice! :-) \$\endgroup\$ – Olivier Grégoire Apr 4 '18 at 12:48
3
\$\begingroup\$

Perl 5, 181 bytes

I thought this would have worked out a lot shorter...

$_="sd,ds = sd,ds = {dsxsd}s= {dsx&&xsd}s= d..d
"x4;s!s!("()][<<   [)[[<=<  (]]]<<=  [][]<=<= "=~s/[<= ]+/$&$&/gr=~/ +|<=|./g)[$-++]!ge;s/d/$F[$x%2]+{8,1,9,-1,19,-1,28,1}->{$x++}/ge

Try it online!

Explanation

Originally this was using a printf format string, but just having s and d was shorter when combined with s///.

First the format string is built into $_ and quadruplicated, then all ss are replaced with the corresponding bracket, <, <= or spaces, depending on the replacement index. I hoped to save some more bytes with the duplication of the last 5 chars of each block, but this only ended up saving 2 bytes. The resultant string is split up into elements of spaces, <= or single characters.

Finally all ds are replaced with the desired number which is adjusted based on the index of the current replacement via a hash key.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 190 189 bytes

x=>y=>`(0)7]0[7{1<x<2}  7{1<52}  73..4
[0)7[0[7{18x<26{185261..4
(0]7]0]7{1<x826{1<5=263..2
[0]7[0]7{18x82}7{185=2}71..2`.replace(/\d/g,d=>[[x,y],x,y,x+1,y-1,`x&&x<`,`}  = `,` = `,`<=`][d])

Try it online

\$\endgroup\$
3
\$\begingroup\$

m4, 194

Seems like a job for a macro processor. Not sure if m4 meets our standards for a programming language. It does have looping ability and arithmetic eval, so I assume its close to the mark.

define(l,`$1a,b$2 = $3a,b$4 = {a<$5x<$6b} $8= {a<$5x&&x<$6b} $8= $7')dnl
l(`(',`)',],[,,,incr(a)..decr(b),`  ')
l([,`)',[,[,=,,a..decr(b),` ')
l(`(',],],],,=,incr(a)..b,` ')
l([,],[,],=,=,a..b,)

This is my first non-trivial look at m4, so I suspect there are more golfing opportunities I've missed.

Inputs are passed using -D macro definitions at the command line. Not sure if anything needs to be added to the score for these, as as far as I can tell this is the only way to meaningfully pass parameters:

$ m4 -Da=-5 -Db=10 intnot.m4
(-5,10) = ]-5,10[ = {-5<x<10}   = {-5<x&&x<10}   = -4..9
[-5,10) = [-5,10[ = {-5<=x<10}  = {-5<=x&&x<10}  = -5..9
(-5,10] = ]-5,10] = {-5<x<=10}  = {-5<x&&x<=10}  = -4..10
[-5,10] = [-5,10] = {-5<=x<=10} = {-5<=x&&x<=10} = -5..10 $

Try it online.


Thanks @Dennis for adding m4 to TIO so quickly!

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't see why m4 wouldn't be a valid programming language to answer in, but what do I know. ;) As for the -D arguments it's completely fine. In my challenge description I stated I/O is completely flexible, so whether you take input as STDIN, function parameters, program command-line arguments, compiler-flags, reading a file, or whatever other input-method you can think of is completely up to you. \$\endgroup\$ – Kevin Cruijssen Apr 5 '18 at 15:39
2
\$\begingroup\$

Python 2, 277 199 193 189 bytes

a,b=input()
for i in 4,3,2,1:x,y=i%2,i>2;e='=';p=`a`+','+`b`;print'(['[x]+p+'])'[y],e,']['[x]+p+']['[y],e,2*('{%d<%s<%s%d} %s= '%(a,e*x+'%sx',e[y:],b,i/2*' '))%('','x&&')+`a+1-x`+'..'+`b-y`

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 2*('{%d<%s<%s%d} %s= '%(a,e*x+'%sx',e[y:],b,i/2*' '))%('','x&&') -> 2*('{%d<%s%%sx<%s%d} %s= '%(a,e*x,e[y:],b,i/2*' '))%('','x&&') saves two :) \$\endgroup\$ – Jonathan Allan Apr 4 '18 at 19:47
1
\$\begingroup\$

Excel, 399 bytes

="("&A1&","&B1&") = ]"&A1&","&B1&"[ = {"&A1&"<x<"&B1&"}   = {"&A1&"<x&&x<"&B1&"}   = "&A1+1&".."&B1-1&"
 ["&A1&","&B1&") = ["&A1&","&B1&"[ = {"&A1&"<=x<"&B1&"}  = {"&A1&"<=x&&x<"&B1&"}  = "&A1&".."&B1-1&"
 ("&A1&","&B1&"] = ]"&A1&","&B1&"] = {"&A1&"<x<="&B1&"}  = {"&A1&"<x&&x<="&B1&"}  = "&A1+1&".."&B1&"
 ["&A1&","&B1&"] = ["&A1&","&B1&"] = {"&A1&"<=x<="&B1&"} = {"&A1&"<=x&&x<="&B1&"} = "&A1&".."&B1

Nothing particularly interesting here.

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 224 237 bytes

f(a,b,c,m,n,o){for(c=0;++c<5;printf("%c%d,%d%c = %c%d,%d%c = {%d<%sx<%s%d}%*s= {%d<%sx&&x<%s%d}%*s= %d..%d\n","[("[m],a,b,"])"[n],"[]"[m],a,b,"]["[n],a,"="+m,"="+n,b,o,"",a,"="+m,"="+n,b,o,"",a+m,b-n)){m=c%2;n=c<3;o=3-c/2;}}

Try it online!

Moving the "<[=]" into the format string allowed me to remove the array altogether. Also, moving printf() into the for loop saved a semicolon.

Original answer

f(a,b,c,m,n,o){char*e[]={"<=","<"};for(c=0;++c<5;){m=c%2;n=c<3;o=3-c/2;printf("%c%d,%d%c = %c%d,%d%c = {%d%sx%s%d}%*s= {%d%sx&&x%s%d}%*s= %d..%d\n","[("[m],a,b,"])"[n],"[]"[m],a,b,"]["[n],a,e[m],e[n],b,o,"",a,e[m],e[n],b,o,"",a+m,b-n);}}

Try it online!

There's nothing particularly noteworthy here: I used the usual tricks for shaving the size of the function (hoisting int autos into the function header, using K&R style, indexing into string constants.) Storing the {"<=","<"} into an array proved more size-efficient than inserting a format specifier for this challenge as it was used more than once.

\$\endgroup\$
1
\$\begingroup\$

Javascript, 273 258 232 bytes

Thanks Kevin Cruijssen for saving me 15 bytes !

e=>f=>'(j)h]j[h{a<x<b}  h{a<x&&x<b}  hc..d\n[j)h[j[h{a<=x<b} h{a<=x&&x<b} ha..d\n(j]h]j]h{a<x<=b} h{a<x&&x<=b} hc..b\n[j]h[j]h{a<=x<=b}h{a<=x&&x<=b}ha..b'.replace(/a|b|c|d|h|j/g,m=>{return{a:e,b:f,c:e+1,d:f-1,h:" = ",j:e+","+f}[m]})

Try it online

Thanks for TFeld for giving me that idea, saving around 60 bytes from my original answer.

Ungolfed :

e => f => '(j)h]j[h{a<x<b}  h{a<x&&x<b}  hc..d'
 + '\n[j)h[j[h{a<=x<b} h{a<=x&&x<b} ha..d'
 + '\n(j]h]j]h{a<x<=b} h{a<x&&x<=b} hc..b'
 + '\n[j]h[j]h{a<=x<=b}h{a<=x&&x<=b}ha..b'
.replace(/a|b|c|d|h|j/g, m=>{
        return {a:e,b:f,c:e+1,d:f-1,h:" = ",j:e+","+f}[m]
    }
)

Javascript (original answer), 340 bytes

(a,b)=>alert(`(${a},${b}) = ]${a},${b}[ = {${a}<x<${b}}   = {${a}<x&&x<${b}}   = ${a+1}..${b-1}\n[${a},${b}) = [${a},${b}[ = {${a}<=x<${b}}  = {${a}<=x&&x<${b}}  = ${a}..${b-1}\n(${a},${b}] = ]${a},${b}] = {${a}<x<=${b}}  = {${a}<x&&x<=${b}}  = ${a+1}..${b}\n[${a},${b}] = [${a},${b}] = {${a}<=x<=${b}} = {${a}<=x&&x<=${b}} = ${a}..${b}\n`)
\$\endgroup\$
  • 1
    \$\begingroup\$ The new-line at {a<=x&&\nx<=b} shouldn't be there, and you're missing the first (a,b) = . As for some golfing: instead of alert you can just return the result. (m)=> can be m=>. (e,f)=> can be e=>f=>. And you can use g directly instead of creating the let g=: m=>{return{a:e,b:f,c:e+1,d:f-1}[m]}. Try it online 258 bytes. And probably more can be golfed, I'm not too skilled in JS.. \$\endgroup\$ – Kevin Cruijssen Apr 4 '18 at 9:43
  • \$\begingroup\$ Well you look more skilled than I am. Thanks for the tips, i'll add them to the answer \$\endgroup\$ – The random guy Apr 4 '18 at 9:46
  • \$\begingroup\$ Well, I'm mostly someone who golfs in Java (or Whitespace), and these golfing tips also apply to Java answers, which is mostly why I know them: (m)-> to m->; (e,f)-> to e->f->, and var g=new int[]{...}return g[m] to return new int[]{...}[m]. ;) Also, I've been active on this SO for little over two years now, so I've seen JS answers use these kind of things before as well. \$\endgroup\$ – Kevin Cruijssen Apr 4 '18 at 9:49
1
\$\begingroup\$

Retina, 216 bytes

.+
$&()][<<  ¶$&[)[[<=< ¶$&(]]]<<= ¶$&[][]<=<=
((.*),(.*))(.)(.)(.)(.)(<=?)(<=?)( *)
$4$1$5 = $6$1$7 = {$2$8x$9$3}$10 = {$2$8x&&x$9$3}$10 = $2_$8..$3_$9
_<=

\d+_
$&*___
T`<`_` _+<|\.-_+<
___<
_
__<
-1
_(_*)
$.1
-0
0

Try it online! Explanation:

.+
$&()][<<  ¶$&[)[[<=< ¶$&(]]]<<= ¶$&[][]<=<=
((.*),(.*))(.)(.)(.)(.)(<=?)(<=?)( *)
$4$1$5 = $6$1$7 = {$2$8x$9$3}$10 = {$2$8x&&x$9$3}$10 = $2_$8..$3_$9

Build up the main bulk of the result.

_<=

If the variable is involved in a loose inequality, then the value is inclusive, so we can delete the placeholder.

\d+_
$&*___

Convert the value to unary and add 2.

T`<`_` _+<|\.-_+<

Remove the placeholder for a strict lower inequality or a negative strict upper inequality. It's still had 2 added, but 1 will be subtracted later, giving the desired result.

___<
_

Subtract 2 from the other non-zero strict inequalities, restoring the original value, from which 1 will be subtracted later.

__<
-1

Change a strict upper inequality of 0 to -1.

_(_*)
$.1

Subtract 1 from the remaining strict inequalities and convert to decimal.

-0
0

Fix up another edge case.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 180 bytes:

lambda a,b:[eval('f"'+"{%r[j]}{a},{b}{%r[i]} = "*2%('([',')]','][','[]')+"{{{a}<{'='[:j]}x%s<{'='[:i]}{b}}}{'  '[i+j:]} = "*2%('','&&x')+'{a+j}..{b-i}"')for i in(1,0)for j in(1,0)]

explanation

Basically builds an f-string that is evaluated in a list comprehension. Old-style % string interpolation is used to delay evaluating the expressions until the f-string is evaluated.

lambda a,b:[
    eval(
      'f"' +                                                            # f-string prefix
      "{%r[j]}{a},{b}{%r[i]} = "*2%('([',')]','][','[]') +              # first two terms
      "{{{a}<{'='[:j]}x%s<{'='[:i]}{b}}}{'  '[i+j:]} = "*2%('','&&x') + # second two terms
      '{a+j}..{b-i}"'                                                   # last term
      )
    for i in(1,0)for j in(1,0)
    ]

The first part of the string, 'f"', will become the prefix for the f-string.

The second part of the string builds the format string for the first two interval expressions. %r is used to save from needing to put quotes in the format, i.e., "{%r[j]}" is the same as "{'%s'[j]}". When the f-string is evaluated, the correct bracket is selected.

The third part of the string builds the next two interval expressions.

The last part formats the "a..b" part of the f-string.

The assembled f-string looks like: f"{'(['[j]}{a},{b}{')]'[i]} = .... = {a+j}..{b-i}"

When the f-string is evaluated, all the expressions in braces {} are are replaced by their value. So, {a} gets replaced by the value of a, and {'(['[j]} gets replaced by ( if j is 0 or [ if j is 1.

\$\endgroup\$
1
\$\begingroup\$

SOGL V0.12, 110 bytes

<ŗŗ}”⁴
"{ŗ<ŗx³
W}↔b ,e++Κ+²
4∫2\f»¹Aa{Ƨ[(²a{Ƨ[]²ba{ =*}eο+++:³⁴;³&&x⁴a_beh+H⁶;!+ƨ.+Κο++++}⁰№Iā;0E{┼ē4=‽"δY↑æ‘┼

Try it Here!

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

Python 3, 248 bytes

def f(a,b):
 l=[['(',')','[','<',1],['[',']',']','<=',0]]
 r=[0,1]
 for i in r:
  for j in r:
   print(('%s%d,%d%s='*2+'{%d%sx%s%d}={%d%sx&&x%s%d}=%d..%d')%((l[j][0],a,b,l[i][1],l[1-j][2],a,b,l[i][2])+(a,l[j][3],l[i][3],b)*2+(a+l[j][4],b-l[i][4])))

Try it online!

\$\endgroup\$

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.