所以我正在研究问题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) < …
当我们在解释器中输入以下内容时:
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。它甚至不打印“你好”。所以这表明我们对带括号的表达式求值的假设似乎不成立。
那么它究竟是如何工作的呢?
所以假设我有一个带有子列表的列表(用于可视化演示的对齐):
[[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)
我知道我可以替换所有的nan值df.fillna(0)并替换单个值df.replace('-',1),但是如何用单个值替换所有非零值?
我试图估计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) 这是我要解决的问题:欧拉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) 我正在使用需要的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)
它失败了:
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格式的方法?
>>=今天我一直在玩,试图了解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吗?
我想要一个具有指定百分比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) 我目前正致力于解决问题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)
基本上说:
a)是一个内部包含它,并继续检查以查看任何子节点a也是内部的.在GHCi中,我运行了以下内容:
> let tree4 = Branch 1 (Branch 2 Empty (Branch 4 Empty Empty)) (Branch 2 …Run Code Online (Sandbox Code Playgroud)