乌鸦与出租车


36

想象一下,行驶到一个点躺在一个远离水平英里,从当前位置英里远垂直。或者换句话说,从(0, 0)起点到终点(a, b)。您需要旅行多远?这似乎是一个简单的问题,但是答案取决于您问的是谁。如果你是一只乌鸦,你可以前往直线距离,行驶距离仅仅是欧氏距离(a, b)。这是

sqrt(a^2 + b^2)

但是,如果您只是一个无聊的人,那么您真的不想走那么远,因此您需要乘出租车。大多数出租车都不会直线驶向您的目的地,因为他们通常会试图留在道路上。因此,您最终要行驶的实际距离是垂直距离和水平距离的总和。或公式为:

abs(a) + abs(b)

这称为出租车距离。这张图片很好地展示了两者之间的区别:

enter image description here

要前往(6, 6),乌鸦只能在绿线上飞行,所以距离6 * sqrt(2)约为8.49。出租车可以走红色,蓝色或黄色的道路,但全部走12条路。

这引出了我要问的真正问题。如果一只乌鸦和一辆出租车都从起点离开(0, 0),然后行驶到终点(a, b),出租车的路程将持续多长时间?或者,用更多的数学术语来说,

给定二维向量,请确定向量的norm2与向量的norm1之间的差。

您必须编写最短的程序或函数才能回答此问题。您可以选择将“ a”和“ b”作为两个单独的输入,或作为两个项目的元组。您可以采用任何合理的格式进行输入和输出。如果差异是非整数,则必须至少精确到两位小数。

您始终可以假设'a'和'b'将是整数,并且它们都不都是0。(尽管它们中的任何一个都可能为零)

像往常一样,存在标准漏洞,并尝试使程序尽可能短(以字节计)。

我将对所有答案进行投票,该答案将说明代码的工作原理,并演示用于保存字节的任何绝妙技巧。

以下是一些示例,可用于测试您的代码:

#input    #output
3, 4      2
-3, 4     2
-3, -4    2
6, 6      3.51
42, 0     0
10, 10    5.86
3, 3      1.76

打高尔夫球吧!:)


9
我们可以将输入作为复数吗?
Uriel'7

我认为测试用例10,10需要为5.86,因为它出来了5.85786...,您将其四舍五入到了下面。
numbermaniac

4
我首先读到《牛与出租车》的标题,希望能找到一些涉及碰撞物理的东西……
MooseBoys

我们可以给出负面结果吗?
2015年

@Adám号。(从概念上讲,您返回的距离始终是正值)
DJMcMayhem

Answers:


108

出租车7394 3773字节

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Tom's Trims.Go to Tom's Trims:n.[a]Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l 2 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Multiplication Station.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 2 l 2 r.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to The Underground.Go to Cyclone:n 1 l 1 l.Pickup a passenger going to Multiplication Station.Go to The Underground:n 2 r 2 r.Switch to plan "b" if no one is waiting.Pickup a passenger going to Charboil Grill.Go to Charboil Grill:s 2 r 1 l 1 l 2 r.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:e 2 r.Pickup a passenger going to Multiplication Station.Go to The Underground:w 1 r 2 r 1 r 2 l.[b]Go to Multiplication Station:s 1 l 1 r.Go to Tom's Trims:s 1 r 3 r.Switch to plan "c" if no one is waiting.Switch to plan "a".[c]Go to Multiplication Station:s 1 l 3 l.Pickup a passenger going to Cyclone.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 2 l 2 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Cyclone.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Multiplication Station.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 1 l 1 l 3 l.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:w 1 r 2 r 1 r 4 l.Pickup a passenger going to Rob's Rest.Go to Rob's Rest:s 1 r 2 l 1 l 1 r 1 r.Go to Cyclone:s 1 l 1 l 1 l 1 r.Pickup a passenger going to Cyclone.[d]Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:s 1 l 2 r 4 l.Pickup a passenger going to Addition Alley.Go to Cyclone:s 1 r 2 l 2 r.Switch to plan "e" if no one is waiting.Switch to plan "d".[e]Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Cyclone.99 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 1 l 1 l 3 l.Pickup a passenger going to The Underground.Go to Cyclone:e 1 l 2 r.Pickup a passenger going to Trunkers.Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:n 1 r.Go to Trunkers:s 1 l.[f]Pickup a passenger going to Cyclone.Go to Cyclone:w 2 r.Pickup a passenger going to Divide and Conquer.Pickup a passenger going to Trunkers.Go to Trunkers:s 1 l.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l.Pickup a passenger going to Divide and Conquer.Go to Zoom Zoom:n.Go to Divide and Conquer:w 1 l 2 r 1 r.Pickup a passenger going to Addition Alley.Go to Cyclone:e 1 l 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Divide and Conquer.2 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 1 l 3 l 2 r.Pickup a passenger going to Divide and Conquer.Go to Divide and Conquer:w 1 r 2 r 1 r 2 l 3 l.Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:e 1 l 1 l 2 l 1 l.Go to The Underground:s 1 l 1 r 2 l.Switch to plan "g" if no one is waiting.Pickup a passenger going to The Underground.Go to Trunkers:s 2 r 1 l.Switch to plan "f".[g]Go to Rob's Rest:n 3 l 2 l 1 l 2 r 1 r.Pickup a passenger going to What's The Difference.Go to Sunny Skies Park:s 1 l 1 l.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:e 3 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.

