扩大比较链


9

与大多数语言不同,Python a<b<c会像在数学中一样进行评估,实际上是比较三个数字,而不是将布尔值a<b与进行比较c。用C(以及许多其他语言)编写此代码的正确方法是a<b && b<c

在这个挑战中,您的任务是将这样任意长度的比较链从Python /直观表示扩展到如何用其他语言编写。

技术指标

  • 您的程序必须要处理运算符:==, !=, <, >, <=, >=
  • 输入将具有仅使用整数的比较链。
  • 不用担心一路比较的真实性,这纯粹是语法/语法上的挑战。
  • 输入将没有任何空格,以防止答案因分割空格而使解析变得琐碎。
  • 但是,您的输出可能只有一个空格,要么仅包含&&,要么包含比较运算符和&&,或者两者都不包含,但是要保持一致。

测试用例

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

这是 ,因此以字节为单位的最短代码胜出!

code-golf  parsing  conversion  syntax  code-golf  sequence  primes  code-challenge  geometry  optimization  code-golf  graph-theory  code-golf  number-theory  primes  integer  code-golf  source-layout  cops-and-robbers  code-golf  source-layout  cops-and-robbers  code-golf  sequence  primes  integer  code-golf  math  number-theory  primes  rational-numbers  code-golf  math  sequence  number-theory  primes  code-golf  string  code-golf  math  combinatorics  permutations  restricted-complexity  code-golf  array-manipulation  code-golf  number  sequence  code-golf  number  sequence  code-golf  binary-matrix  code-golf  math  tips  javascript  algorithm  code-golf  string  code-golf  number  sequence  code-golf  math  arithmetic  parsing  code-golf  number  sequence  primes  code-golf  string  ascii-art  geometry  integer  code-golf  geometry  code-golf  number  array-manipulation  code-golf  math  geometry  code-golf  number  sequence  arithmetic  integer  code-golf  string  kolmogorov-complexity  code-golf  number  code-golf  number  chess  code-golf  sequence  decision-problem  subsequence  code-golf  math  number  primes  code-golf  primes  permutations  code-golf  integer  probability-theory  statistics  code-golf  string  code-golf  sequence  decision-problem  parsing  board-game  code-golf  binary  graph-theory  code-golf  board-game  classification  tic-tac-toe  code-golf  ascii-art  polyglot  code-golf  date  code-golf  geometry 


我的两边可以有两个空格&&吗?
H.PWiz

@ H.PWiz不,对不起。
Maltysen '17

Answers:



5

外壳16 14字节

在每个运算符周围打印一个空格。

wJ"&&"m←C2X3ġ±

在线尝试!

说明:

                  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

好一个。您可以使用w而不是使用;更直接的方法来连接带有空格的字符串
Leo

哦,是的,我怎么没想到呢?
H.PWiz

3

(==|!=|<=?|>=?)可以 \D+
ovs '17

同样(?<!^|\d)可以(?<=\D)。同样,(?=\d+)也是不必要的,运算符将始终跟随一个操作数,此时您可以将放到运算符+之后\D。还有,$&而不是$1$2,然后可以通过捕获后面并向前看而不是捕获前面和后退来保存更多字节。
尼尔

(\D(\d+))(?=\D)在第1行和$1&&$2第2行上就足够了(22个字节)。在这里尝试。
凯文·克鲁伊森

2

V,37字节

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

在线尝试!

十六进制转储:

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                             .. ..

2

Clojure,88字节

更新:subs代替clojure.string/join

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

2

J59 46字节

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

在线尝试!

它过去如何运作

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

我们正在寻找运营商的界限。“斩首”和“缩减”的字符串分别变为零和1(其中0是数字),然后异或在一起。前面加零以匹配长度。

                   +/\                      </. ]     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.


1

木炭,29字节

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

相同基本算法的两种形式略有不同。输入字符串按字符进行迭代。找到数字后,它们将收集在变量中。当找到数字和运算符之间的边界时,将显示一个额外的“ &&”和变量,并清除该变量。变量最初被初始化为空格,以便第一个边界不会触发额外的“ &&”。


1

果冻,16字节

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

在线尝试!

说明:

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 '&&'

1

Java 8,46字节

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

说明:

在这里尝试。

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)

正则表达式说明:

(\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)

替换的分步示例:

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



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.