嘿。这个例子非常具体,但我认为它可以适用于广泛的功能。它取自一些在线编程竞赛。
有一个游戏的获胜条件很简单。抽奖是不可能的。游戏不可能永远持续下去,因为每一步都会让你更接近终止条件。在给定状态的情况下,该函数应该确定现在要移动的玩家是否有获胜策略。在示例中,状态是一个整数。玩家选择一个非零数字并将其从数字中减去:新状态是新整数。获胜者是达到零的玩家。
我这样编码:
from Memoize import Memoize
@Memoize
def Game(x):
if x == 0: return True
for digit in str(x):
if digit != '0' and not Game(x-int(digit)):
return True
return False
Run Code Online (Sandbox Code Playgroud)
我认为它是如何工作的很清楚。我也意识到对于这个特定的游戏,可能有一个更聪明的解决方案,但我的问题是笼统的。然而,即使对于相对较小的输入,这也会使 python 变得疯狂。有什么方法可以使此代码与循环一起工作?
谢谢
这就是我所说的转换为循环的意思:
def fac(x):
if x <= 1: return x
else: return x*fac(x-1)
def fac_loop(x):
result = 1
for i in xrange(1,x+1):
result *= i
return result
## dont try: fac(10000)
print fac_loop(10000) % 100 ## works
Run Code Online (Sandbox Code Playgroud) 我有一个触发器来检查表中的单个列,当该列中有更新(AFTER UPDATE)时,我的触发器被调用,然后我从触发器中调用存储过程,以便执行一些编码的业务逻辑Java的.
到现在为止还挺好.现在情况变得越来越复杂,有一个新的要求意味着如果其他4个表中的列发生变化,也应该执行相同的逻辑(触发器执行的逻辑).我认为在5个不同的表中使用相同的触发器来监听不同的列是不切实际的,我不确定是否应该考虑为这些列创建"视图"并在该视图中放置一个触发器(成本是多少)在性能和/或资源方面付费?)
在您之前使用Oracle的过程中,您对此方案的方法或解决方案是什么?
我想使用https来使用WebClient的UploadData方法上传文件.我想忽略服务器端证书验证(始终接受服务器证书并且仅使用https的加密功能).
我努力但却找不到类似的样品.谁能告诉我如何实施?有参考样品吗?
我使用的是VSTS2008 + C#+ .Net 3.5.目标服务器与IIS 7.0 + Windows Vista x64一起运行.
我正在通过TCP/IP发送消息,我需要在char数组中为消息长度添加前缀,然后发送它.我该怎么做?
还请你提供一个如何在另一端提取它的例子.如果可能的话,请解释一下.
我正在使用C++和Winsock.
编辑:
string writeBuffer = "Hello";
unsigned __int32 length = htonl(writeBuffer.length());
Run Code Online (Sandbox Code Playgroud)
它没有返回正确的长度而是非常大的数字.
对于接收部分,如果我使用ntohl(),那么我也得到一个大数而不是正确的长度?为什么会这样?我收到这样的
bool Server::Receive(unsigned int socketIndex)
{
// Read data from the socket
if (receivingLength)
{
bytesReceived = recv(socketArray[socketIndex - WSA_WAIT_EVENT_0],
((char*)&messageLength) + bytesReceived, MESSAGE_LENGTH_SIZE - bytesReceived, 0);
if (bytesReceived == SOCKET_ERROR)
{
return false;
}
if (bytesReceived == MESSAGE_LENGTH_SIZE)
{
// If uncomment the following line,
// I won't get the correct length, but a large number
//messageLength = ntohl(messageLength);
receivingLength = false;
bytesReceived = 0; …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的类浏览器对话框,允许用户打开一个程序集并从内部选择一个静态方法.但是,在某些情况下会缺少程序集的依赖项.
因为我只需要方法名而不是它的完整原型,有没有办法通过调用Assembly.ReflectionOnlyLoadFrom时引发的FileNotFoundException ?我目前正在处理AppDomain.ReflectionOnlyAssemblyResolve但是如果我返回null,那么我得到一个FileLoadException并带有以下消息:
无法解析对程序集"..."的依赖性,因为它尚未预加载.使用ReflectionOnly API时,必须通过ReflectionOnlyAssemblyResolve事件按需预加载或加载相关的程序集.
我知道这一定是可能的,因为.NET Reflector允许您跳过未解析的依赖项.但是我开始认为,Reflector可能会手动解析程序集,而不是将其加载到CLR中并使用标准反射.也许它使用Mono项目中的Cecil之类的东西.
我想在浏览器中打开PDF,以便我可以使用以下代码,但我发生了错误:"文件不以%PDF开头".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div style="width:358px;height:206px;">
<img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_home_image_117713.jpg" border="2" width="358" height="206" />
<img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_mug_image_117713.jpg" border="1" width="68" height="68" style="position:absolute; top:190px; left:25px; z-index:2"/>
<font style="position:absolute; top:15px; left:15px; z-index:2; border:1px; color:FFFFFF;font-family:impact;font-size:14px;">
</font>
</div>
<div style="width:425px;height:206px;padding-top:5px; padding-left:75px; text-align:center; border:1px;font-family:Arial;font-size:9px;">
</div>
</body>
</html>
<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">
<cfcontent type="application/pdf">
Run Code Online (Sandbox Code Playgroud) 我有一个在django中运行的小型多线程脚本,随着时间的推移,它开始使用越来越多的内存.离开它一整天吃大约6GB的RAM,我开始交换.
在http://www.lshift.net/blog/2008/11/14/tracing-python-memory-leaks之后,我将其视为最常见的类型(仅使用了800M的内存):
(Pdb) objgraph.show_most_common_types(limit=20)
dict 43065
tuple 28274
function 7335
list 6157
NavigableString 3479
instance 2454
cell 1256
weakref 974
wrapper_descriptor 836
builtin_function_or_method 766
type 742
getset_descriptor 562
module 423
method_descriptor 373
classobj 256
instancemethod 255
member_descriptor 218
property 185
Comment 183
__proxy__ 155
Run Code Online (Sandbox Code Playgroud)
没有表现出任何奇怪的东西.我现在该怎么做才能帮助调试内存问题?
更新:尝试一些人们推荐的东西.我一夜之间运行程序,当我工作时,使用了50%*8G == 4G的RAM.
(Pdb) from pympler import muppy
(Pdb) muppy.print_summary()
types | # objects | total size
========================================== | =========== | ============
unicode | 210997 | 97.64 MB
list | 1547 | …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用FileLock在Windows环境中使用Java锁定文件,我遇到了一个问题:在我锁定文件之后,至少在某种程度上它仍然可以被其他进程访问.
示例代码如下:
public class SimpleLockExample {
public static void main(String[] args) throws Exception {
String filename = "loremlipsum.txt";
File file = new File(filename);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel channel = raf.getChannel();
FileLock lock = null;
try {
lock = channel.tryLock();
String firstLine = raf.readLine();
System.out.println("First line of file : " + firstLine);
waitForEnter();
lock.release();
} catch (OverlappingFileLockException e) {
e.printStackTrace();
}
lock.release();
System.out.println("Lock released");
channel.close();
}
private static void waitForEnter() throws Exception {
BufferedReader reader = …Run Code Online (Sandbox Code Playgroud) .net ×2
c# ×2
java ×2
python ×2
arrays ×1
assemblies ×1
c++ ×1
cfdocument ×1
clr ×1
coldfusion ×1
date ×1
debugging ×1
django ×1
filelock ×1
format ×1
https ×1
locale ×1
memory-leaks ×1
oracle ×1
pdf ×1
php ×1
recursion ×1
refactoring ×1
reflection ×1
tcp ×1
triggers ×1
view ×1
windows ×1
winsock ×1