我在解决Code Jam问题时给了Julia一个机会,在这种情况下,来自2010年第1C轮的Rope Intranet(https://code.google.com/codejam/contest/619102/dashboard)
解决方案基本上是
for tc = 1:int(readline())
n = int(readline())
a = [map(int, split(readline())) for _ = 1:n]
ans = 0
for (i, x) in enumerate(a)
for y in a[i + 1:end]
ans += (x[1] - y[1]) * (x[2] - y[2]) < 0
end
end
println("Case #", tc, ": ", ans)
end
Run Code Online (Sandbox Code Playgroud)
但是,与c ++和python中的解决方案相比,大输入的时间结果并不是很令人印象深刻:
julia
real 0m6.196s
user 0m6.028s
sys 0m0.373s
c++
real 0m0.392s
user 0m0.338s
sys 0m0.053s
pypy
real 0m0.529s
user 0m0.507s
sys 0m0.016s
Run Code Online (Sandbox Code Playgroud)
情况发生变化,当我用随机数替换文件输入时(仍然比c ++慢): …
函数签名清楚地表明它应该返回一个Bool,为什么函数返回96?更重要的是,编译器认为96实际上是Bool.这是一个错误吗?
> isLeapYear year =\
| (modBy 4 year == 0) && (modBy 100 year /= 0) || (modBy 400 year == 0)
<function> : Int -> Bool
> isLeapYear 1996
96 : Bool
Run Code Online (Sandbox Code Playgroud)
它似乎有时工作:
> isLeapYear 2000
True : Bool
> isLeapYear 1800
False : Bool
Run Code Online (Sandbox Code Playgroud) 我有一个数字作为字符串,我想找到由原始数字组成的最小数字,即
56340902138765401345 -> 10001233344455566789
Run Code Online (Sandbox Code Playgroud)
我正在将字符串转换为列表并对其进行排序。
num = '56340902138765401345'
a = list(num)
a.sort()
Run Code Online (Sandbox Code Playgroud)
由于数字不能以零开头(但我需要使用原始数字中的零),我正在寻找第一个非零元素并将其放在前面:
inext = next(i for i, x in enumerate(a) if x != '0')
a.insert(0, a.pop(inext))
Run Code Online (Sandbox Code Playgroud)
然后我将列表转换回字符串并显示它。
num2 = ''.join(map(str, a))
print(num2)
Run Code Online (Sandbox Code Playgroud)
它有效,但对我来说似乎不太蟒蛇或优雅。有没有更好的办法?
algorithm ×1
boolean ×1
compiler-bug ×1
elm ×1
input ×1
julia ×1
optimization ×1
performance ×1
permutation ×1
python ×1
string ×1