我正在尝试简化我对Project Euler问题11的解决方案(在20x20网格中找到最大的4行数字产品).
我对答案的主要抱怨是sub_lists_at_xy定义中的四个try/except子句.我的每个方向(东,南,东南和西南)都有一个可以在板上运行的四排列表.您对简化或干预此实施有什么建议吗?
from operator import mul
with open("11.txt") as f:
nums = [[int(num) for num in line.split(' ')] for line in f.read().split('\n')]
def prod(lst):
return reduce(mul, lst, 1)
def sub_lists_at_xy(array, length, x, y):
try:
east=array[y][x:x+length]
except IndexError:
east=[0]*length
try:
south=[list[x] for list in array[y:y+length]]
except IndexError:
south=[0]*length
try:
southeast=[array[y+i][x+i] for i in range(length)]
except IndexError:
southeast=[0]*length
try:
southwest=[array[y+i][x-i] for i in range(length)]
except IndexError:
southwest=[0]*length
return east, south, southeast, southwest
sub_lists=[]
for x in range(len(nums[0])):
for y …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作更短,更pythonic,可读的python.我为Project Euler的问题8提供了这个有效的解决方案(找到1000位数字中5个连续数字的最大乘积).
写这个脚本更pythonic版本的建议?
numstring = ''
for line in open('8.txt'):
numstring += line.rstrip()
nums = [int(x) for x in numstring]
best=0
for i in range(len(nums)-4):
subset = nums[i:i+5]
product=1
for x in subset:
product *= x
if product>best:
best=product
bestsubset=subset
print best
print bestsubset
Run Code Online (Sandbox Code Playgroud)
例如:下面的代码片段必须有一个单行代码.我确定这里有一个过去的主题,但我不确定如何描述我在下面做的事情.
numstring = ''
for line in open('8.txt'):
numstring += line.rstrip()
Run Code Online (Sandbox Code Playgroud)
有什么建议?多谢你们!
尝试使用Ruby 1.9.3并rest-client发出https请求,例如:
RestClient.get('https://google.com')
Run Code Online (Sandbox Code Playgroud)
总是给我一个SSL错误,
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server key exchange B: bad ecpoint
Run Code Online (Sandbox Code Playgroud)
这是我无法弄清楚的.bad ecpoint?
我毫不费力地使用1.9.3&rest-client在另一台Mountain Lion机器上发出相同的请求.
关于这台机器的注意事项:MacBook pro带有新的Mountain Lion安装,我最初在通过XCode命令行工具安装gcc-4.2时遇到了一些麻烦,但最终通过自制软件/ dupes获得了gcc-4.2 .从那时起,我已经卸载并重新安装了RVM和1.9.3.