我遇到的一个编程问题涉及计算大数(最多10 ^ 5的数字)的阶乘.我见过一个简单的Haskell代码,就像这样
factorial :: (Eq x, Num x) => x -> x
factorial 0 = 1
factorial a = a * factorial (a - 1)
Run Code Online (Sandbox Code Playgroud)
即使没有代码中涉及的任何缓存,它也会隐式处理大量数字并以某种方式运行得更快.
当我尝试使用Java解决问题时,我不得不使用BigInteger来保存大数字并使用因子的迭代版本
public static BigInteger factorialIterative(int n)
{
if(n == 0 || n == 1) return BigInteger.valueOf(1);
BigInteger f = BigInteger.valueOf(1);
for(int i = 1 ; i <= n ;i++)
f = f.multiply(BigInteger.valueOf(i));
return f;
}
Run Code Online (Sandbox Code Playgroud)
上述代码超出了程序执行的设定时间限制.我也尝试了factorial的缓存递归版本
public static BigInteger factorial(int n)
{
if(cache[n] != null)
return cache[n];
else if(n == 0)
return new …
Run Code Online (Sandbox Code Playgroud) I am referring to the following python code
all(a==2 for a in my_list)
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码返回True,如果my_list中的所有元素都是2.但是当我将my_list设为空并将其作为
my_list = []
all(a==2 for a in my_list)
Run Code Online (Sandbox Code Playgroud)
它也返回True.我对这种行为感到困惑.是不是应该返回False,因为my_list中没有值为2的元素?
我试图以一种方式模拟urllib2.urlopen库,我应该为我传递给函数的不同URL获得不同的响应.
我现在在我的测试文件中这样做的方式是这样的
@patch(othermodule.urllib2.urlopen)
def mytest(self, mock_of_urllib2_urllopen):
a = Mock()
a.read.side_effect = ["response1", "response2"]
mock_of_urllib2_urlopen.return_value = a
othermodule.function_to_be_tested() #this is the function which uses urllib2.urlopen.read
Run Code Online (Sandbox Code Playgroud)
我希望othermodule.function_to_be_tested在第一次调用时获得值"response1",在第二次调用时获得"response2",这是side_effect将执行的操作
但是othermodule.function_to_be_tested()收到了
<MagicMock name='urlopen().read()' id='216621051472'>
Run Code Online (Sandbox Code Playgroud)
而不是实际的反应.请告诉我出错的地方或更简单的方法.
我遇到了两个动态编程问题.其中一个问题是
考虑到我可以一次跳1步,2步或3步,爬楼梯的步数是多少.
用于解决该问题的动态编程方法如下.
If C(n) is number of ways of climbing the staircase, then
C(n) = C(n-1) + C(n-2) + C(n-3) .
This is because , if we reach n-1 stairs, we can hop to n by 1 step hop or
if we reach n-2 stairs, we can hop to n by 2 step hop or
if we reach n-3 stairs, we can hop to n by 3 step hop
Run Code Online (Sandbox Code Playgroud)
正如我想要的那样,我理解了上述方法,我遇到了硬币变化问题
给出无限数量的25美分硬币,10美分硬币(硬币),5美分硬币(镍币)和1美分硬币,代表n美分的方式数量是多少
事实证明,这个问题的解决方案与上面的解决方案不同,并且有点复杂.即, C(n)= C(n-1)+ C(n-5)+ C(n-10)+ C(n-25)不成立.我仍然试图理解解决这个问题的方法.但我的问题是硬币变化问题与更简单的攀爬步骤问题有何不同?
我一直在尝试使用自签名证书配置在本地服务器上部署的网站,以用于开发目的。我希望我的网站工作的地址是example.company.local。使用SelfSSL生成证书后,将其复制到“个人”和“受信任的证书”根目录下的“本地计算机”存储中。但是,当我尝试访问URL时,出现错误“服务器的证书与URL不匹配”。尽管我可以忽略它,但我知道证书身份验证失败,并且我想解决此问题。我正在列出我遵循的步骤
selfssl /N:CN=example.company.local /V:9999
。这会将证书添加到我在本地计算机上的个人存储中请建议我要去哪里错了。我提供的网址是https://example.company.local
,但它指向本地主机证书。
我正在尝试解决动态编程问题,部分问题涉及查找一组'p'数字的排列数,这些数字将总和为数字'n'.p数集中的每个数字应该在0到n之间(包括0和n).
例如,如果n = 4且p = 3,则我有以下12个排列
{4,0,0},{0,4,0},{0,0,4}
{2,2,0},{2,0,2},{0,2,2}
{1,3,0},{1,0,3},{0,1,3}
{1,1,2},{1,2,1},{2,1,1}
Run Code Online (Sandbox Code Playgroud)
我开始使用这种DP方法.
n(i,j) represents number of ways of representing n using i,j in p positions
Run Code Online (Sandbox Code Playgroud)
我的基本情况是
n(i,0) = n(0,i) = p
Run Code Online (Sandbox Code Playgroud)
(例如,p = 3处的n(4,0)为3,即{4,0,0},{0,4,0},0,0,4}
递归案例
n(i,j) = n(i,j-1)*n(i-1,j)
Run Code Online (Sandbox Code Playgroud)
(例如:n(1,2)= n(1,1)*n(0,2)递归到n(1,0)*n(0,1)*2)
我不确定我是否正朝着正确的方向前进,因为上述方法并没有让我得到正确答案.请指导我正确的方向.
python ×2
algorithm ×1
factorial ×1
haskell ×1
iis-7 ×1
java ×1
mocking ×1
permutation ×1
python-2.7 ×1
recursion ×1
self-signed ×1
subset-sum ×1
unit-testing ×1