我正在做面试准备和审查图表实现.我一直看到的最重要的是邻接列表和邻接矩阵.当我们考虑基本操作的运行时,为什么我从未看到使用散列的数据结构?
例如,在Java中,通常会使用邻接列表ArrayList<LinkedList<Node>>,但为什么人们不使用HashMap<Node, HashSet<Node>>?
设n =节点数,m =边数.
在这两种实现方式,去掉了节点U包括通过所有的收藏和取出诉搜索.在邻接表,这是为O(n ^ 2),但在"邻接集",这是为O(n).同样,删除边缘涉及从v列表中删除节点u,从u列表中删除节点v.在邻接列表中,那是O(n),而在邻接集中,它是O(1).其他操作,例如查找节点后继,查找两个节点之间是否存在路径等,对于这两种实现都是相同的.空间复杂度也都是O(n + m).
我能想到的邻接集的唯一缺点是添加节点/边是分摊O(1),而在邻接列表中这样做是真正的O(1).
也许我没有看到任何东西,或者我在计算运行时忘了考虑事情,所以请告诉我.
我目前正在使用Selenium Webdriver在页面上进行一些验证.Webdriver由PhantomJS驱动.我知道在PhantomJS中,您可以使用如下示例来收听网络:(来自https://github.com/ariya/phantomjs/wiki/Network-Monitoring).
var page = require('webpage').create();
page.onResourceRequested = function (request) {
console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
console.log('Receive ' + JSON.stringify(response, undefined, 4));
};
page.open(url);
Run Code Online (Sandbox Code Playgroud)
如何在Webdriver中实现此功能?我可以将函数绑定到DesiredCapabilities吗?
我可以为SQLAlchemy Core连接使用上下文管理器吗?换句话说,是
conn = engine.connect()
conn.execute('SELECT * FROM FOO')
conn.close()
Run Code Online (Sandbox Code Playgroud)
同样的
with engine.connect() as conn:
conn.execute('SELECT * FROM FOO')
Run Code Online (Sandbox Code Playgroud)
我不需要在上下文管理器出口处检查提交,回滚等.
我正在尝试使用tastypie实现一个带RESTful API的简单Django服务.我的问题是,当我尝试使用PUT创建WineResource时,它工作正常,但是当我使用POST时,它会返回HTTP 501错误.阅读tastypie文档,似乎应该只是工作,但事实并非如此.
这是我的api.py代码:
class CustomResource(ModelResource):
"""Provides customizations of ModelResource"""
def determine_format(self, request):
"""Provide logic to provide JSON responses as default"""
if 'format' in request.GET:
if request.GET['format'] in FORMATS:
return FORMATS[request.GET['format']]
else:
return 'text/html' #Hacky way to prevent incorrect formats
else:
return 'application/json'
class WineValidation(Validation):
def is_valid(self, bundle, request=None):
if not bundle.data:
return {'__all__': 'No data was detected'}
missing_fields = []
invalid_fields = []
for field in REQUIRED_WINE_FIELDS:
if not field in bundle.data.keys():
missing_fields.append(field)
for key in bundle.data.keys(): …Run Code Online (Sandbox Code Playgroud) 这听起来很重要,但我找不到 Blueimp jQuery File Upload 的代码示例,它使浏览器的默认文件输入框(显示“选择文件...”和浏览的那个)元素隐藏,而是通过按钮触发对话框. 有人可以告诉我如何做到这一点吗?
我已经尝试将 input 元素放在一个按钮元素中,如本演示所示,但它在 Firefox 上不起作用。有什么方法可以使用 jQuery 来触发对话框。除了“浏览”按钮恰好与 Chrome 中的按钮对齐而不是 FF 中的按钮外,我不确定为什么这在 Chrome 中有效。
这是我基本上要做的事情:
<button id="upload-button" class="btn btn-primary btn-large" type="button">
<input type="file" name="image" id="fileupload" multiple data-url=""/>
Upload Images
</button><br>
我遇到了JQuery的问题:每当调用JQuery函数时,只要执行了if块,页面就会刷新.
$(".remove").click(function() {
removeOption($(this));
});
function removeOption(obj){
if (obj.parent().siblings().size() > 1){
obj.parent().remove();
}
}
Run Code Online (Sandbox Code Playgroud)
如果执行(obj.parent()...)块,页面将刷新.它不仅限于函数:如果我将if(obj.parent()...)更改为if(true),我也会遇到同样的问题.如果我把removeOption(obj)作为匿名函数放在第一个函数中,我也有问题.这可能是JQuery中的错误,还是有人有任何见解?
我正在使用Python的crypt包来保存Django网站的MySQL数据库中的加密密码.我不确定这是不是一个bug,但这是我正在使用的代码:
要加密/保留密码:
user.password = crypt(request.POST['password'], netid)
user.save()
Run Code Online (Sandbox Code Playgroud)
要在登录时检查密码是否正确:
if crypt(password, email) == user.password:
# Grant acccess to the user's profile, etc.
Run Code Online (Sandbox Code Playgroud)
问题如下.如果我有可变密码进行加密netid = test@example.com和request.POST['password'] = 'ABC123abc',它工作正常.但是,当我尝试登录时,如果我使用密码'ABC123abc[trailing_chars]',其中trailing_chars可以是任何有效的字符串,我仍然可以登录.为什么会这样?它本身就是一个很大的安全漏洞.
我正在尝试构建一个快速的Web爬虫,因此,我需要一种有效的方法来查找页面上的所有链接.快速XML/HTML解析器(如lxml)和使用正则表达式匹配之间的性能比较是什么?
我正在尝试用Python制作内存中的zip文件并将其上传到Amazon S3.我已经阅读过关于此事的类似帖子,但无论我尝试什么,Windows和Linux(RHEL5)都无法打开它(它已经损坏).这是我正在运行的代码:
f_redirects = StringIO()
f_links = StringIO()
f_metadata = StringIO()
# Write to the "files"
zip_file = StringIO()
zip = zipfile.ZipFile(zip_file, 'a', zipfile.ZIP_DEFLATED, False)
zip.writestr('redirects.csv', f_redirects.getvalue())
zip.writestr('links.csv', f_bad_links.getvalue())
zip.writestr('metadata.csv', f_metadata.getvalue())
f_redirects.close()
f_links.close()
f_metadata.close()
k = Key(BUCKET)
k.key = '%s.zip' % base_name
k.set_metadata('Content-Type', 'application/zip')
k.set_contents_from_string(zip_file.getvalue())
zip.close()
zip_file.close()
Run Code Online (Sandbox Code Playgroud) 假设我有一个递归函数,它在具有n节点和高度的完美平衡的二叉树上工作,并log(n)在树的根部调用下面的函数。
void printPaths(TreeNode root, int[] array, int depth){
if (root == null){
print array;
}
array[depth] = root.data;
printPaths(root.left, array, depth + 1);
printPaths(root.right, array, depth + 1);
}
array = new int[depthOfTree]
printPaths(root, array, 0);
Run Code Online (Sandbox Code Playgroud)
让数组为长度log(n)(它将沿树的路径存储值)。我知道递归调用堆栈将是 max height log(n)。我不确定的是 Java 的“按值传递”性质和 Java 垃圾收集如何影响时间和空间复杂性。
1) 将数组传递给递归调用的时间复杂度是多少?如果 Java 是“按值传递”,那么O(log(n))在开始任何函数执行之前,每个递归调用是否都只是简单地复制数组?
2) 任一时刻在内存中浮动的这些数组副本的上限是多少?我的倾向是说O(log(n))。这是否意味着空间复杂度是O(log(n)log(n))?我在一本书中读到“空间复杂度是O(log(n))因为算法会递归O(log(n))次数,并且路径参数O(log(n))在递归过程中只在空间分配一次”。
java recursion pass-by-value time-complexity space-complexity
python ×5
django ×2
javascript ×2
jquery ×2
ajax ×1
algorithm ×1
amazon-s3 ×1
crypt ×1
encryption ×1
file-upload ×1
graph ×1
html-parsing ×1
java ×1
lxml ×1
mysql-python ×1
phantomjs ×1
recursion ×1
regex ×1
rest ×1
runtime ×1
selenium ×1
sqlalchemy ×1
stringio ×1
tastypie ×1
unix ×1
web-crawler ×1
webdriver ×1
zip ×1
zipfile ×1