I <3条件

あなたは非常に長くて退屈な条件をコード内にたくさん持っています。

if flag == 1:

while have != needed:

if type == 7:

These can be transformed into their much more lovable
<3 conditionals counterparts:

if abs(flag - 1) + 2 <3:

while 3 - abs(have - needed) <3:

if 2 + abs(type - 7) <3:

仕事

Your 仕事 is to take a conditional and make it in terms of
<3. The only spacing that matters is that there is
none between < and 3.

Conditionals will be two expressions seperated by either
==, !=, >,
<, >= or <=.
Expressions will only contain addition, subtraction, unary negation
(-something), where there is one + or
- before each variables or numbers (except the first
which has nothing or - before it).
Numbers will be [0-9]+, and variables will be
[a-z]+. If the answer needs to use |x|
(The absolute value of x), use the abs()
function. You may assume that all variables are integers, and all
number constants in the input are < 1000.

The output does not need to be in it’s simplest form.
It does need to be a conditional like above, meaning that it is two
expressions only, seperated by one conditional sign, but it can
also use the abs function, enclosing a valid
expression, and then it acts like a variable, in terms of
validity.

If the input does not have an output for any value of a
variable, output a condition that is always false, but still in
terms of <3.

Part of the challenge is figuring out how to do it, but here are
the steps for the have != needed above:

have != needed
have - needed != 0
abs(have - needed) > 0
-abs(have - needed) < 0
3 - abs(have - needed) <3

Scoring

This is code-golf, so the shortest valid code, in bytes,
wins.

Test cases

(Note, these outputs aren’t the only outputs, but I tried to
simplify them.)

flag == 1
abs(flag - 1) + 2 <3

have != needed
3 - abs(have - needed) <3

type == 7
2 + abs(type - 7) <3

x > y
3 - x + y <3

x + 5 < -y
x + 8 + y <3

x + 6 <= y
x + 8 - y <3

-x >= y + 3
x + y + 5 <3

x < x
3 <3
# Unsimplified; both would be valid outputs.
x - x + 3 <3
ベストアンサー

Retina, 95 bytes

<=
<1+
>=
>-1+
(.*)(.=)(.*)
$2abs($1-($3))
==
2+
!=
3-
(.*)>(.*)
$2<$1
(.*)<(.*)
$1-($2)+3
$
<3

Try it
online!

かなり純粋な解決策ですが、私は改善を見つけることができませんでした。

これは単なる一連の置換です。

<=
<1+
>=
>-1+

Get rid of “or equal to” comparisons by replacing x <=
y
with x < 1 + y, and x >= y
with x > -1 + y.

(.*)(.=)(.*)
$2abs($1-($3))

== abs(x - y)x!= y!= abs(x -
y)

==
2+
!=
3-

全体的な置換が 3 - になるように、 ==2
+
3-abs(x-y) 2 +
abs(x-y)
コード>。

(.*)>(.*)
$2<$1

Normalize the direction of the remaining inequalities, replacing
x > y with y < x.

(.*)<(.*)
$1-($2)+3

Replace x < y with x - y + 3.

$
<3

文字列の最後にハートを追加します。

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です