首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下落ASCII球

下落ASCII球
EN

Code Golf用户
提问于 2016-06-25 10:45:27
回答 3查看 1.8K关注 0票数 16

输入

你会得到一张二维地图,里面有球和地面。看起来是这样的:

代码语言:javascript
复制
  1         5          2
                 3
     4


__________________________

每个数字都是一个球,而_是地面水平。除底层行外,任何其他行都不允许使用下划线_字符。只有空格,换行符和数字0-9允许在地面以上。你不能假设最后一行是地面--允许地面以下的空行。您也可以添加空格,以填补空行,如果这对您有帮助的话。

球可以有从09的数字,可以放在另一个上面,但不能放在地下。球的号码是独一无二的。

假设每个字符是一米。

从巴斯特垃圾箱拿地图!

测试用例1 -应该输出类似于的内容

测试用例2 -应该产生与第一张地图相同的结果

挑战

您的挑战是从文件或stdin中读取这样的地图--您可以使用cat balls.txt | ./yourexecutable --以及当每个球落地时的输出速度。

这是速度的公式:

假设h是地面线数和球线数之间的行号差,g等于10m/s^2

输出

你应该在地面上输出m/s中的每个球数和速度。例如,N - Vm/s,其中N是球数,V是它的速度。如果需要,还可以输出数组。

快乐编码!:)

EN

回答 3

Code Golf用户

发布于 2016-06-25 16:02:45

Python3,84个字节

版本6,84个字节:(感谢漏洞Nun!)

代码语言:javascript
复制
lambda a:[(c,(~-(len(a)-i)*20)**.5)for i,s in enumerate(a)for c in s if c.isdigit()]

版本5,91个字节:

代码语言:javascript
复制
lambda a:[c+":"+str((~-(len(a)-i)*20)**.5)for i,s in enumerate(a)for c in s if c.isdigit()]

版本4,92个字节:

代码语言:javascript
复制
lambda i:[c+":"+str((~-(len(i)-n)*20)**.5)for n in range(len(i))for c in i[n]if c.isdigit()]

版本3,99个字节:

代码语言:javascript
复制
def r(i):x=len(i);print([c+":"+str((~-(x-n)*20)**.5)for n in range(x)for c in i[n] if c.isdigit()])

版本2,102个字节:

代码语言:javascript
复制
def r(i):
 n=len(i)
 for l in i:
  for c in l:
   if c.isdigit():print(c+":"+str((~-n*20)**.5))
  n-=1

以上版本以字符串数组作为输入。

版本1,140个字节:

代码语言:javascript
复制
with open(input(),"r")as i:
 n=sum(1for l in i);i.seek(0)
 for l in i:
  for c in l:
   if c.isdigit():print(c+":"+str((~-n*20)**.5))
  n-=1

这将文件的目录作为用户的输入。

票数 3
EN

Code Golf用户

发布于 2016-06-25 19:48:49

Python3,84个字节

代码语言:javascript
复制
lambda x:[[i,(20*x[x.find(i):x.find('_')].count('\n'))**.5]for i in x if i.isdigit()]

一个匿名函数,它以多行字符串的形式接受参数输入,其中的所有空行都填充了空格,并返回一个数组,其中每个元素都是表单球数、速度。

是如何工作的

代码语言:javascript
复制
lambda x                      Function with input x
...for i in x if i.isdigit()  Loop through all characters i in x for which i is a digit,
                              and hence one of the balls
x[x.find(i):x.find('_')]      Slice x to give the substring between the ball and the ground
....count('\n')               Count the number of newlines in the substring to give the
                              height of the ball
(20*...)**.5                  Calculate the speed of the ball as it hits the ground
[i,...]                       Package the ball number and speed into a list
:[...]                        Return all ball-speed pairs as a list with elements [ball
                              number, speed]

在Ideone身上试试

票数 2
EN

Code Golf用户

发布于 2016-06-25 23:28:06

JavaScript (ES6) 93

编辑2字节保存的thx @Jacajack

以多行字符串作为输入参数的函数。未对输出进行排序(因为未请求输出)

代码语言:javascript
复制
a=>[...a].reverse().map(c=>c>'Z'?b=i:c<' '?++i:c>' '&&console.log(c,Math.sqrt((i-b)*20)),i=0)

测试

代码语言:javascript
复制
F=
a=>[...a].reverse().map(c=>c>'Z'?b=i:c<' '?++i:c>' '&&console.log(c,Math.sqrt((i-b)*20)),i=0)

function test()
{
  F(I.value);
}

test()
代码语言:javascript
复制
#I { height: 12em; width: 30em}
代码语言:javascript
复制
<textarea id=I>
    
 
  1         5          2
                 3
     4


__________________________




</textarea>
<button onclick="test()"></button>
票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/83792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档