小编Dud*_*ude的帖子

如何计算两个单词之间的"最短距离"?

最近我接受了采访,我被要求编写一个算法来查找从特定单词到给定单词的最小1个字母变化数,即Cat-> Cot-> Cog-> Dog

我不希望问题的解决方案只是指导我如何在此算法中使用BFS?

algorithm graph-theory data-structures

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

在快速排序中使用中位数选择?

我对Quicksort有一个小问题.在选择阵列的最小值或最大值的情况下,分区的枢轴值非常低效,因为阵列大小仅减小1.

但是,如果我添加选择该数组中值的代码,我认为Ii会更有效率.由于分区算法已经是O(N),因此它将给出O(N log N)算法.

可以这样做吗?

sorting algorithm quicksort selection

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

python中IF-ELSE块的缩进

嗨,我是python新手,我正在使用python工作NLP.我在写python中的if-else块时遇到错误.当我写作时,只有当时阻止它正常工作:

if xyzzy.endswith('l'):
    print xyzzy
Run Code Online (Sandbox Code Playgroud)

进入冒号后,我按下回车键,它会自动将我带到正确的缩进处.

但是当我在print语句后按"Enter"键后尝试添加else块时,它正在考虑它只是IF块的声明,所以它给了我不正确的缩进,因为我想要阻止之后,而当我我试图写别阻止我的自我它给了我这个错误.

else:  
   ^
Run Code Online (Sandbox Code Playgroud)

IndentationError: unexpected indent

那么在写完print语句后我该怎么办?输入显然不起作用,因为它正在向前移动光标,而当我使用空格来到正确的指针时它会给我一个错误.

python indentation

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

grep的管道不适用于尾巴?

我试图通过检查日志来调试一个场景,这是我的命令

tail -f eclipse.log | grep 'enimation' | grep  -i 'tap'
Run Code Online (Sandbox Code Playgroud)

基本上我想要的是,在所有的线条中,我在其中打印带有enimation的线条,然后在所有动画中,我想看到带有"tap"的动画.

以下是返回空结果的sammple数据

*******enimation error*********TapExpand
*******enimation error*********TapShrink
Run Code Online (Sandbox Code Playgroud)

这将返回空结果.

如果我运行此命令

 tail -f eclipse.log | grep  -i 'enimation.*tap'
Run Code Online (Sandbox Code Playgroud)

它返回正确的结果.有人可以向我解释一下,上述两个命令之间有什么区别,以及为什么结果会有差异.它们看起来都和我一模一样.

regex linux bash grep

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

如何在python中使用自定义类实现优先级队列?

我来自java背景,我需要这样的东西

public class Item implements Comparable<Item> {

    int score;
    ArrayList<Integer> arr;

    @Override
    public int compareTo(Item o2) {
        return score != o2.score ? score - o2.score : arr.size() - o2.arr.size();
    }


    public static void main(String[] args) {
        PriorityQueue<Item> p = new PriorityQueue<Item>();

    }
}
Run Code Online (Sandbox Code Playgroud)

所以我有一个有两个变量的类,分数和列表。也有自然排序的计算。

有人可以告诉我如何使用Python吗?heapq 对我不起作用,因为我的得分函数根据两个变量而不是一个变量检查​​得分。

python

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

使用邻接矩阵进行深度优先搜索

谁能解释使用邻接矩阵进行深度优先搜索的算法?我知道使用递归的深度优先搜索的算法,我尝试使用Adjacency矩阵实现它,但它不是很成功.

到目前为止我所拥有的是什么

 dfs(G,i){

  mark i as visited;

  for(traverse through the edges of i vertex){

     if(vertex of edge is unseen){

       DFS(G,newVerted)
     }
   }

}
Run Code Online (Sandbox Code Playgroud)

algorithm depth-first-search data-structures

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

问号在ruby中的函数定义中意味着什么?

我有这样的功能 -

函数名称为seld.is_dl,它接受路径参数.我的问题是这个?函数定义中的符号表示什么.

def self.is_dl?(path)

  path = File.basename(path)

  if path =~ /setup.exe/i

    return false

  else

    return true

  end

end
Run Code Online (Sandbox Code Playgroud)

我是java开发人员,我看过"?" 在主要是If-ELSE块的情况下,这就是为什么我无法确定这是什么意思?

ruby

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

正则表达式提取bash shell中引号中的所有内容?

考虑这个数据文件

随机文本"txt"随机文本
随机文本"txt1"随机文本"txt2"
随机文本"txt1"
随机文本"txt3"随机文本"txt1"
随机文本"txt4"随机文本"txt1"
随机文本" txt5" 随机文本" txt1"随机文本"txt5"随机文本"txt6"随机文本

对于每一行,我需要提取引号内的所有内容,IE

txt
txt1,txt2
txt1,txt3
txt1
,txt4 txt1 ,txt5
txt1,txt5,txt6一行中
可以有多个引号.

我在shell中编写了这个正则表达式(实际上我写了一个sed命令,但是当我在这里粘贴它时,它会搞砸了.*)

^ dotStar"[^"] +"dotStar $(单号引号)
^ dotStar"[^"] +"dotStar"[^"] +"dotStar $(如果有两个引号)

如您所见,我的正则表达式取决于出现的引号数量.任何人都可以给我一个通用的注册表,无论引用的次数如何,它都会给我一些文本.

regex bash shell sed

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

有人可以解释这段clojure代码吗?

我正在学习clojure,我在理解这个clojure代码时遇到了问题,所以我有这个部分功能

 (def add-five (partial + 5))
Run Code Online (Sandbox Code Playgroud)

我跑的时候

(add-five 2)# I get 7
(add-five 2 5) # I get 12
Run Code Online (Sandbox Code Playgroud)

首先,我提出一个论点; 第二,我提出两个论点.

(map add-five [1 2 3 4 5])
Run Code Online (Sandbox Code Playgroud)

这给了我

(6 7 8 9 10)
Run Code Online (Sandbox Code Playgroud)

在这里,我假设,add-five正在应用列表中的元素.但是当我跑的时候,

(reduce add-five [1 2 3 4 5])
Run Code Online (Sandbox Code Playgroud)

,我不知道发生了什么事?

 (reduce add-five [0]) #gives me zero
 (reduce add-five [0 0]) #gives me five
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下我运行如上所述的减少会发生什么?

clojure

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

每个二元关系BCNF如何?

因此,作为我作业的一部分,我必须证明与两个属性的任何关系都符合 BCNF。

根据我的理解,如果对于一个关系我们有第三范式并且一个非关键属性在功能上决定了关键属性,那么它就违反了 BCNF。

假设我的关系由两个属性 A1,A2 组成

场景1(只有一个函数依赖)

A1 -> A2 (so A1 is the key, and A2 does not FD A1 : so no violation)
Run Code Online (Sandbox Code Playgroud)

同样适用于

A2 -> A1
Run Code Online (Sandbox Code Playgroud)

但如果

A1->A2 and A2->A1
Run Code Online (Sandbox Code Playgroud)

这里的key可以是A1,A2。另一个非关键属性在功能上决定了关键。

database-normalization functional-dependencies bcnf

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