10
\$\begingroup\$

Unlike most languages, Python evaluates a<b<c as it would be done in mathematics, actually comparing the three numbers, as opposed to comparing the boolean a<b to c. The correct way to write this in C (and many others) would be a<b && b<c.

In this challenge, your task is to expand such comparison chains of arbitrary lengths from the Python/intuitive representation, to how it would be written in other languages.

Specifications

  • Your program will have to handle the operators: ==, !=, <, >, <=, >=.
  • The input will have comparison chains using only integers.
  • Don't worry about the trueness of any of the comparisons along the way, this is purely a parsing/syntactical challenge.
  • The input won't have any whitespace to prevent answers that trivialize parsing by splitting on spaces.
  • However, your output may have a single space surrounding either just the &&'s, or both the comparison operators and the &&'s, or neither, but be consistent.

Test Cases

Input Output --------------------------------------------------------------- 3<4<5 3<4 && 4<5 3<4<5<6<7<8<9 3<4 && 4<5 && 5<6 && 6<7 && 7<8 && 8<9 3<5==6<19 3<5 && 5==6 && 6<19 10>=5<7!=20 10>=5 && 5<7 && 7!=20 15==15==15==15==15 15==15 && 15==15 && 15==15 && 15==15 

This is , so shortest code in bytes wins!

\$\endgroup\$
3
  • \$\begingroup\$ related \$\endgroup\$ Commented Oct 9, 2017 at 3:50
  • \$\begingroup\$ Can I have two spaces either side of the &&? \$\endgroup\$ Commented Oct 9, 2017 at 4:34
  • \$\begingroup\$ @H.PWiz nope, srry. \$\endgroup\$ Commented Oct 9, 2017 at 4:35

14 Answers 14

6
\$\begingroup\$

Retina, 33 22 17 bytes

-5 bytes thanks to @MartinEnder

