我正在尝试实现Heap定义的优先级队列,该算法来自CLRS第6章.伪代码如下所示:
Max_Heap_Insert(A, key):
A.heap_size = A.heap_size + 1
A[A.heap_size] = -?
Heap_Increase_Key(A, A.heap_size, key)
Run Code Online (Sandbox Code Playgroud)
我的问题是使用python,我该如何定义-∞?
我是Haskell世界的新来者,我想知道是否有这样的事情:
data IndexedList a = IList Int [a]
findIndex::(Int->Int)->IndexedList a->(a,IndexedList a)
findIndex f (IList x l) = (l!!(f x), IList (f x) l)
next::IndexedList a->(a,IndexedList a)
next x = findIndex (+1) x
Run Code Online (Sandbox Code Playgroud)
我注意到这种列表不是纯函数,而是对某些应用程序有用.它应该被视为有害吗?
谢谢,
短发
我只是想弄清楚这些结果背后的逻辑:
>>>nan = float('nan')
>>>nan == nan
False
# I understand that this is because the __eq__ method is defined this way
>>>nan in [nan]
True
# This is because the __contains__ method for list is defined to compare the identity first then the content?
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,我认为在幕后这个功能PyObject_RichCompareBool被称为正确?为什么会有区别?他们不应该有相同的行为吗?
假设我有以下代码:
type s = A of a | B of b
let foo (a:?) =
let bar (input:s) = match input with
| A a -> foo input
| B b -> ...
Run Code Online (Sandbox Code Playgroud)
我的问题是我应该在foo的签名中填写问号,这样我就不需要(冗余)匹配语句了吗?或者有一个击球手模式来做到这一点?
根据此演示文稿(http://oud.ocaml.org/2012/slides/oud2012-paper13-slides.pdf,PDF第4页),以下两种数据结构使用不同的内存量
type t1 = { f1: float; f2:float};;
type t2 = (float * float);;
Run Code Online (Sandbox Code Playgroud)
并且t1使用的内存少于t2,有人可以向我解释为什么会这样吗?
嗨,假设我有一个带ID的按钮,form-submit我的OCaml文件中有以下功能:
let () =
let form_submit = Dom_html.getElementById "form-submit" in
...
Run Code Online (Sandbox Code Playgroud)
向按钮添加on_click方法的正确方法是什么?我应该这样做吗?
form_submit##onclick <- ...
Run Code Online (Sandbox Code Playgroud)
但是处理函数的类型是什么?我在文档中找不到它.
假设我有一个Graph类和一个Vertex类,定义如下
Graph.py
class Graph:
def __init__(self):
self.adjacencyList = {}
def __str__(self):
return str(self.adjacencyList)
def addVetex(self,key,value):
if Vertex(key,value) not in self.adjacencyList:
self.adjacencyList[Vertex(key,value)] = []
Run Code Online (Sandbox Code Playgroud)
Vertex.py
class Vertex:
def __init__(self,key,value):
self.key = key
self.value = value
def __str__(self):
return "Key: ",str(self.key)," Value: ",str(self,value)
def __hash__(self):
return self.key
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
G = Graph()
G.addVetex(1,None)
G.addVetex(2,None)
G.addVetex(1,3)
print G
Run Code Online (Sandbox Code Playgroud)
打印出来{<Vertex.Vertex instance at 0x110295b90>: [], <Vertex.Vertex instance at 0x110295bd8>: []}但是我期待着类似的东西{"Key:1 Value:None":[]...}
我的问题是我做错了什么?当一个词被打印出来时,为什么它不会尝试调用其键/值的str函数?
谢谢.
嗨,我std::optional在这里看一下这里的一个实现,我发现这段代码片段让我困惑:
// workaround: std utility functions aren't constexpr yet
template <class T> inline constexpr T&& constexpr_forward(typename
std::remove_reference<T>::type& t) noexcept
{
return static_cast<T&&>(t);
}
Run Code Online (Sandbox Code Playgroud)
所以我不会通过以下方式理解这一点:
typename在这里做?只是声明以下部分是一种类型?std::remove_reference这里?我们没有在type&部件中添加参考吗?static_cast.template <class... Args>
constexpr storage_t( Args&&... args ) : value_(constexpr_forward<Args>(args)...) {},那么它对args做了什么?非常感谢.
正如这里 所说std::string的不是模板函数,而是标准选择使用函数重载来为不同类型提供此函数。我的问题是,在这种情况下,当模板/专业化似乎对我更有意义时,为什么要使用重载?考虑一下,如果标准定义了这样的东西:
template <typename T>
std::string std::to_string(const T& v);
Run Code Online (Sandbox Code Playgroud)
然后我们可以在我们的程序中自由地为任何类型添加特化以符合这个签名,因此 C++ 将有一种统一的方式将类型转换为人类可读的字符串。为什么不这样做?当前设计背后的想法是什么?
编辑1:
我对当前设计的主要批评std是不允许向 to 添加重载,因此我们不能编写任何类似的东西,std:to_string(object-that-is-of-user-defined-types)并且必须退回到to_string()在自己的命名空间中定义 a并记住在哪里使用他们的版本或std版本取决于他们正在处理的类型......这对我来说听起来很头疼。
我真正喜欢 Python(或其他一些语言)的一件事是,您可以通过实现一些魔术方法使自己的类型像本机类型一样工作。我认为这个问题的根本是为什么 C++ 决定禁止人们std::to_string()为他们自己的类型实现,从而禁止我们在任何地方都遵循相同的接口。
对于诸如hashor 之类的常见事物to_string(),在语言/stdlib级别上拥有单个界面,然后期望用户遵守该界面,而不是拥有多个界面,不是更好吗?
我试图解决H99中的一个问题:将列表分成两部分; 给出了第一部分的长度.
不要使用任何预定义的谓词.
例:
> (split '(a b c d e f g h i k) 3)
( (A B C) (D E F G H I K))
Run Code Online (Sandbox Code Playgroud)
我可以快速找到解决方案:
split'::[a]->Int->Int->[a]->[[a]]
split' [] _ _ _ = []
split' (x:xs) y z w = if y == z then [w,xs] else split' xs y (z+1) (w++[x])
split::[a]->Int->[[a]]
split x y = split' x y 0 []
Run Code Online (Sandbox Code Playgroud)
我的问题是我正在做的只是以递归格式重写循环版本.这是你在Haskell做事的正确方法吗?这不是命令式编程吗?
编辑:另外,你如何在这里避免额外的功能?