你会得到一张二维地图,里面有球和地面。看起来是这样的:
1 5 2
3
4
__________________________每个数字都是一个球,而_是地面水平。除底层行外,任何其他行都不允许使用下划线_字符。只有空格,换行符和数字0-9允许在地面以上。你不能假设最后一行是地面--允许地面以下的空行。您也可以添加空格,以填补空行,如果这对您有帮助的话。
球可以有从0到9的数字,可以放在另一个上面,但不能放在地下。球的号码是独一无二的。
假设每个字符是一米。
从巴斯特垃圾箱拿地图!
测试用例1 -应该输出类似于这的内容
测试用例2 -应该产生与第一张地图相同的结果
您的挑战是从文件或stdin中读取这样的地图--您可以使用cat balls.txt | ./yourexecutable --以及当每个球落地时的输出速度。
这是速度的公式:

假设h是地面线数和球线数之间的行号差,g等于10m/s^2。
你应该在地面上输出m/s中的每个球数和速度。例如,N - Vm/s,其中N是球数,V是它的速度。如果需要,还可以输出数组。
快乐编码!:)
发布于 2016-06-25 16:02:45
版本6,84个字节:(感谢漏洞Nun!)
lambda a:[(c,(~-(len(a)-i)*20)**.5)for i,s in enumerate(a)for c in s if c.isdigit()]版本5,91个字节:
lambda a:[c+":"+str((~-(len(a)-i)*20)**.5)for i,s in enumerate(a)for c in s if c.isdigit()]版本4,92个字节:
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个字节:
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个字节:
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个字节:
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这将文件的目录作为用户的输入。
发布于 2016-06-25 19:48:49
lambda x:[[i,(20*x[x.find(i):x.find('_')].count('\n'))**.5]for i in x if i.isdigit()]一个匿名函数,它以多行字符串的形式接受参数输入,其中的所有空行都填充了空格,并返回一个数组,其中每个元素都是表单球数、速度。
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]发布于 2016-06-25 23:28:06
编辑2字节保存的thx @Jacajack
以多行字符串作为输入参数的函数。未对输出进行排序(因为未请求输出)
a=>[...a].reverse().map(c=>c>'Z'?b=i:c<' '?++i:c>' '&&console.log(c,Math.sqrt((i-b)*20)),i=0)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()#I { height: 12em; width: 30em}<textarea id=I>
1 5 2
3
4
__________________________
</textarea>
<button onclick="test()"></button>https://codegolf.stackexchange.com/questions/83792
复制相似问题