小编mad*_*mma的帖子

为什么我的函数抛出'StopIteration'异常?

我有一个使用发生器计算毕达哥拉斯三元组的函数.但是,当我调用next(myfunc())它时会抛出此错误:

Traceback (most recent call last):
  File "path omitted", line 124, in <module>
    next(x)
StopIteration
Run Code Online (Sandbox Code Playgroud)

哪里 x = myfunc()

这是我的功能:

import math

def myfunc():
    i = 1
    for z in range(0, i):
        for y in range(0, z):
            for x in range(0, y):
                if (math.pow(x, 2) + math.pow(y, 2)) == math.pow(z, 2):
                    yield (x*y*z)
                    i += 1
Run Code Online (Sandbox Code Playgroud)

python generator

1
推荐指数
1
解决办法
250
查看次数

以整数形式存储数组中的位

所以我有一个位数组,在字符数组中基本上是0和1.

现在我想要做的是将这些位存储在另一个数组(int数组)中的整数中,但我不知道该怎么做.

这是我获取位的代码:

char * convertStringToBits(char * string) {
    int i;
    int stringLength = strlen(string);
    int mask = 0x80; /* 10000000 */
    char *charArray;
    charArray = malloc(8 * stringLength + 1);
    if(charArray == NULL) {
        printf("An error occured!\n");
        return NULL; //error - cant use charArray
    }

    for(i = 0; i < stringLength; i++) {
        mask = 0x80;
        char c = string[i];
        int x = 0;
        while(mask > 0) {
            char n = (c & mask) > 0;
            printf("%d", n);
            charArray[x++] …
Run Code Online (Sandbox Code Playgroud)

c arrays

1
推荐指数
1
解决办法
181
查看次数

从 Cassandra CQL 中的多个表中进行选择

所以我正在使用的查询中有两个表:

SELECT
    R.dst_ap, B.name
FROM airports as A, airports as B, routes as R
WHERE R.src_ap = A.iata
AND R.dst_ap = B.iata;
Run Code Online (Sandbox Code Playgroud)

但是它抛出错误:

mismatched input 'as' expecting EOF (..., B.name    FROM airports [as] A...)
Run Code Online (Sandbox Code Playgroud)

无论如何,我可以在 Cassandra CQL 中做我想做的事情(这就是它的相关工作方式)吗?

cql cassandra nosql cql3 cqlsh

1
推荐指数
1
解决办法
4938
查看次数

Dijkstra的最短路径算法不返回最小权重的最短路径

我正在使用一个名为JGraphT的图形库,在我的程序中,我有几个顶点通过边缘连接在一起,并且具有行程成本的权重.

在一个例子中,我只是一个整数,它的工作原理!但是,当我改变这个使用我的班级FlightData作为重量时,它不起作用.

这是我的代码,权重只是一个整数:

List<DefaultWeightedEdge> path = DijkstraShortestPath.findPathBetween(graph, start, end);
    for(int i = 0; i < path.size(); i++) {
        DefaultWeightedEdge edge = path.get(i);
        System.out.println((i+1) + " " + graph.getEdgeSource(edge) + " -> " + graph.getEdgeTarget(edge));
    }
Run Code Online (Sandbox Code Playgroud)

这是我的FlightData类的权重代码:

List<FlightData> path = DijkstraShortestPath.findPathBetween(graph, start, end);    
for(int i = 0; i < path.size(); i++) {
        FlightData f = path.get(i);
    System.out.println((i+1) + " " + graph.getEdgeSource(f) + " -> " + graph.getEdgeTarget(f));
}
Run Code Online (Sandbox Code Playgroud)

我的FlightData类只是一个带有访问器方法的类:

import org.jgrapht.graph.DefaultWeightedEdge;

public class FlightData extends DefaultWeightedEdge
{
    private …
Run Code Online (Sandbox Code Playgroud)

java dijkstra jgrapht jgraph

0
推荐指数
1
解决办法
695
查看次数

我的二叉树是否正确添加节点?

我刚刚创建了一个测试二叉树实现高度的方法,如下所示:

public int height() {
    return height(rootNode);
}
private int height(BinaryTreeNode node) {
    if(node == null) return -1;
    else return 1 + Math.max(height(node.getLeftChild()), height(node.getRightChild()));
}
Run Code Online (Sandbox Code Playgroud)

但是当我添加节点1-6时,它返回高度6,而不是7.

这是我的二叉树代码:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree<E extends Comparable<E>>
{
    private class BinaryTreeNode
    {
        private E value;
        private BinaryTreeNode leftChild, rightChild;

        public BinaryTreeNode(E value) {
            this(value, null, null);
        }

        public BinaryTreeNode(E value, BinaryTreeNode leftChild, BinaryTreeNode rightChild) {
            this.value = value;
            this.leftChild = leftChild;
            this.rightChild = rightChild;
        }

        public E …
Run Code Online (Sandbox Code Playgroud)

java binary-tree

0
推荐指数
1
解决办法
130
查看次数

尝试在C++中打印数组时,'Table'格式不好

所以我试着打印出一个表格,即使是填充也是如此:

int rowLength = 8 * 4 + 8 + 1;
char arr[rowLength];

fill(arr, arr + rowLength, '-');
string divider = arr;

for(int y = 0; y < 5; y++) {
    cout << "\n" << divider << "\n";
    for(int x = 0; x < 8; x++) {
        cout << "|  " << "1";
        if(x == 7)
            cout << "|";
    }
}
cout << "\n" << divider;
Run Code Online (Sandbox Code Playgroud)

而且我希望桌子能够均匀打印,但它打印如下:

图片1

有谁知道我如何修复打印代码,所以它均匀打印,最后没有2小时?

谢谢!

c++

0
推荐指数
1
解决办法
57
查看次数

标签 统计

java ×2

arrays ×1

binary-tree ×1

c ×1

c++ ×1

cassandra ×1

cql ×1

cql3 ×1

cqlsh ×1

dijkstra ×1

generator ×1

jgraph ×1

jgrapht ×1

nosql ×1

python ×1