我正在学习Cormen和Co.的算法,我从他们的伪代码实现合并排序有问题.我编译它:
$ gcc -Wall -g merge_sort.c
Run Code Online (Sandbox Code Playgroud)
我有一个问题因为数字:
2 4 5 7 1 2 3 6
Run Code Online (Sandbox Code Playgroud)
结果是:
1 2 2 3 3 4 5 5
Run Code Online (Sandbox Code Playgroud)
我试着仔细阅读伪代码,但这对我没有帮助.我想知道我做错了什么.以下是我的代码:
#include <stdio.h>
#define SIZE 8
void merge(int *array_of_integers, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int i, j, k;
int left_array[n1 + 1];
int right_array[n2 + 1];
for (i = 0; i < n1; i++)
left_array[i] = array_of_integers[p + i];
for …Run Code Online (Sandbox Code Playgroud) My Rails应用程序使用两个数据库,第一个是postgresql,第二个是-sqlite,最后一个是测试环境.我使用了Devise gem,它使用inet数据类型来提供有关IP的信息.它适用于postgresql,但是为sqlite提供了一些错误.我已经安装了一个gem postgres_ext并将其添加到Gemfile中,但它没有解决问题.下面是一段日志:
$ RAILS_ENV=test rake db:setup
(in /user/project)
db/test.sqlite3 already exists
-- enable_extension("plpgsql")
-> 0.0030s
-- create_table("comments", {:force=>:cascade})
-> 0.3368s
-- add_index("comments", ["commentable_id"],
{:name=>"index_comments_on_commentable_id", :using=>:btree})
-> 0.1680s
-- add_index("comments", ["commentable_type"], {:name=>"index_comments_on_commentable_type", :using=>:btree})
-> 0.1898s
-- add_index("comments", ["user_id"], {:name=>"index_comments_on_user_id", :using=>:btree})
-> 0.1568s
-- create_table("contacts", {:force=>:cascade})
-> 0.2803s
-- create_table("delayed_jobs", {:force=>:cascade})
-> 0.2818s
-- add_index("delayed_jobs", ["priority", "run_at"], {:name=>"delayed_jobs_priority", :using=>:btree})
-> 0.1677s
-- create_table("exercises", {:force=>:cascade})
-> 0.3360s
-- create_table("languages", {:force=>:cascade})
-> 0.3025s
-- create_table("levels", {:force=>:cascade})
-> 0.2692s
-- create_table("solutions", {:force=>:cascade})
-> …Run Code Online (Sandbox Code Playgroud) 我们是否必须在函数声明中使用模式匹配中的括号?
在下面的示例中,我有一个模式x:xs,其中x从列表中获取第一个元素并xs包含其余元素.
我想问一下括号是否是这种模式匹配的必要部分.
head' :: [a] -> a
head' [] = error "Can't call head on an empty list!"
head' (x:_) = x
Run Code Online (Sandbox Code Playgroud)
我尝试使用它没有大括号,但它在加载到ghci期间导致错误.
我正在学习Haskell,我试图自己实现这个replicate功能,下面是我工作的结果:
replicate' :: Enum a => a -> b -> [b]
replicate' a b = [b | _ <- [1..a]]
Run Code Online (Sandbox Code Playgroud)
但在加载脚本到ghci期间我得到了msg:
proginhaskell.hs:152:29:
Could not deduce (Num a) arising from the literal `1'
from the context (Enum a)
bound by the type signature for
replicate' :: Enum a => a -> b -> [b]
at proginhaskell.hs:152:1-34
Possible fix:
add (Num a) to the context of
the type signature for replicate' :: Enum a => a -> b -> [b] …Run Code Online (Sandbox Code Playgroud) ember-cli为rails中的这些添加了类似的生成器,我想问一下这两个库/框架之间是否有任何区别.