我是python的新手,我开始使用后缀树.我可以构建它们,但是当字符串变大时我遇到了内存问题.我知道它们可以用于处理大小为4 ^ 10或4 ^ 12的DNA字符串,但每当我尝试实现一种方法时,我最终会遇到内存问题.
这是我生成字符串和后缀树的代码.
import random
def get_string(length):
string=""
for i in range(length):
string += random.choice("ATGC")
return string
word=get_string(4**4)+"$"
def suffixtree(string):
for i in xrange(len(string)):
if tree.has_key(string[i]):
tree[string[i]].append([string[i+1:]][0])
else:
tree[string[i]]=[string[i+1:]]
return tree
tree={}
suffixtree(word)
Run Code Online (Sandbox Code Playgroud)
当我达到4**8左右时,我遇到了严重的内存问题.我对此很陌生,所以我肯定我错过了保存这些东西的东西.任何建议将不胜感激.
作为注释:我想进行字符串搜索以在非常大的字符串中查找匹配的字符串.搜索字符串匹配大小为16.因此,这将在大字符串中查找大小为16的字符串,然后移动到下一个字符串并执行另一个搜索.由于我将进行大量搜索,因此建议使用后缀树.
非常感谢
double get_random(double min, double max) {
/* Returns a random double between min and max */
return min * ((double) rand() / (double) RAND_MAX) - max;
}
Run Code Online (Sandbox Code Playgroud)
这是我在最小和最大之间产生随机双打的功能.但是,当我打电话时get_random(-1.0, 1.0);,我得到-2.0和-1.0之间的值.
知道我做错了什么以及如何解决它?
我有一个函数声明为:
void foo(unsigned int x)
Run Code Online (Sandbox Code Playgroud)
如何检查 foo() 没有收到负数?我希望如果我调用 foo(-1) 会抛出异常,但当然由于 x 会自动转换为无符号,因此我无法检查其正性,例如:
if not(x>=0){ do_something();};
Run Code Online (Sandbox Code Playgroud)
或类似的检查。
你好,我有以下代码:
enum {a, b, c, d, ..., z} abc;
int main()
{
int val = 20;
if (val == a || val == b ||val == c||val == d..... || val == z)
{
/*Do something*/
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以跳过OR操作,因为如果有1000个枚举成员,那么我们如何才能提前检查所有成员.请帮忙.
所以我正在玩堆栈,我已经在我的主函数中填充了一个,但现在我想把它传递给我的其他函数,所以我可以遍历它.我不确定将什么类型的数据类型放入原型中以便它接受它.建议?这就是我所拥有的:
Main.cpp的
#include <iostream>
using namespace std;
#include "stack.h"
void displayStack(char &stackRef);
int main()
{
Stack<char> stack;
stack.push('a');
stack.push('b');
stack.push('c');
return 0;
};
void displayStack(char starRef)
{
// Cannot Get here - Errors!
};
Run Code Online (Sandbox Code Playgroud)
它告诉我我有太多的参数,它与参数列表不匹配.
嗨,我刚开始学习C++.我为Dummies书买了这个大C++,并且经历过它.到目前为止它真的很有趣,但现在我被卡住了.我一直在谷歌搜索这个问题,但无济于事.我正在使用我使用的代码块10.05与GNU GCC.
我一直收到错误消息:
In function 'main':
undefined reference to 'SafeCracker(int)'
Run Code Online (Sandbox Code Playgroud)
代码并不复杂.我只是新人,非常沮丧.我不想跳过这一部分; 我想知道发生了什么.
主要:
#include <iostream>
#include "safestuff.h"
using namespace std;
int main()
{
cout << "Surprise, surprise!" << endl;
cout << "The combination is (once again)" << endl;
cout << SafeCracker(12) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
功能:
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "13-26-16";
}
Run Code Online (Sandbox Code Playgroud)
标题:
using namespace std;
#ifndef SAFESTUFF_H_INCLUDED
#define SAFESTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // SAFESTUFF_H_INCLUDED
Run Code Online (Sandbox Code Playgroud) 我理解C++中引用的概念,我理解它们在函数参数中使用时的作用,但我仍然对它们如何使用返回类型感到困惑.
例如,在参数中使用时,此代码:
int main (void) {
int foo = 42;
doit(foo);
}
void doit (int& value) {
value = 24;
}
Run Code Online (Sandbox Code Playgroud)
类似于这段代码:
int main (void) {
int foo = 42;
doit(&foo);
}
void doit (int* value) {
*value = 24;
}
Run Code Online (Sandbox Code Playgroud)
(知道该编译器会自动把一个星号前面值每次它的第一个代码示例中使用时间DOIT,但在后者,你也许就有您尝试使用的时候把星号在自己值)
因此,当用作参考时,下一个代码(使用返回类型中的引用)转换为什么?它是否返回指向int的指针?或者只是返回一个int?
int main (void) {
int* foo = /*insert useful place in memory*/;
foo = doit(foo);
}
int& doit (int* value) {
//insert useful code
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试用一些Java背景学习C++,我正在尝试编写返回两个列表交集的代码.我相信我在概念上有正确的想法,但由于没有任何编译,我的语法有问题.
这是我提出的代码:
#include <iostream>
using namespace std;
#include <list>
template <typename Object>
list<Object> intersection( const list<Object> & L1, const list<Object> & L2){
std::list<Object> result;
int pos1 = 0;
int pos2 = 0;
while (pos1 < L1.size() && pos2 < L2.size()) {
if (L1[pos1] > L1[pos2]) {
pos1++;
} else if (L2[pos2] > L1[pos1]) {
pos2++;
} else {
result.push_back(L2[pos2]);
pos1++;
pos2++;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我认为我需要的东西:一个迭代器(我确定我访问列表的方式不正确)
我有一个程序可以“一个一个”地创建新进程。是否可以更改此代码,以便创建进程的“列表”——即子进程 1 是子进程 2 的父进程,子进程 2 是子进程 3 的父进程,等等?
#include <string>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "err.h"
using namespace std;
int main ()
{
pid_t pid;
int i;
cout << "My process id = " << getpid() << endl;
for (i = 1; i <= 4; i++)
switch ( pid = fork() ) {
case -1:
syserr("Error in fork");
case 0:
cout << "Child process: My process id = " << getpid() << endl;
cout << …Run Code Online (Sandbox Code Playgroud) 我刚刚遇到了凸优化的cvxopt包的问题,我在文档中没有提到.我想知道是否有人知道是什么导致它以及如何最好地解决它.
问题是,当您import cvxopt在使用multiprocessing.Process实例的Python程序中时,进程不能再并行运行.它们似乎自动同步.请注意,无论程序是否cvxopt实际使用了任何功能,都会发生这种情况.只需导入包就会产生这种效果.
例:
# import cvxopt
from multiprocessing import Queue, Process
def compute(queue):
"""
Pick integers from a queue and perform some useless
calculations on them just to keep the CPU busy.
"""
total = 0
while True:
item = queue.get()
if item is None:
break
for i in range(item):
total += i
if __name__ == '__main__':
queue = Queue()
procs = []
for i in range(4):
proc = …Run Code Online (Sandbox Code Playgroud) c++ ×8
python ×2
c ×1
codeblocks ×1
cvxopt ×1
enums ×1
fork ×1
function ×1
hash ×1
int ×1
intersection ×1
iterator ×1
list ×1
parameters ×1
python-3.x ×1
random ×1
reference ×1
return-type ×1
stack ×1
suffix-tree ×1
unsigned ×1