在线尝试!

经验丰富的Taxi高尔夫球手工程师Toast决定花一些时间(可能比我少很多),并通过基本上重写它来简化我的Taxi程序。您可以在编辑历史记录中找到我的旧答案正文并链接到我的旧TIO。

工程师吐司的无平方根算法:在线试用!

取消高尔夫,并附有说明:

[ Crow vs. Taxi ]


[ GET THE NEGATIVE ABSOLUTE VALUES OF BOTH STDINS ]

[Move the stdin values to Tom's Trims b/c:]
[1) Stdin doesn't count as a passenger waiting]
[2) Checking for no one waiting is shorter that keeping tracker of a count for just 2 iterations OR repeating all the code over again]
Go to Post Office:w 1 l 1 r 1 l.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Tom's Trims.
Go to Tom's Trims:n.

[a]
[Clone the first waiting value]
[If we've already picked up both, move to the next process]
[Switch to plan "c" if no one is waiting.]
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:s 1 l 1 r.
Pickup a passenger going to Cyclone.
Go to Cyclone:n 1 l 1 l 2 r.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Multiplication Station.

[Add one to the value]
1 is waiting at Starchild Numerology.
Go to Starchild Numerology:s 2 l 2 r.
Pickup a passenger going to Cyclone.
Go to Cyclone:w 1 r 4 l.
Pickup a passenger going to Addition Alley.
Go to Addition Alley:n 2 r 1 r.
Pickup a passenger going to The Underground.

[Pick up a clone of that one to feed into Multiplication Station]
Go to Cyclone:n 1 l 1 l.
Pickup a passenger going to Multiplication Station.

[Subtract one and see if that's more than zero]
Go to The Underground:n 2 r 2 r.
Switch to plan "b" if no one is waiting.

[It was more than zero so we make it negative]
[First, though, get rid of the pesky result from The Underground]
Pickup a passenger going to Charboil Grill.
Go to Charboil Grill:s 2 r 1 l 1 l 2 r.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology:e 2 r.
Pickup a passenger going to Multiplication Station.
Go to The Underground:w 1 r 2 r 1 r 2 l.

[b]
[Make sure it's a negative value and leave it at Multiplication Station]
[Either it was already negative OR we now have -1 as a passenger]
Go to Multiplication Station:s 1 l 1 r.

[Get the next stdin unless we've already gotten them both]
Go to Tom's Trims:s 1 r 3 r.
Switch to plan "c" if no one is waiting.
Switch to plan "a".


[ ADD THE TWO NEGATIVES AND INVERT TO GET THE TAXI DISTANCE]

[c]
[Pickup the two negative absolute values and clone them]
Go to Multiplication Station:s 1 l 3 l.
Pickup a passenger going to Cyclone.
Pickup a passenger going to Cyclone.
Go to Cyclone:s 1 r 2 l 2 r.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Cyclone.
Pickup a passenger going to Addition Alley.

[Add the two clones values to get the negative Taxi distance]
Go to Addition Alley:n 2 r 1 r.
Pickup a passenger going to Multiplication Station.

[Invert to get the Taxi distance and store it at Rob's Rest]
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology:n 1 l 1 l 1 l 3 l.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station:w 1 r 2 r 1 r 4 l.
Pickup a passenger going to Rob's Rest.
Go to Rob's Rest:s 1 r 2 l 1 l 1 r 1 r.


[ FIND THE CROW DISTANCE ]
[Uses the Babylonian method: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method ]

[Square and Sum the cloned values]
Go to Cyclone:s 1 l 1 l 1 l 1 r.
Pickup a passenger going to Cyclone.
[d]
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station:s 1 l 2 r 4 l.
Pickup a passenger going to Addition Alley.
Go to Cyclone:s 1 r 2 l 2 r.
Switch to plan "e" if no one is waiting.
Switch to plan "d".
[e]
Go to Addition Alley:n 2 r 1 r.
Pickup a passenger going to Cyclone.

[Pickup our cycle counter]
[It's cheaper to do this than to check each iteration's value as equal to the last]
[Taxi can only handle integers up to 2^31-1 so 99 iterations is sufficient.]
99 is waiting at Starchild Numerology.
Go to Starchild Numerology:n 1 l 1 l 1 l 3 l.
Pickup a passenger going to The Underground.

[Duplicate stdin to be stored as S at Trunkers and as x0 at Sunny Skies Park]
[a & b are always integers so Trunkers won't hurt S and it's close with a short name so it's good for golfing]
Go to Cyclone:e 1 l 2 r.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park:n 1 r.
Go to Trunkers:s 1 l.

[ This is our starting position for square root:           ]
[  • x at Sunny Skies Park                                 ]
[  • S at Trunkers                                         ]
[  • Taxi at Trunkers                                      ]
[  • The iterator as a passenger going to The Underground  ]

[f]

[Duplicate S so we don't lose it]
Pickup a passenger going to Cyclone.
Go to Cyclone:w 2 r.
Pickup a passenger going to Divide and Conquer.
Pickup a passenger going to Trunkers.
Go to Trunkers:s 1 l.

[Diplicate x for division and addition]
Go to Sunny Skies Park:w 1 r.
Pickup a passenger going to Cyclone.
Go to Cyclone:n 1 l.
Pickup a passenger going to Divide and Conquer.

[Gas will be super important in this loop]
Go to Zoom Zoom:n.

[Perform (x + S/x)/2]
[(/2) turns out to be a few bytes shorter than (*.5), mostly due to place names]
Go to Divide and Conquer:w 1 l 2 r 1 r.
Pickup a passenger going to Addition Alley.
Go to Cyclone:e 1 l 1 l 2 l.
Pickup a passenger going to Addition Alley.
Go to Addition Alley:n 2 r 1 r.
Pickup a passenger going to Divide and Conquer.
2 is waiting at Starchild Numerology.
Go to Starchild Numerology:n 1 l 1 l 3 l 2 r.
Pickup a passenger going to Divide and Conquer.
Go to Divide and Conquer:w 1 r 2 r 1 r 2 l 3 l.
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park:e 1 l 1 l 2 l 1 l.
[Now we have the next iteration of x]

[Check the iterator]
Go to The Underground:s 1 l 1 r 2 l.
Switch to plan "g" if no one is waiting.
Pickup a passenger going to The Underground.

[Reset the loop]
Go to Trunkers:s 2 r 1 l.
Switch to plan "f".


[ ADD THE NEGATIVE SUM TO THE SQUARE ROOT TO GET THE NEGATIVE DIFFERENCE ]

[g]
Go to Rob's Rest:n 3 l 2 l 1 l 2 r 1 r.
Pickup a passenger going to What's The Difference.
Go to Sunny Skies Park:s 1 l 1 l.
Pickup a passenger going to What's The Difference.
Go to What's The Difference:n 1 r 1 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:e 3 r.
Pickup a passenger going to Post Office.
Go to Post Office:n 1 l 1 r.

1
这太疯狂了……
狮ry –恢复莫妮卡的时间

2
@Gryphon疯狂的事情是我100%确信一旦我不睡着就重新访问自己的逻辑,就可以删除两千个字节,就像我一样
Stephen

4
4小时本身值得+1!
毛茸茸的

4
我敢肯定,会读完整说明的高尔夫球手的数量将少于您的字节:D
Grajdeanu Alex。

1
+1 for-1 is waiting at Starchild Numerology.
Keyu Gan

10

Javascript(ES6),36个字节

-1字节感谢@dtkaias

(x,y,s=Math.hypot)=>s(x)+s(y)-s(x,y)

示例代码段:

f=

(x,y,s=Math.hypot)=>s(x)+s(y)-s(x,y)

for(i=0;i<7;i++)
  a=[3,-3,-3,6,42,10,3][i],
  b=[4,4,-4,6,0,10,3][i],
  console.log(`f(${a},${b}) = ${f(a,b)}`)


ES6没有**
尼尔

2
真正的ES6版本可能是41个字节:(x,y,s=Math.sqrt)=>s(x*=x)+s(y*=y)-s(x+y)
尼尔

@DanEsparza显然已经习惯了。这就是修订历史记录的内容……
Neil

为什么不a=>b=>a+b-Math.hypot(a,b)呢?
dtkaias

1
是的(x,y,s=Math.hypot)=>s(x)+s(y)-s(x,y),对于36字节,ES6也兼容

8

朱莉娅,20个字节

x->norm(x,1)-norm(x)

ab作为列表。

Julia的norm第二个参数默认为2-因此等同于norm(x, 1) - norm(x, 2)


我还考虑过使用Julia!
埃德尼尔2016年

与我要发布的MATLAB答案非常相似。
TheIncredibleZ17年

6

Java 8,47字节

打高尔夫球:

(a,b)->(a<0?-a:a)+(b<0?-b:b)-Math.sqrt(a*a+b*b)

这几乎是最基本的:将两个计算出的值相减以求出差值。这使用三元逻辑,而不是Math.abs()每次出现都节省一个字节。不幸的是,由于运算符的优先级,需要使用括号。

输出是Java double可以容纳的任何内容,其精确度可以超过两个小数位,并且可以满足问题的准确性要求。

取消高尔夫:

public class TheCrowVsTheTaxicab {

  public static void main(String[] args) {
    int[][] inputs = new int[][] { { 3, 4 }, { -3, 4 }, { -3, -4 }, { 6, 6, }, { 42, 0 }, { 10, 10 }, { 3, 3 } };
    double[] outputs = new double[] { 2, 2, 2, 3.51, 0, 5.85, 1.76 };

    for (int i = 0; i < inputs.length; ++i) {
      double actual =
        f((a, b) -> (a < 0 ? -a : a) + (b < 0 ? -b : b) - Math.sqrt(a * a + b * b), inputs[i][0], inputs[i][1]);

      System.out.println("Input:    " + inputs[i][0] + ", " + inputs[i][1]);
      System.out.println("Expected: " + outputs[i]);
      System.out.println("Actual:   " + actual);
      System.out.println();
    }
  }

  private static double f(java.util.function.BiFunction<Integer, Integer, Double> f, int a, int b) {
    return f.apply(a, b);
  }
}

输出:

Input:    3, 4
Expected: 2.0
Actual:   2.0

Input:    -3, 4
Expected: 2.0
Actual:   2.0

Input:    -3, -4
Expected: 2.0
Actual:   2.0

Input:    6, 6
Expected: 3.51
Actual:   3.5147186257614305

Input:    42, 0
Expected: 0.0
Actual:   0.0

Input:    10, 10
Expected: 5.85
Actual:   5.857864376269049

Input:    3, 3
Expected: 1.76
Actual:   1.7573593128807152

1
您可以使用currying保存一个字节:a->b->
雅各布

4

Mathematica,32个字节

N[Tr@Abs[a={##1}]-Sqrt@Tr[a^2]]&

要么

Mathematica,31个字节

N[Abs@#+Abs@#2-Sqrt[#^2+#2^2]]&

或@不是树的建议

Mathematica,26个字节

N[Tr@Abs@{##}-Abs[#+I#2]]&

或@alephalpha的建议

Mathematica,19个字节

N[#~Norm~1-Norm@#]&

干得好!您可以通过对欧几里得部分使用复数来节省一些字节:N[Tr@Abs@{##}-Abs[#+I#2]]&
不是一棵树

2
N[#~Norm~1-Norm@#]&
alephalpha

2
@alephalpha #~Norm~1-N@Norm@#&也许吗?
Martin Ender

4

Dyalog APL,13个字节

+/∘|-.5*⍨+.×⍨

在线尝试!

说明(输入X):

+/∘|     - Sum of the element-wise absolute value of X
-        - Minus
.5*⍨+.×⍨ - Euclidean distance = sqrt(X . X)

4

R,30个字节

function(v)norm(v)-norm(v,'f')

v为1列矩阵。norm计算矩阵的特定范数,默认值为L1范数(出租车)和fL2范数('f'对于Frobenius / Euclidean)。

在线尝试!


哇,R都内置了,太好了!
BLT


3

Japt11个 9字节

@ETHproductions --2个字节

Nxa -MhUV

在线尝试!

讲解

Nxa -MhUV   // implicit: U and V are input integers, N = [U,V]

N a         // get absolute value of both inputs
 x          // sum those values
    -MhUV   // subtract hypot(U, V) -> sqrt(U^2 + V^2)

很好,我认为这是我第一次看到Mh它。我相信您可以缩短Ua +VaNxa(输入总和,a在每个输入上运行)
ETHproductions'Jul

@ETHproductions哦,对了,忘记了总和具有可选的map参数。谢谢!
贾斯汀·马里纳

因此,*有“ Mh!”的用法::D Japt中的大多数方法都可以将@ETHproductions称为“自动功能”的参数作为参数。有关更多信息,请参见此技巧。我明天将在此撰写技巧。使用带有数组归约的自动函数来实现一些有趣的结果(例如,rw将返回数组中的最大整数)
Shaggy

@Shaggy谢谢,我的基本想法是“自动功能”,但没有意识到Japt技巧的帖子。一定会浏览该帖子。
贾斯汀·马里纳

关于这个问题,我们还有很多工作要做,因此,如果您有可以添加的内容,请这样做。或者,如果您有一个适合您的查询,请在评论中让我们知道,或在聊天室加入我们。
毛茸茸的

3

方案-58个字节。

(define (f a b) (-(+(abs a)(abs b))(sqrt(* a a)(* b b))))

3
欢迎来到PPCG!
Martin Ender

您可以删除两个空格-一个之间define(,和之间的一个)(
clismique

Error: sqrt: too many arguments (at most: 1 got: 2) [f, sqrt]
亚伦

(lambda(a b)(...))应该足够,而不是将其绑定到名称。另外,(sqrt(* a a b b))将节省几个字节。
Daniel Schepler


3

APL(Dyalog),14个字节

以形式接受参数xJy,例如3J4

||-2+/∘|9 11○⊢

在线尝试!

|文件  的大小

| 论点的大小

- 减去

2+/ 成对和

 的

| 的大小

9 11.○⊢ 论证的实部和虚部。文件

打高尔夫球的特殊技巧是使用成对减少(2+/)来提供+/无操作左引数,从而避免出现括号:||-(+/∘|9 11○⊢)


3

J,13个字节

+/@:|-+/&.:*:

这是一个将坐标作为数组的函数,例如:

   (+/@:|-+/&.:*:) _3 4
2

说明:

+/             sum
  @:           of
    |          absolutes
     -         minus
      +/       sum
        &.:    under
           *:  square

很好用&.:-我不知道:Under。
约拿(Jonah)


2

TI基本(TI-84 Plus CE),10字节

sum(abs(Ans))-√(sum(Ans2

编程将输入输入为in中的两个整数的列表Ans,例如用调用{3,4}:prgmCROW(替换3,4输入和CROW程序名)。

说明:

sum(abs(Ans))-√(sum(Ans2
        Ans               # The input list of two integers
    abs(   )              # Absolute value of each item in the list
sum(        )             # Sum of the list
                    Ans   # The input list of two integers
                       2  # Square of each item in the list
                sum(      # Sum of the list
              √(          # Square root of the sum
             -            # Difference of the two values

在我看来,这似乎是24-26个字节,具体取决于$ Ans ^ 2 $的编码方式。也许有10次击键,但这不是一回事。

@Ray TI-Basic是一种标记化语言
pizzapant184 '17

我对TI-89 Basic非常熟悉,但实际情况并非如此。异议撤回。

2

MATL8 7字节

|sG2&|-

在线尝试!

说明

|    % Implicit input: vector of two numbers. Absolute value, element-wise
s    % Sum of vector
G    % Push input again
2    % Push 2
&|   % 2-norm of input
-    % Subtract. Implicit display


2

GNU APL 1.2,24个字节

∇f P
(+/|P)-(+/P*2)*.5
∇

∇f P声明一个函数f,该函数采用P包含距离的向量作为参数(例如[3, 4]

APL对向量+/|P进行|运算,因此将运算符(abs函数)应用于向量中的每个元素,然后求值+对每个元素(因此将所有元素相加)。这给出了出租车的距离。

P*2产生的向量与相同,P但每个元素均平方。+/P*2将它们加在一起,然后(用括号括起来,因为APL是从右到左的)*.5得到平方根。这给出了乌鸦的距离。

为滑行距离加一个额外的括号对作为优先级并计算差值。

结束功能。


1
您不能使用匿名lambda吗?{(+/|⍵)-(+/⍵*2)*.5}
阿达姆(Adám)

@Adám我对APL没有太多经验,最新版本的GNU解释器(1.7)无法在Mac上编译,因此可能会有一些限制。我稍后再试看是否可行。谢谢你的提示。
Arc676 '17

GNU APL的几乎所有功能也都在Dyalog APL中,而Dyalog APL 在Mac上免费提供。您也可以在线尝试
亚当

另外,请随时加入APL聊天室以了解更多APL。
亚当

2

J9 8字节

-1感谢我的同事马歇尔。

+&|-|@j.

在线尝试!

注意到一个如左边的参数和作为右边的参数。

+ 总和

& 的

| 幅度

- 减去

| 幅度

@ 的

j.A + B i

高尔夫技巧:将值组合为单个复数,因为对角线很容易获得,而又将它们分开,因为对角线很容易获得。


2

加++59 57字节

D,f,@@,|@|+
D,g,@@,d*@d*+
_
$f>G>G
V
$g>?>?
S
-G
$f>x>0
O

在线尝试!

这花了我很长时间才能解决。它无法解决最终答案,因为在Add ++中是不可能的。程序就是这样处理输入的,-3并且-4ACC是累加器值)

D,        Define a function
  f,      called f
  @@,     that takes 2 arguments (e.g. -3, -4)
     |    absolute value;   STACK = [-3, 4]
     @    reverse stack;    STACK = [4, -3]
     |    absolute value;   STACK = [4, 3]
     +    sum;              STACK = [7]
          implicitly return the top of the stack

D,        Define a function
  g,      called g
  @@,     that takes 2 arguments (e.g. -3, -4)
     d    duplicate;  STACK = [-3, -4, -4]
     *    multiply;   STACK = [-3, 16]
     @    reverse;    STACK = [16, -3]
     d    duplicate;  STACK = [16, -3, -3]
     *    multiply;   STACK = [16, 9]
     +    sum;        STACK = [25]
          implicitly return the top of the stack

_         store the inputs in the second stack;  ACC = 0;  STACK = [-3, -4]
$f>G>G    apply f with -3 and -4 as arguments;   ACC = 7;  STACK = []
V         store ACC in the stack;                ACC = 7;  STACK = [7]
$g>?>?    apply g with -3 and -4 as arguments;   ACC = 25; STACK = [7]
S         square root the ACC;                   ACC = 5;  STACK = [7]
-G        subtract the stack value from the ACC; ACC = -2; STACK = []
$f>x>0    apply f with ACC and 0 as arguments;   ACC = 2;  STACK = []
O         output ACC as a number


1

Excel VBA,34字节

匿名VBE立即窗口函数,该函数从范围获取输入,[A1:B1]并将欧几里得距离和出租车距离之间的差输出到VBE立即窗口。

?[ABS(A1)+ABS(B1)-SQRT(A1^2+B1^2)]

1
将其直接放在工作表的单元格中,要短两个字节:=ABS(A1)+ABS(B1)-SQRT(A1^2+B1^2)
Engineer Toast

@EngineerToast,我以为我已经安装了Google Sheets / Excel解决方案-感谢您指出这一点
Taylor Scott





1

Ruby(2.0.0-2.3.0),57字节

x,y=$*.map(&:to_i);puts x.abs+y.abs-Math.sqrt(x**2+y**2)

假设从ARGV接收输入,例如

ruby -e 'x,y=$*.map(&:to_i);puts x.abs+y.abs-Math.sqrt(x**2+y**2)' -- -3 4

由于Ruby附带了一个具有Abs和sqrt函数的数学库(这与编写自己的Abs和sqrt函数的人不同,尽管我没有看到专门禁止使用此类函数的家伙),这感觉像是在作弊。

第一个技巧是使用.map而不是.each保存一个字节,然后使用&:symbol符号将映射传递给我们执行的procto_i在数组中每个项目上,并使用多个赋值将值分配给x和y。

更长的版本是:

(x, y) = ARGV.map{ |string| string.to_i }

(由于map返回一个数组,因此可能会使用多次赋值,这确实会丢弃任何其他参数,但无论如何我们都假设只有两个输入)

然后,我只删除了方程式中的所有空格。

这是一个较长的版本,84字节

$*.map!(&:to_i);puts$*.inject(0){|x,y|x+y.abs}-Math.sqrt($*.inject(0){|x,y|x+y**2})

这里的目标是不要重复自己,例如必须写xabs两次,而我平方两次x**2 + y**2

这没用。

但是有趣的是,puts不需要空格,我猜词法分析器足够聪明,可以看到特殊的char并知道它是特殊的var。

inject并且reduce是同义词,inject的签名为

inject(initial) {| memo, obj | block }

在我们的例子中,我们需要将初始值设置为0,然后得到我们的累加器(即memo = 0)和每次迭代的对象。

该方法的缺点是它将接受两个以上的输入,并且将对数组中的所有值求和或求平方,相加然后乘以sqrt。

认为-尽管我没有要测试的Ruby 2.4.0-这也可以正常工作,它的大小为72个字节:

$*.map!(&:to_i);puts$*.sub{|x,y|x+y.abs}-Math.sqrt($*.sum{|x,y|x+y**2})

总和默认为0,据我所知,其工作方式与注入/减少相同。


他不使用内建函数的唯一原因是因为该语言没有可供他使用的语言。好的答案,欢迎来到PPCG!:)
科纳·奥布莱恩

欢迎来到PPCG!TIO Portal具有可正常工作的Ruby 2.4.0,您可以使用它来在线尝试一些事情。在这里尝试
价值墨水

利用灵活的I / O需求,您可以避免解析和放置,将高尔夫球运动降至35字节的lambda
benj2240 '18

1

Google表格,31字节

工作表功能,可接收范围输入[A1:B1]并输出欧几里得距离与出租车距离之间的差

=ABS(A1)+ABS(B1)-SQRT(A1^2+B1^2

Excel,32个字节

与上述相同,但格式为MS Excel

=ABS(A1)+ABS(B1)-SQRT(A1^2+B1^2)


0

,15字节

ABa+ABb-RT$+g*g

从命令行参数获取输入。在线尝试!

说明

在伪代码中,这是abs(a) + abs(b) - sqrt(fold+(g*g))ab是前两个cmdline参数,并且g是cmdline参数(即argv)的列表。*像许多Pip运算符一样,运算符矢量化$+g*g也与相同a*a + b*b。其余的非常简单。

不幸的是,我无法使用来保存任何字节$+ABg,因为带有fold的运算符的优先级无法正常运行。$+优先级应该比二进制优先级稍高-,但目前它解析为$+(ABg-RT$+g*g),给出了错误的答案。($+ABg)-RT$+g*g与上面的混淆程度较低的版本相比,这样做不会节省任何字节。

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.