-2`\D(\d+) $&&&$1 

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ @MartinEnder A pity 1>-2 doesn't work to limit from both ends at once... \$\endgroup\$ Commented Oct 9, 2017 at 7:57
  • \$\begingroup\$ @Neil yeah, Retina 1.0 will get a new limit syntax where that's possible. \$\endgroup\$ Commented Oct 9, 2017 at 7:57
  • \$\begingroup\$ Could you post an explanation? \$\endgroup\$ Commented Oct 9, 2017 at 19:17
5
\$\begingroup\$

Husk, 16 14 bytes

Prints a space around each operator.

wJ"&&"m←C2X3ġ± 

Try it online!

Explanation:

 Implicit input, e.g "3<4<5<6" ġ± Group digits ["3","<","4","<","5","<","6"] X3 Sublists of length 3 [["3","<","4"],["<","4","<"],["4","<","5"],["<","5","<"],["5","<","6"]] C2 Cut into lists of length 2 [[["3","<","4"],["<","4","<"]],[["4","<","5"],["<","5","<"]],[["5","<","6"]]] m← Take the first element of each [["3","<","4"],["4","<","5"],["5","<","6"]] J"&&" Join with "&&" ["3","<","4","&&","4","<","5","&&","5","<","6"] w Print, separates by spaces 
\$\endgroup\$
2
  • \$\begingroup\$ Nice one. You could use w instead of ; for a more direct approach to joining strings with spaces \$\endgroup\$ Commented Oct 9, 2017 at 4:53
  • \$\begingroup\$ Oh yes, how didn't I think of that? \$\endgroup\$ Commented Oct 9, 2017 at 4:54
3
\$\begingroup\$

Retina, 42 47 22 bytes

Massively golfed thanks to Kevin Cruijssen

(\D(\d+))(?=\D) $1&&$2 

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ (==|!=|<=?|>=?) can be \D+ \$\endgroup\$ Commented Oct 9, 2017 at 6:37
  • \$\begingroup\$ Similarly (?<!^|\d) can be (?<=\D). Also the (?=\d+) is unnecessary, the operator will always be followed by an operand, at which point you can drop the + after the \D. There's also $& instead of $1$2, and then a further byte can be saved by capturing behind and looking ahead instead of capturing ahead and looking behind. \$\endgroup\$ Commented Oct 9, 2017 at 8:02
  • \$\begingroup\$ (\D(\d+))(?=\D) on line 1, and $1&&$2 on line two is enough (22 bytes). Try it here. \$\endgroup\$ Commented Oct 9, 2017 at 14:28
2
\$\begingroup\$

V, 37 bytes

òͨ䫄0-9& ]«©¨ä«©¨„0-9& ]«©/±² ¦¦ ²³ 

Try it online!

Hexdump:

00000000: f2cd a8e4 ab84 302d 3926 205d aba9 a8e4 ......0-9& ].... 00000010: aba9 a884 302d 3926 205d aba9 2fb1 b220 ....0-9& ]../.. 00000020: a6a6 20b2 b3 .. .. 
\$\endgroup\$
2
\$\begingroup\$

Clojure, 88 bytes

Update: subs instead of clojure.string/join.

#(subs(apply str(for[p(partition 3 2(re-seq #"(?:\d+|[^\d]+)" %))](apply str" && "p)))4) 
\$\endgroup\$
2
\$\begingroup\$

J, 59 46 bytes

4(}.;)_2{.\3' && '&;\]</.~0+/\@,2~:/\e.&'!<=>' 

Try it online!

How it used to work

 (0 , }. ~:&(e.&'!<=>') }:) 

We’re looking for operator boundaries. “Beheaded” and “curtailed” strings are turned into zeroes and ones where 0s are digits, then xored together. Prepend zero to match the length.

 +/\ </. ] Split on boundaries. ┌──┬──┬─┬─┬─┬──┬──┐ │10│>=│5│<│7│!=│20│ └──┴──┴─┴─┴─┴──┴──┘ 3' && '&;\ Add && to infixes of 3. ┌────┬──┬──┬──┐ │ && │10│>=│5 │ ├────┼──┼──┼──┤ │ && │>=│5 │< │ ├────┼──┼──┼──┤ │ && │5 │< │7 │ ├────┼──┼──┼──┤ │ && │< │7 │!=│ ├────┼──┼──┼──┤ │ && │7 │!=│20│ └────┴──┴──┴──┘ _2{.\ Take even numbered rows. ;}., Concatenate after dropping the first box. 
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 29 bytes

≔ ηFθ«¿›ι9«F›η!⁺&&η≔ωη»≔⁺ηιηι ≔ ηFθ«F∧›ι9›η!⁺&&η≔⎇›ι9ω⁺ηιηι 

Two slightly different formulations of the same basic algorithm. The input string is iterated by character. As digits are found they are collected in a variable. When a boundary between a number and operator is found, an extra "&&" plus the variable is printed and the variable is cleared. The variable is initially initialised to a space so that the first boundary doesn't trigger the extra "&&".

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

Jelly, 16 bytes

e€ØDŒg⁸ṁṡ3m2j⁾&& 

Try it online!

Explanation:

e€ØDŒg⁸ṁṡ3m2j⁾&& Uses Jelly stringification, thus full program only e€ØD For each each char, 1 if it's in '0123456789', otherwise 0 Œg Split into longest runs of equal elements ⁸ṁ Reshape original input like the list we just made Reshaping will separate numbers from operators ṡ3 Get contiguous sublists of length 3 m2 Keep every second item, starting from the first This will keep every (number operator number) and remove every (operator number operator) substring j⁾&& Join with '&&' 
\$\endgroup\$
1
\$\begingroup\$

Java 8, 46 bytes

s->s.replaceAll("(\\D(\\d+))(?=\\D)","$1&&$2") 

Explanation:

Try it here.

s-> // Method with String as both parameter and return-type s.replaceAll("(\\D(\\d+))(?=\\D)", "$1&&$2") // Replace the match of the regex // with the second String // End of method (implicit / single-line return-statement) 

Regex explanation:

(\D(\d+))(?=\D) # 1) For all matches of this regex (\d+) # Capture group 2: a number (one or more digits) (\D \d+ ) # Capture group 1: anything but a number + the number # (`\D` will match the `=`, `!`, `<`, or `>`) (?=\D) # Look-ahead so everything after the match remains as is $1&&$2 # 2) Replace it with $1 # Result of capture group 1 (trailing part of the equation + the number) && # Literal "&&" $2 # Result of capture group 2 (the number) 

Step by step example of the replacements:

Initial: 10>=5<7!=20 Match of first replacement: =5 Replace with: =5&&5 After first replacement: 10>=5&&5<7!=20 Match of second replacement: <7 Replace with: <7&&7 After second replacement (result): 10>=5&&5<7&&7!=20 
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 47 + 1 (-p) = 48 bytes

$\.=$1." && "x(!!$')while s/(\d+\D+(\d+))/$2/}{ 

Try it online!

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

Python 2, 60 59 58 57 bytes

lambda s:re.sub(r'(\D(\d+))(?=\D)',r'\1&&\2',s) import re 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ re.sub(r'\D(\d+)(?=\D)',r'\g<0>&&\1',s) saves 1 byte. \$\endgroup\$ Commented Oct 9, 2017 at 7:59
0
\$\begingroup\$

Ruby, 37 bytes

->s{s.gsub(/\D(\d+)(?=\D)/,'\&&&\1')} 

Try it online!

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

JavaScript (SpiderMonkey), 43 bytes

f=s=>s.replace(/(\W+(\d+))(?!$)/g,"$1&&$2") 

Try it online!

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

Jelly, 15 bytes

e€ØDnƝkṡ3m2j⁾&& 

Try it online!

Outputs with no spaces around the &&, but the Footer in the TIO link adds the spaces to make the output easier to read

How it works

e€ØDnƝkṡ3m2j⁾&& - Main link. Takes a string S on the left e€ØD - Replace digits with 1, otherwise 0 Ɲ - To overlapping pairs [a,b]: n - a ≠ b k - Split S on truthy elements, grouping digits together ṡ3 - Split into overlapping lists of length 3 m2 - Take every other element j⁾&& - Join by "&&" 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.