小编Ber*_*rer的帖子

替换Rust中的路径部分

假设我有以下三条路径:

let file = path::Path::new("/home/meurer/test/a/01/foo.txt");
let src = path::Path::new("/home/meurer/test/a");
let dst = path::Path::new("/home/meurer/test/b");
Run Code Online (Sandbox Code Playgroud)

现在,我想复制filedst,但为此我需要更正路径,以便我可以new_file使用解析的路径/home/meurer/test/b/01/foo.txt.换句话说,如何src从中删除file然后将结果附加到dst

/home/meurer/test/a/01/foo.txt - > /home/meurer/test/b/01/foo.txt

请注意,我们不能假设src总是这样dst.

formatting replace path rust

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

确定两个字符串匹配多少

假设我们有3个字符串

songA = "(Used to Be A) Cha-Cha.flac"
songB = "(Used to Be A) Cha-Cha [Alt Take].flac"
Run Code Online (Sandbox Code Playgroud)

我们想要匹配第三个字符串

songC = "(Used to Be A) Cha-Cha.flac"
Run Code Online (Sandbox Code Playgroud)

现在,尽管songA和SongB都与songC相匹配,但很明显songA是一个"更好"的匹配,因为它没有[Alt Take]部分,从而完美匹配.如果我使用该songA in songB方法两者都将匹配,那么我如何计算匹配的好坏?

python string match

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

Python 3从Web解析PDF

我试图从网页上获取PDF,进行解析,然后使用PyPDF2将结果打印到屏幕上。我使用以下代码正常工作:

with open("foo.pdf", "wb") as f:
    f.write(requests.get(buildurl(jornal, date, page)).content)
pdfFileObj = open('foo.pdf', "rb")
pdf_reader = PyPDF2.PdfFileReader(pdfFileObj)
page_obj = pdf_reader.getPage(0)
print(page_obj.extractText())
Run Code Online (Sandbox Code Playgroud)

只是写一个文件,尽管听起来很浪费,但我仍然可以读取它,所以我想我会这样切掉中间人:

pdf_reader = PyPDF2.PdfFileReader(requests.get(buildurl(jornal, date, page)).content)
page_obj = pdf_reader.getPage(0)
print(page_obj.extractText())
Run Code Online (Sandbox Code Playgroud)

但是,这给了我一个AttributeError: 'bytes' object has no attribute 'seek'。如何将requests直接来自PyPDF2 的PDF 送入?

python pdf python-requests pypdf2

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

Haskell打印阶乘

我刚开始编写Haskell编程,主要是因为我在寻找一种比C#更强大的数学语言,现在我很困惑.

现在我正试图简单地找到4的阶乘并打印出来,这是我到目前为止写的:

fact n = product [1..n]
main = do
   print fact 4
Run Code Online (Sandbox Code Playgroud)

当我尝试调试它时,我得到了

错误:(3,8)ghc:无法匹配期望的类型a1 -> t0' with actual typeIO()'函数print' is applied to two arguments, but its type(a0 - > a0) - > IO()'只有一个在'do'块的stmt中:print fact 4 In表达式:do {print fact 4}

我究竟做错了什么?

math haskell factorial

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

使用OpenMP循环时的线程安全

我正在使用C ++和GMP开发小型Collat​​z猜想计算器,并且正在尝试使用OpenMP在其上实现并行性,但是遇到了有关线程安全性的问题。就目前而言,尝试运行代码将产生以下结果:

*** Error in `./collatz': double free or corruption (fasttop): 0x0000000001140c40 ***
*** Error in `./collatz': double free or corruption (fasttop): 0x00007f4d200008c0 ***
[1]    28163 abort (core dumped)  ./collatz
Run Code Online (Sandbox Code Playgroud)

这是重现该行为的代码。

 #include <iostream>
 #include <gmpxx.h>

 mpz_class collatz(mpz_class n) {
     if (mpz_odd_p(n.get_mpz_t())) {
         n *= 3;
         n += 1;
     } else {
         n /= 2;
     }
     return n;
 }

 int main() {
     mpz_class x = 1;
 #pragma  omp parallel
     while (true) {
         //std::cout << x.get_str(10);
         while (true) {
             if …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading openmp gmp

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