我有一个 Google App Engine 日期时间属性,我用x.date = datetime.datetime.now(). 我在日期之间进行了大量比较,经过多次调试,结果发现我的客户端设备发送的日期精度低于 Python 日期,这导致了严重的混乱。
这是Python生成的:
2012-08-28 21:36:13.158497with datetime.datetime.now(),但我想要的是2012-08-28 21:36:13.158000(注意末尾的三个零。)
我怎样才能实现这个目标?(请记住,我不是想格式化字符串或任何东西。我想格式化日期对象。)
我想一种方法是将其格式化为具有所需精度的字符串,如下所示:
dateString = date.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
Run Code Online (Sandbox Code Playgroud)
然后回到日期对象。但一定有更好的方法。
我知道这是一个非常模糊的问题,但我正在寻找一个非常抽象的答案.自从几个月前我开始使用GAE以来,我总是对memcache不屑一顾,因为它没有用,也是一种麻烦的麻烦,它真的不那么重要了.但似乎memcache被认为是一个非常有益的功能,谷歌甚至说"高性能可扩展的Web应用程序经常在某些任务的前面或代替强大的持久存储使用分布式内存数据缓存."等等我认为必须有一些值得关注的事情.
我只是不明白.它对性能有何益处?首先,您必须检查内存中是否有内容,如果没有,请查询.我一直认为没有必要处理这个问题会更快,而且无论如何只是查询,但似乎这可能是一个天真的方法?这有多大差异?
我想我从未理解的内容是memcache有用的地方.我可以看到它在Stackoverflow主页中是多么有用,其中所有用户几乎都看到相同的东西,所以它会很有用,实际上很难在这样的情况下不使用memcache.但是像Facebook这样的社交网络.每个用户看到不同的东西.没有两个人看到相同的数据和内容,并且事情变化如此之快以至于memcache可能不断需要更新.memcache在这种情况下可以扮演什么角色?
此外,在像社交网络这样的私人网站中,如果每个用户都必须在memcache中存储不同的信息,那么memcache真正适合多少?我知道GAE没有谈到其内存缓存的大小,那么存储数十万条记录是否安全?
我有一个简单的 bash 脚本:
#!/bin/bash
JAVA_HOME=/usr
EC2_HOME=~/ec2-api
echo $EC2_HOME
export PATH=$PATH:$EC2_HOME/bin
Run Code Online (Sandbox Code Playgroud)
我像这样运行脚本
$ ./ec2
/Users/user/ec2-api
Run Code Online (Sandbox Code Playgroud)
该脚本运行并产生正确的输出。
但是,当我现在尝试访问 EC2_HOME 变量时,我什么也没得到:
$ echo $EC2_HOME
Run Code Online (Sandbox Code Playgroud)
我得到一个空字符串。我究竟做错了什么?
好吧,让我说我有boolean x = false.如果我有一个循环说明while (!x)这意味着x是非假(真)或x不是真(假)?
编辑:好的,我有点困惑,我想我得到了不同的答案.所以,如果我有
int x=0;
boolean add1=false;
while (!add1){
x=1;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,x的最终值是多少?
class Position {
private double x,y;
private int id;
public String toString(Position a){
String words ="(" + a.x + "," +a.y + ")";
return words;
Run Code Online (Sandbox Code Playgroud)
所以我在这里得到了一个内存地址.我究竟做错了什么?我想获得使用setter设置的x和y的实际值.我也有吸气剂,我试过而不是把斧头放入getX(),但这仍然给我另一个内存地址.我究竟做错了什么?
我正在尝试编写一个方法来查看某个颜色的对象数组,这也是一个对象.
public Ghost findFirst(Color c){
for (int i=0;i<ghosts.length;i++) {
if (ghosts[i].getColor()==c)
return ghosts[i];
else
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果某个幽灵的颜色与颜色c匹配,则返回该幽灵.但是,我收到了i ++的死代码警告.我的代码有什么问题?哦,我也得到一个方法错误,说这个函数应该返回一个鬼.我以为我是?
好的说我有一个函数可以在自定义LinkedList类中查找特定的单词:
public LinkedList find(String word) {
if (this.word.equals(word))
return this;
if (next==null)
return null;
if (next.find(word)==next)
return next;
return null;
}
Run Code Online (Sandbox Code Playgroud)
此代码工作正常,但它返回匹配条件的FIRST找到的对象.如果我想返回与参数匹配的LAST对象怎么办?我很难搞清楚这一点.请记住我想使用递归.
编辑:这段代码会出现什么问题:
public LinkedList findLast(String word) {
LinkedList temp=new LinkedList(word, null);
if (next==null && next.word.equals(word))
return next;
if (next==null && !next.word.equals(word))
temp=next.findLast(word);
return temp;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码,由于某些我试图弄清楚的原因,它无法正常工作:
public static int score(String gene1, String gene2){
char[] a=new char[gene1.length()];
char[] b=new char[gene2.length()];
a=gene1.toCharArray();
b=gene2.toCharArray();
return score(a, b, 0,0);
}
private static int score(char[] a, char[] b, int i, int j){
if(a[i]=='\0' || b[j]=='\0')
return 0;
else if (a[i]==b[j])
return 1+score(a, b, i+1, j+1);
else
return max(score(a, b,i+1, j),score(a, b, i, j+1));
}
private static int max (int a, int b){
if (a<b) return b;
else return a;
}
Run Code Online (Sandbox Code Playgroud)
这是它失败的地方:
assertEquals(2, GeneAnalysis.score("ACGT","AC"));
Run Code Online (Sandbox Code Playgroud)
我得到一个IndexOutofBoundsError
有任何想法吗?此外,在提供帮助时,请不要更改方法参数.他们应该是他们的方式.
我只是无法得到递归的悬念,尤其是复杂的例子.如果有人花些时间解释一下,我真的很感激.我确实有4张纸都跟我一起追踪这个功能,但我不知道怎么把它放在一起.
public static String shortestPath(int x, int y, int tX, int tY,boolean blocked[][]) {
if(x>blocked.length-1 || y>blocked[0].length-1 || x<0 || y<0 )
return null;
if(blocked[x][y]==true)
return null;
if(x==tX && y==tY)
return "";
String paths[]=new String[4];
blocked[x][y]=true; //this just means this coordinate is blocked, so dont use it
paths[0]=shortestPath(x, y+1, tX, tY, blocked);
paths[1]=shortestPath(x, y-1, tX, tY, blocked);
paths[2]=shortestPath(x+1, y, tX, tY, blocked);
paths[3]=shortestPath(x-1, y, tX, tY, blocked);
blocked[x][y] = false;
int result=findShortestString(paths, 0, 3);
//findShortestString just takes an array of …Run Code Online (Sandbox Code Playgroud) 我试图在Java中混洗2D对象数组.我认为Collections.shuffle可以解决这个问题,但看起来它只是将每行中的对象混合在一起,但不会将行混合在一起,这是我希望它做的.任何内置的方法或易于实现的方法可以为我洗牌2D数组?阵列是cards[13][4].