小编Dai*_*air的帖子

函数`(y*y)<x'应用于两个参数,但其类型`Bool'没有

所以我正在研究问题31.

我写了以下函数,希望确定一个数字是否为素数:

isPrime :: Integer -> Bool

isPrime x = prime x 2
            where
            prime :: Integer -> Integer -> Bool
            prime x y | ((y*y) < x) and ((x `mod` y) /= 0) = prime x (y+1)
                      | ((y*y) >= x) = True
                      | otherwise = False
Run Code Online (Sandbox Code Playgroud)

我的逻辑是创建一个isPrime函数,并在isPrime调用prime中存储一个函数来存储2个参数,我要检查的数字是否为prime(x)和迭代器来检查x的sqrt以下的所有数字并查看它们是否分开x.prime有3名警卫:

| ((y*y) < x) and ((x `mod` y) == 0) = prime x (y+1)
Run Code Online (Sandbox Code Playgroud)

这一行应该说:我传递的数字是否小于x(((y*y) < …

haskell

5
推荐指数
2
解决办法
3614
查看次数

当一个语句中有两个不同的错误时,Python 如何决定显示哪一个?

当我们在解释器中输入以下内容时:

10(b)
Run Code Online (Sandbox Code Playgroud)

我们得到一个未定义 b 的 NameError。但是,当我们尝试:

10(4)
Run Code Online (Sandbox Code Playgroud)

现在,我们得到了 int 不可调用的错误信息。为什么 Python 在上面的第一种情况下没有给出相同的错误,因为即使在那里, int 也是不可调用的?换句话说,在第一个片段中,我们有“int not callable”和“NameError”这两个错误。如果 Python 从左到右扫描,它首先会看到对 int (10) 执行的调用操作。所以它也应该为第一个给出“int not callable”错误。为什么没有?

我认为这可能是因为它首先评估括号内的表达式。但是,当我们尝试这样做时:

f(print("Hello"))
Run Code Online (Sandbox Code Playgroud)

现在,它说 NameError 没有定义 f。它甚至不打印“你好”。所以这表明我们对带括号的表达式求值的假设似乎不成立。

那么它究竟是如何工作的呢?

python-3.x

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

如何在Python中添加不均匀的子列表?

所以假设我有一个带有子列表的列表(用于可视化演示的对齐):

[[1,2,3,4],
[1,2,3],
[0,3,4]]
Run Code Online (Sandbox Code Playgroud)

我想将它们加在一起得到:

[2,7,10,4]
Run Code Online (Sandbox Code Playgroud)

最初,对于我正在处理的事情,我知道这些列表的上限,我正在考虑遍历每个子列表并添加0填充并使每个列表与上限一样长:

result+=[0]*(len(upper_bound)-len(list))
Run Code Online (Sandbox Code Playgroud)

然后使用:

result = [sum(x) for x in zip(list1,list2)
Run Code Online (Sandbox Code Playgroud)

得到这笔钱.但是上限变得非常大(比如10000+),列表的数量也很多(比如1000多个列表).

我的问题是:是否有一种更有效的方法来添加N个可能不均匀大小的子列表来给出结果列表,或者我是否要求太多?(我也不能使用任何花哨的数字库,如numpy)

python python-2.x

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

熊猫取代非零值

我知道我可以替换所有的nan值df.fillna(0)并替换单个值df.replace('-',1),但是如何用单个值替换所有非零值?

python pandas

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

我怎么不正确地计算e ^ x?

我试图估计e^x在Haskell中使用幂级数进行近似.

import Data.Function

-- Take two integers and divide them and return a float as a result.
-- So 1/2 would be 0.5
fd :: Int -> Int -> Double
fd = (/) `on` fromIntegral

-- Helper function to compute factorial
fact :: Int -> Int
fact 1 = 1
fact n = n * fact (n-1)

-- Calculate e^x using the power series for e^x (n is the number of
-- of terms used to approximate e^x …
Run Code Online (Sandbox Code Playgroud)

haskell

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

欧拉计划为什么说我得错了20号?

这是我要解决的问题:欧拉20.

n! 手段 n ? (n - 1) ? ... ? 3 ? 2 ? 1

例如,10! = 10 ? 9 ? ... ? 3 ? 2 ? 1 = 3628800和数字中的数字之和10!3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

找到数字中的数字总和 100!

我试过这个:

y = 1  #The actual number
sum = 0 #The sum of its digits.

def factorial(x): #Calculates the number using a recursive factorial algorithm …
Run Code Online (Sandbox Code Playgroud)

python

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

如何将Rust`Args`转换为argc和argv C等价物?

我正在使用需要的C API(特别是MPI_Init)int argc, char **argv.我正在尝试argc, argv使用以下代码生成等效代码:

let argc = std::env::args().len() as c_int;
let c_strs: ~[CString] = std::env:args().map(|s: & &str| s.to_c_str());
let mut argv: ~[*c_char] = c_strs.map(|c: &CString| c.with_ref(|ptr| ptr));
if null_terminate {
    argv.push(std::ptr::null());
}
Run Code Online (Sandbox Code Playgroud)

通过在Github上调整此讨论.

它失败了:

error: expected type, found `~`
src/lib.rs:37   let c_strs: ~[CString] = std::env::args().map(|s: & &str| s.to_c_str());
                            ^
Run Code Online (Sandbox Code Playgroud)

我摆脱了~它然后它找不到to_c_str(),并不确定要替换什么to_c_str,to_raw()(例如)失败.

有谁知道转换Args为更友好的C格式的方法?

c rust

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

>> =和concatMap之间的区别

>>=今天我一直在玩,试图了解monad,并找到了一个有趣的模式.使用列表monad时,>>=似乎表现得像concatMap.我四处寻找,试图找到任何相似之处,特别是在hackage的定义中.

我试过的一些事情:

[1, 2, 3] >>= (iter 5 id) => [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3]

concatMap (iter 5 id) [1, 2, 3]=> [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3]
Run Code Online (Sandbox Code Playgroud)

[1, 2, 3] >>= (iter 5 (+5)) => [1,6,11,16,21,2,7,12,17,22,3,8,13,18,23]

concatMap (iter 5 (+5) ) [1, 2, 3] => [1,6,11,16,21,2,7,12,17,22,3,8,13,18,23]

iter 只是非无限迭代,

iter i f a = toL $ Data.Sequence.iterateN i f a
  where
    toL = Data.Foldable.toList :: Data.Sequence.Seq a -> [a] 
Run Code Online (Sandbox Code Playgroud)

(在repl.it中工作所以进口被搞砸了).

(>>=)相当于concatMap对列表?这是概括concatMap吗?

monads haskell

3
推荐指数
2
解决办法
414
查看次数

有没有一种在Pytorch中创建随机位掩码的有效方法?

我想要一个具有指定百分比0s 的随机位掩码.我设计的功能是:

def create_mask(shape, rate):
    """
    The idea is, you take a random permutations of numbers. You then mod then
    mod it by the [number of entries in the bitmask] / [percent of 0s you
    want]. The number of zeros will be exactly the rate of zeros need. You
    can clamp the values for a bitmask.
    """
    mask = torch.randperm(reduce(operator.mul, shape, 1)).float().cuda()
    # Mod it by the percent to get an even dist of 0s.
    mask = torch.fmod(mask, …
Run Code Online (Sandbox Code Playgroud)

python pytorch

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

函数内部的非穷举模式

我目前正致力于解决问题62

我尝试了以下代码来解决它:

data Tree a = Empty | Branch a (Tree a) (Tree a)
              deriving (Show, Eq)

internals :: Tree a -> [a]

internals (Branch a Empty Empty) = []

internals (Branch a b c) = [a]++(internals b)++(internals c)

internals (Branch a b Empty) = [a]++(internals b)

internals (Branch a Empty c) = [a]++(internals c)
Run Code Online (Sandbox Code Playgroud)

基本上说:

  1. 如果两个子项都为空,则不在内部列表中包含该列表元素.
  2. 如果两个子节点都是非空的,那么node(a)是一个内部包含它,并继续检查以查看任何子节点a也是内部的.
  3. 如果其中一个子节点非空,则该节点是内部节点,并递归地检查子节点是否也是内部节点.

在GHCi中,我运行了以下内容:

> let tree4 = Branch 1 (Branch 2 Empty (Branch 4 Empty Empty)) (Branch 2 …
Run Code Online (Sandbox Code Playgroud)

haskell

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

标签 统计

haskell ×4

python ×4

c ×1

monads ×1

pandas ×1

python-2.x ×1

python-3.x ×1

pytorch ×1

rust ×1