我有一个使用发生器计算毕达哥拉斯三元组的函数.但是,当我调用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) 我正在写一个彩票抽奖模拟程序作为一个项目.游戏的工作方式是你需要选择从49中抽取的6个数字才能获胜.
你获胜的机会是1/13,983,816,因为这是49个中有6个的组合.将demo program on Go playground 产生围绕循环下去,每次六个新的号码.
每次生成一组新的数字时,我都会测试它是否已经存在,如果确实存在,我就会突破循环.对于13,983,816种组合,您会认为在相同的6个数字重复之前需要很长时间,但在测试中它总是在10000次迭代之前失败.有谁知道为什么会这样?
我想第一次使用codeigniter生成条形码图像.我在这个网址中使用这个库:https: //github.com/desta88/Codeigniter-Barcode-Generator-Zend-Library
我遵循所有的指示.但我得到了错误.它告诉我zend lib不存在.
不存在的类:Zend
这是我的库文件夹结构:application - libraries - zend.php - zend(文件夹) - 条形码(文件夹) - barcode.php - exeception.php - loader.php - validate.php
这是我的控制器:
public function set_barcode($code) {
//load library
$this->load->library('zend');
//load in folder Zend
$this->zend->load('Zend/Barcode');
//generate barcode
Zend_Barcode::render('code128', 'image', array('text'=>$code), array());
}
Run Code Online (Sandbox Code Playgroud)
提前谢谢了.
ps:即时通讯使用codeigniter v 3.05 zend framework v 2.49
我是发电机的新手.当我用yield替换print时,为什么print语句的第一个正确函数不起作用(Python 2.7)
首先是正确的打印功能:
def make_all_pairs(list_):
pivot = list_.pop(0)
for el in list_:
pair = (pivot, el)
print pair
if len(list_) > 1:
make_all_pairs(list_)
make_all_pairs(["a","b","c","d","e"])
('a', 'b')
('a', 'c')
('a', 'd')
('a', 'e')
('b', 'c')
('b', 'd')
('b', 'e')
('c', 'd')
('c', 'e')
('d', 'e')
Run Code Online (Sandbox Code Playgroud)
然后发电机不给出所有组合
def make_all_pairs(list_):
pivot = list_.pop(0)
for el in list_:
pair = (pivot, el)
yield pair
if len(list_) > 1:
make_all_pairs(list_)
x = make_all_pairs(["a","b","c","d","e"])
for el in x:
print el …Run Code Online (Sandbox Code Playgroud) 我正在学习围棋的语言,并试图重写一些使用golang我的Python代码.我写了一个生成器函数,它逐行读取文本文件并发送(使用yield关键字)只有"有效"行(忽略空白行,重新构造未完成的行).
示例文件(myfile.txt):
#123= FOOBAR(1.,'text');
#126= BARBAZ('poeazpfodsp',
234,56);
Run Code Online (Sandbox Code Playgroud)
parse.py:
#!/usr/bin/python
def validlines(filename):
with open(filename) as fdin:
buff = ''
for line in fdin.readlines():
line = line.strip()
if line == '':
continue
buff += line
if line[-1] != ';':
continue
yield buff
buff = ''
fdin.close()
for line in validlines('myfile.txt'):
print(line)
Run Code Online (Sandbox Code Playgroud)
显示:
#123= FOOBAR(1.,'text');
#126= BARBAZ('poeazpfodsp',234,56);
Run Code Online (Sandbox Code Playgroud)
现在,我尝试使用golang中的闭包以相同的方式执行此操作:
parse.go:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func validLines(filename string) (func() (string, bool)) {
file, _ := os.Open(filename) …Run Code Online (Sandbox Code Playgroud) 我正在尝试为使用生成器的函数编写单元测试。下面是我的代码:
def extract_data(body):
for i in body:
a = re.sub('<[^<]+?>', '', str(i))
b = re.sub('view\xc2\xa0book\xc2\xa0info', '', str(a))
c = re.sub('key', '', str(b))
d = re.sub('\xc2', ' ', str(c))
e = re.sub('\xa0', '', str(d))
yield e
Run Code Online (Sandbox Code Playgroud)
我的单元测试代码:
def test_extract_data(self):
sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
expected_res = 'This Test Passes'
res = extract_data(sample_input)
self.assertEqual(expected_res, res)
Run Code Online (Sandbox Code Playgroud)
如果extract_data函数使用return而不是yield,则该测试顺利通过。如何编写生成器的测试?
我正在尝试实现一个可以生成无限数字的自然数生成器,我的代码:
def nature():
s = 0
while True:
yield s
s += 1
Run Code Online (Sandbox Code Playgroud)
当我使用时next(nature()),我得到一个0的序列,为什么这个?以及如何解决它?
>>> next(nature())
0
>>> next(nature())
0
>>> next(nature())
0
>>> next(nature())
0
Run Code Online (Sandbox Code Playgroud) 例如,我有一个列表:
L=[-13, -24, -21, -3, -23, -15, -14, -27, -13, -12]
Run Code Online (Sandbox Code Playgroud)
如果在%timeit -n 10 myList = [item for item in L if item < 15]
输出中键入10 loops, best of 3: 1.25 µs per loop
如果我输入myGen = (item for item in L if item < 15)
输出是1000000 loops, best of 3: 561 ns per loop
在情况2中,我不明白为什么生成器需要1000000个循环而不是10个循环?“ 3中最佳”是什么意思?我如何算出每个公共区域所需的总秒数,例如案例1的10 * 1.25 = 12.5 us?
我把一个生成器放在一起来计算Fibonacci数/序列.但是,它没有按照我的预期运作.我已经从python中"编译"了它,但我不知道我的JavaScript代码中的内容是什么不符合我的逻辑,就像python那样......任何人都可以给我一个暗示吗?
这是代码:
// Fibonacci generator
function* fibonacci() {
var a = 0;
var b = 1;
while (true) {
yield a;
a = b;
b = a + b;
}
}
// Instantiates the fibonacci generator
fib = fibonacci();
// gets first 10 numbers from the Fibonacci generator starting from 0
for (let i = 0; i < 10; i++) {
console.log(i + ' => ' + fib.next().value);
}Run Code Online (Sandbox Code Playgroud)
我认为这是一个可变范围的问题.我这样做是为了让这个工作:
// Fibonacci generator
function* fibonacci() {
var a = 0; …Run Code Online (Sandbox Code Playgroud)我已经编写了一个随机数生成器srand(),它创建了一个给定大小的随机数数组.我想我的随机数值取值高达1000.000并得到这个,我已经定义了数组的每个条目,如rand()%1000000下面的代码所示.奇怪的是,随机值都高达30.000左右,并且没有创建更大的随机数,如987.623,即随机数的数字数不超过5.有没有人知道为什么会发生这种情况?是否有其他方法(功能)可以提供比这些更大的随机数?
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <vector>
using namespace std;
int * rng(int size) {
int* a = NULL;
a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = rand() % 1000000;
if (a[i] == 0) {
a[i] += 1;
}
}
for (int j = 0; j < size; j++) {
cout << a[j] << " ";
}
delete[] a;
a = NULL; …Run Code Online (Sandbox Code Playgroud) generator ×10
python ×6
go ×2
random ×2
arrays ×1
barcode ×1
c++ ×1
codeigniter ×1
fibonacci ×1
function ×1
ipython ×1
javascript ×1
math ×1
numbers ×1
python-2.x ×1
time ×1
unit-testing ×1