当我在Project Euler中努力做问题14时,我发现我可以使用一个叫做memoization的东西来加速我的进程(我让它运行了15分钟,它仍然没有回复).问题是,我该如何实现它?我试过,但是我得到了一个keyerror(返回的值无效).这让我很烦,因为我很肯定我可以对此应用memoization并加快速度.
lookup = {}
def countTerms(n):
arg = n
count = 1
while n is not 1:
count += 1
if not n%2:
n /= 2
else:
n = (n*3 + 1)
if n not in lookup:
lookup[n] = count
return lookup[n], arg
print max(countTerms(i) for i in range(500001, 1000000, 2))
Run Code Online (Sandbox Code Playgroud)
谢谢.
在Facebook的" 发送对话框 " 的文档中,它指出:
此对话框可与JavaScript SDK一起使用,也可以执行完全重定向到URL.移动设备不支持此功能.
发送对话框(当它工作时)正是我想要使用的(因为它默认发送给特定的人):

作为后备,您可以使用" 共享按钮 ",但共享按钮的用户流略有不同(您必须选择将其发送给特定人员):

现在我正在使用Zurb Foundation的Visibility Classes来触发显示哪个按钮,如下所示:
<button id='actionShare' class='button large-12 hide-for-touch'>share on facebook</button>
<a href='https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html' class='button small-12 show-for-touch' target='_blank'>share on facebook</a>
<script>
$('#actionShare').on('click', function() {
FB.ui({
method: 'send',
link: window.location.href
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
有谁知道Facebook使用的标准(所以我可以触发正确的后备)?
我有一个简单的 Android 应用程序,代码如下(来自Android 文档):
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Run Code Online (Sandbox Code Playgroud)
但是我找不到检测电子邮件是否已成功发送(或用户取消)的方法。有没有办法读取意图响应?
我正在学习如何通过学习python的方式进行编码,而且我最近第一次陷入困境.对于这个练习,我们应该编写自己的游戏.我这样做了,但出于某种原因,每当我运行它时,right_room()函数在我输入答案后退出,而不是进入下一个房间.任何帮助将不胜感激.这是我的代码:
from sys import exit
def bear_room():
print "You are in a room with a bear."
print "You have two choices. left or right?"
next = raw_input("> ")
if next == "left":
left_room()
elif next == "right":
right_room()
else:
print "No idea what that means..."
def left_room():
print "You went left."
print "There are two doors. right or straight"
next = raw_input("> ")
if next == "right":
bear_room()
elif next == "straight":
second_left()
else:
print "What are you saying, bro?" …Run Code Online (Sandbox Code Playgroud) 根据我可以使用<img/> iOS Safari中很好地支持在元素中引用SVG的功能.但是在我的网站中,我在渲染图像时遇到问题:

HTML非常简单:
<img src='E1M1.svg' class='img-responsive'/>
Run Code Online (Sandbox Code Playgroud)
并且(根据卷曲)SVG正在使用Content-Type image/svg+xml:
HTTP/1.1 200 OK
x-amz-id-2: R7QpUKqnC7vYY/60mGfkkpk528vUlwwaMf8QS00Jvgg5H1EZk7NP6rkFdfvZsC3lLKX1HJXfqj8=
x-amz-request-id: 9D47859B55E37B45
Date: Thu, 22 May 2014 21:36:59 GMT
x-amz-meta-s3cmd-attrs: uid:501/gname:staff/uname:jasonsperske/gid:20/mode:33188/mtime:1400783207/atime:1400783431/ctime:1400783207
Last-Modified: Thu, 22 May 2014 18:39:44 GMT
ETag: "3d7db47140e0f7e34e33ae8cbefaf022"
Content-Type: image/svg+xml
Content-Length: 41276
Server: AmazonS3
Run Code Online (Sandbox Code Playgroud)
它也是有效的SVG.有任何想法吗?
在深入了解uniform.js源代码的过程中,我发现了这一点(第333行):
/**
* Meaningless utility function. Used mostly for improving minification.
*
* @return false
*/
function returnFalse() {
return false;
}
Run Code Online (Sandbox Code Playgroud)
在某些地方调用此函数(趋势似乎是在false像这样的JavaScript Object Literal中使用值的情况下(来自#351行)
bindMany($elem, options, {
'selectstart dragstart mousedown': returnFalse
});
Run Code Online (Sandbox Code Playgroud)
但不是在明确返回false的情况下.我的问题是为什么要说改善缩小?我的第一个想法是,通过拉false入一个函数,函数可以重命名为'a',所以false预期它可以表示为a(),但这似乎并没有为我认为是一个好处提供了很多好处.执行中额外功能的成本.我有什么不明白的事情吗?
如果给出一个n多边形,一条长度线k(at x,y和angle a),是否有一个算法来检测我碰撞过的多边形的哪一边(如果有的话)?到目前为止,我已经尝试测试是否x,y在多边形之外,然后遍历多边形的每个边缘,计算到每个端点的距离.这是一个JS小提琴,展示了我创造的世界.
这是JavaScript(HTML和CSS不值得复制):
var eventLoop,
maxVelocity = 10,
agility = 5,
baseLength = 5,
degree = ((2*Math.PI)/360),
world = document.getElementById('world'),
context = world.getContext("2d"),
boundry = [[180, 120],[240, 60],[360, 40],[420, 120],[360, 220],[350, 240],[360, 265],[470,360],[450,480],[360,540],[240,550],[140,480],[120,470],[100,360],[120,300],[220,240],[240,220]],
camera = {
location: {
x:300,
y:90
},
angle: 0,
velocity: 0
},
engine = {
drawWorld: function(shape, context) {
var point,
index,
size = shape.length;
context.clearRect(0, 0, world.width, world.height);
context.beginPath();
for(index = 0; index < …Run Code Online (Sandbox Code Playgroud) javascript algorithm collision-detection polygons computational-geometry
我正在尝试为节点中的MongoDB中存储的页面生成URL.
使用以下函数,我想遍历一个javascript对象,并显示每个元素的路径.
我几乎在那里,但我被卡住了 - 使用Async甚至可能有更好的方法来做到这一点(我必须承认,让我有点困惑).
功能:( 演示)
function printTree(people, slug) {
for (var p = 0; p < people.length; p++) {
var root = people[p];
slug = slug + root.name + "/";
console.log(slug);
if (root.children.length > 0) {
var childrenCount = root.children.length;
for (var c = 0; c < childrenCount; c++) {
if (root.children[c].children.length > 0) {
printTree(root.children[c].children, slug + root.children[c].name + "/");
}
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
输出:
/michael/
/michael/angela/oscar
/michael/meredith/creed
/michael/meredith/creed/kelly
Run Code Online (Sandbox Code Playgroud)
预期产出:
/michael/
/michael/angela/
/michael/angela/oscar/
/michael/meredith/
/michael/meredith/creed/ …Run Code Online (Sandbox Code Playgroud) 这一切都开始了,当我开始安装请求库为Python 3(我在OSX小牛运行与Python 2.7.5(由安装brew install python)和3.4.2(由安装brew install python3)。当我运行pip3 --version(或任何有关pip3命令),我看到以下内容:
$ pip3 --version
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 7, in <module>
from pip import main
File "/usr/local/lib/python3.4/site-packages/pip/__init__.py", line 11, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/local/lib/python3.4/site-packages/pip/vcs/mercurial.py", line 9, in <module>
from pip.download import path_to_url
File "/usr/local/lib/python3.4/site-packages/pip/download.py", line 22, in <module>
from pip._vendor import requests, six
File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.py", …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析(并转义)存储在Windows-1251字符编码中的CSV文件的行.使用这个优秀的答案来处理这种编码我最终得到了这一行来测试输出,由于某种原因,这是有效的:
print(row[0]+','+row[1])
Run Code Online (Sandbox Code Playgroud)
输出:
??????? ??????? ???????????,1 ????
Run Code Online (Sandbox Code Playgroud)
虽然这条线不起作用:
print("{0},{1}".format(*row))
Run Code Online (Sandbox Code Playgroud)
输出此错误:
Name,Variant
Traceback (most recent call last):
File "Russian.py", line 26, in <module>
print("{0},{1}".format(*row))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-3: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
以下是CSV的前两行:
Name,Variant
??????? ??????? ???????????,1 ????
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,这里是Russian.py的完整来源:
import csv
import cgi
from chardet.universaldetector import UniversalDetector
chardet_detector = UniversalDetector()
def charset_detect(f, chunk_size=4096):
global chardet_detector
chardet_detector.reset()
while 1:
chunk = f.read(chunk_size)
if not chunk: break
chardet_detector.feed(chunk)
if chardet_detector.done: break
chardet_detector.close()
return chardet_detector.result …Run Code Online (Sandbox Code Playgroud) javascript ×4
python ×4
algorithm ×1
android ×1
csv ×1
facebook ×1
html ×1
math ×1
memoization ×1
minify ×1
mongodb ×1
nested-sets ×1
node.js ×1
optimization ×1
polygons ×1
setuptools ×1
svg ×1
windows-1251 ×1