我有一个 length 数组n,我想m从中随机选择元素并翻转它们的值。最有效的方法是什么?
有两种情况,m=1case是特殊情况。可以单独讨论, 和m=/=1。
我的尝试是:
import numpy as np
n = 20
m = 5
#generate an array a
a = np.random.randint(0,2,n)*2-1
#random choose `m` element and flip it.
for i in np.random.randint(0,n,m):
a[m]=-a[m]
Run Code Online (Sandbox Code Playgroud)
假设m有数十个和n数百个。
我搜索了这个网站,人们说你应该避免使用using namespace std.我完全同意.但是,怎么样using std::cin和using std::string?应该避免还是鼓励这样做?
我知道总是类型std::cin是最安全的选择,但是一次又一次地输入它们是非常繁琐的.
但是,当你using std::cin在文件的开头键入etc 时,它似乎非常人群.例如,这个简单的程序读取和计算学生成绩,在它面前,有太多 using std::,看起来很不舒服.
#include <iostream>
#include <ios>
#include <iomanip>
#include <stdexcept>
#include <vector>
using std::cin; using std::cout;
using std::istream; using std::vector;
using std::setprecision; using std::domain_error;
using std::string; using std::getline;
using std::streamsize;
istream& read_hw(istream& in, vector<double>& homework);
double grade(double mid_exam, double final_exam, \
const vector<double>& homework);
int main() {
std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " + name + "!" …Run Code Online (Sandbox Code Playgroud) 假设我想在这里下载数据:http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html
我想使用python自动执行此操作,我可以在其中指定日期等.
我在这里发现可以使用pandas pd.read_csv从网页上读取数据,但首先需要获得正确的网址.但在我的情况下,我不知道网址是什么.
此外,我还想自己指定日期和合同等.
在询问之前,我实际上尝试了开发工具,我仍然看不到网址,而且我不知道如何使其编程.
我有一组数据。它显然具有一定的周期性。我想通过使用傅里叶变换找出它的频率并将其绘制出来。
这是我的一个镜头,但看起来不太好。
这是相应的代码,我不知道为什么它失败:
import numpy
from pylab import *
from scipy.fftpack import fft,fftfreq
import matplotlib.pyplot as plt
dataset = numpy.genfromtxt(fname='data.txt',skip_header=1)
t = dataset[:,0]
signal = dataset[:,1]
npts=len(t)
FFT = abs(fft(signal))
freqs = fftfreq(npts, t[1]-t[0])
subplot(211)
plot(t[:npts], signal[:npts])
subplot(212)
plot(freqs,20*log10(FFT),',')
xlim(-10,10)
show()
Run Code Online (Sandbox Code Playgroud)
我的问题是:由于原始数据看起来非常周期性,并且我期望看到在频域中峰值非常尖锐;我怎样才能让峰看起来更漂亮?
我想从地图中找到第一个非零元素,因此我做了以下代码:
#include <map>
#include <iostream>
#include <algorithm>
bool nonzero(std::map<char,int>::const_iterator& it);
int main() {
std::map<char, int> m;
m['a'] = 0;
m['b'] = 1;
std::map<char,int>::iterator it = std::find_if(m.begin(), m.end(), nonzero);
std::cout << it->first << '\t' << it->second << std::endl;
return 0;
}
bool nonzero(std::map<char,int>::const_iterator& it) {
return it->second;
}
Run Code Online (Sandbox Code Playgroud)
g ++给出了非常复杂的错误,并说:
/usr/include/c++/5/bits/predefined_ops.h:234:30: error: invalid initialization of reference of type ‘std::_Rb_tree_const_iterator<std::pair<const char, int> >&’ from expression of type ‘std::pair<const char, int>’
{ return bool(_M_pred(*__it)); }
Run Code Online (Sandbox Code Playgroud)
我不明白它说的是什么以及为什么我的程序会失败.
考虑流动的例子:
q)\l sp.q
q)exec (qty;s) from sp
300 200 400 200 100 100 300 400 200 200 300 400
s1 s1 s1 s1 s4 s1 s2 s2 s3 s4 s4 s1
Run Code Online (Sandbox Code Playgroud)
我想以函数形式编写它,对我来说最明显的方法是:
q)?[sp;();();(`qty;`s)]
300 300 300 300 200 300 200 200 400 200 200 200
Run Code Online (Sandbox Code Playgroud)
但它不能给我正确的结果.结果对我来说不直观,为什么这种形式不起作用?
我要做:
q)value ?[sp;();();`qty`s!`qty`s]
300 200 400 200 100 100 300 400 200 200 300 400
s1 s1 s1 s1 s4 s1 s2 s2 s3 s4 s4 s4
Run Code Online (Sandbox Code Playgroud) 在服务器上考虑以下定义:
f:{show "Received ",string x; neg[.z.w] (`mycallback; x+1)}
Run Code Online (Sandbox Code Playgroud)
在客户端:
q)mycallback:{show "Returned ",string x;}
q)neg[h] (`f; 42)
q)"Returned 43"
Run Code Online (Sandbox Code Playgroud)
在q for motrtals,提示说:
执行异步消息传递时,请始终使用neg [.zw]来确保所有消息都是异步的.否则,当每个进程等待另一个进程时,您将遇到死锁.
因此我将服务器上的定义更改为:
f:{show "Received ",string x; .z.w (`mycallback; x+1)}
Run Code Online (Sandbox Code Playgroud)
一切顺利,我没有看到任何僵局.
任何人都可以给我一个例子来说明为什么我应该总是使用neg[.z.w]?
我听说我们应该尽可能地让变量变为本地变量,我同意.考虑以下代码:
int main() {
for(int i = 0; i<5; ++i) {
int temp;
std::cin >> temp;
std::cout << temp << std::endl;
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
temp是for循环的局部变量.但是,我担心temp会在每个循环中声明,因此使程序运行得更慢.避免这种情况并temp在for循环外声明是否会更好?
int main() {
int temp;
for(int i = 0; i<5; ++i) {
std::cin >> temp;
std::cout << temp << std::endl;
}
return 0
}
Run Code Online (Sandbox Code Playgroud) 我正在看这个视频.基本上它说使用dictionary(python的语言?)将计算从时间O(n ^ 2)到O(n)的fibonacci.
我编程了以下代码,fibo1应该是O(n)但实际上它运行得很慢.fibo2是正常的递归,它是O(n ^ 2)解决方案,但实际上它的运行速度比fibo1.我怎么能理解这个?
#include <iostream>
#include <map>
int fibo1(int i, std::map<int,int>& m);
int fibo2(int i);
int main()
{
std::map<int,int> m;
m[1] = 1; m[2] = 1;
int n = 40;
std::cout << fibo1(n,m);
//std::cout << fibo2(n);
return 0;
}
int fibo1(int i, std::map<int,int>& m) {
if(m[i]==0) {
return fibo1(i-1,m)+fibo1(i-2,m);
}
return m[i];
}
int fibo2(int i) {
if(i==1 || i==2) {
return 1;
}
return fibo2(i-1)+fibo2(i-2);
}
Run Code Online (Sandbox Code Playgroud) c++ ×4
kdb ×3
python ×3
algorithm ×1
arrays ×1
coding-style ×1
console ×1
csv ×1
dictionary ×1
display ×1
find ×1
html ×1
javascript ×1
numpy ×1
pandas ×1
performance ×1
python-2.7 ×1
scipy ×1
stl ×1
variables ×1