谁懂如何把Java的代码Transfer成Python的呢? 期中涉及到两个Java的Class 换成一个Python的. 只需要两个Function

2013-04-13 09:56:15 +08:00
 TigerS
public int distance (Vertex x, Vertex y) {
int returnVal = 0;
Queue que = new LinkedList();
x.touched = true;
que.add(x);
while (!que.isEmpty()) {
returnVal++;
Vertex n = (Vertex)que.remove();
Vertex child = null;
while ((child = getUnvisited(n))!=null) {
if (child.value==y.value)
return returnVal;
child.touched=true;
que.add(child);
}
}
return returnVal;
}

public int diameter () {
int maxDistance = 0;
int tempDistance = 0;
Vertex one;
Vertex two;
for (int x = 1; x < numNodes; x++) {
one = (Vertex)vert.get(x);
for (int y = 0; y < x; y++) {
two = (Vertex)vert.get(y);
tempDistance = distance(one, two);
if (tempDistance > maxDistance)
maxDistance = tempDistance;
refreshTouched();
}
}
return maxDistance;
}

==============

Vertex class

public class Vertex {
public boolean touched;
public int value;
public Vertex (int value) {
touched = false;
this.value = value;
}
}

====================

主要需要实现上面的两个功能.
有些部分比如 x.touched = true; 这个需要调用到第二个class vertex 不知道怎么在Python中(一个文件,没有class)表示
2358 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/65768

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX