编写以下代码的pythonic方式是什么?
extensions = ['.mp3','.avi']
file_name = 'test.mp3'
for extension in extensions:
if file_name.endswith(extension):
#do stuff
Run Code Online (Sandbox Code Playgroud)
我有一个模糊的记忆,for
可以避免循环的显式声明并写入if
条件.这是真的?
我一直在研究Ruby并发现它的关键字"直到"和"除非"非常有趣.所以我认为在C/C++中添加类似关键字的好方法是什么.这就是我想出的:
#define until(x) while(!(x))
#define unless(x) if(!(x))
Run Code Online (Sandbox Code Playgroud)
我正在寻找一些建议.谁能提出更好的选择?
这是我编写的一个程序示例,用于说明我打算做什么:
#include <stdio.h>
#include <stdlib.h>
#define until(x) while(!(x))
#define unless(x) if(!(x))
unsigned int factorial(unsigned int n) {
unsigned int fact=1, i;
until ( n==0 )
fact *= n--;
return fact;
}
int main(int argc, char*argv[]) {
unless (argc==2)
puts("Usage: fact <num>");
else {
int n = atoi(argv[1]);
if (n<0)
puts("please give +ve number");
else
printf("factorial(%u) = %u\n",n,factorial(n));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果你能指出一些可以在C或C++中使用的类似技巧的参考文献,那将是很棒的.
如何在n
不使用递归的情况下遍历一棵树?
递归方式:
traverse(Node node)
{
if(node == null)
return;
for(Node child : node.getChilds()) {
traverse(child);
}
}
Run Code Online (Sandbox Code Playgroud) 经过30分钟的徒劳尝试捕获输出后wget
,我发现该程序写入stderr
而不是stdout
.在Web和堆栈溢出中搜索显示这是一个众所周知的事实.
知道为什么会这样吗?
以下是我找到第一个共同祖先的算法.但我不知道如何计算它的时间复杂度,任何人都可以帮忙吗?
public Tree commonAncestor(Tree root, Tree p, Tree q) {
if (covers(root.left, p) && covers(root.left, q))
return commonAncestor(root.left, p, q);
if (covers(root.right, p) && covers(root.right, q))
return commonAncestor(root.right, p, q);
return root;
}
private boolean covers(Tree root, Tree p) { /* is p a child of root? */
if (root == null) return false;
if (root == p) return true;
return covers(root.left, p) || covers(root.right, p);
}
Run Code Online (Sandbox Code Playgroud) 在C中,如果我们有一个像阵列a[10]
,然后a
与&a
具有相同的指针值(但不相同的类型).我想知道为什么C这样设计?
这是为了节省存储所需的额外空间&a
吗?......当你想到a
永远不会指向任何其他位置的事实时,这是有道理的,因此存储&a
是没有意义的.
嘿,我正在寻找一些帮助来找到一个算法,将一组正数分成k部分,这样每个部分都有(大约)相同的总和...让我们说我们有
1,2,3,4,5,6,7,8,9 zh k = 3算法应该像这样划分它1,2,3,4,5 | 6,7 | 8,9的顺序元素无法更改...找到一个贪婪的算法很容易,但我正在寻找一个总是返回最佳解决方案的回溯版本...
Annyone得到了什么提示?
algorithm recursion partitioning backtracking partition-problem
最近我对子集求和问题产生了兴趣,该问题是在超集中找到零和子集.我在SO上找到了一些解决方案,此外,我遇到了一个使用动态编程方法的特定解决方案.我根据他的定性描述在python中翻译了他的解决方案.我正在尝试对更大的列表进行优化,这会占用大量的内存.有人可以推荐优化或其他技术来解决这个特殊问题吗?这是我在python中的尝试:
import random
from time import time
from itertools import product
time0 = time()
# create a zero matrix of size a (row), b(col)
def create_zero_matrix(a,b):
return [[0]*b for x in xrange(a)]
# generate a list of size num with random integers with an upper and lower bound
def random_ints(num, lower=-1000, upper=1000):
return [random.randrange(lower,upper+1) for i in range(num)]
# split a list up into N and P where N be the sum of the negative values and P …
Run Code Online (Sandbox Code Playgroud) 我们在磁盘中有几个巨大的文件(大于RAM的大小).我想在python中逐行读取它们并在终端输出结果.我已经完成了[1]和[2],但我正在寻找不等到整个文件被读入内存的方法.
我将使用这两个命令:
cat fileName | python myScript1.py
python myScript2.py fileName
Run Code Online (Sandbox Code Playgroud)
我是一个蟒蛇新手,我正在尝试为Eratosthenes的筛子编写代码.为此,我必须初始化一组空集.我试过这样做factors=[set()]*1001
,但这会产生一个浅的副本.我想有一个深拷贝,以便factors[i]
和factors[j]
点不同的集合.这样做有一个简单的语法吗?
我在我的盒子里部署了一个Web服务.我想用各种输入检查这项服务的结果.这是我正在使用的代码:
import sys
import httplib
import urllib
apUrl = "someUrl:somePort"
fileName = sys.argv[1]
conn = httplib.HTTPConnection(apUrl)
titlesFile = open(fileName, 'r')
try:
for title in titlesFile:
title = title.strip()
params = urllib.urlencode({'search': 'abcd', 'text': title})
conn.request("POST", "/somePath/", params)
response = conn.getresponse()
data = response.read().strip()
print data+"\t"+title
conn.close()
finally:
titlesFile.close()
Run Code Online (Sandbox Code Playgroud)
打印相同行数后,此代码出错(28233).错误信息:
Traceback (most recent call last):
File "testService.py", line 19, in ?
conn.request("POST", "/somePath/", params)
File "/usr/lib/python2.4/httplib.py", line 810, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.4/httplib.py", line 833, in _send_request
self.endheaders() …
Run Code Online (Sandbox Code Playgroud) python ×5
algorithm ×4
c ×2
arrays ×1
backtracking ×1
binary-tree ×1
c++ ×1
filter ×1
linux ×1
list ×1
partitioning ×1
pointers ×1
recursion ×1
stderr ×1
stdout ×1
string ×1
subset-sum ×1
syntax ×1
wget ×1