标签: pseudocode

有没有更好的方法来计算文件中所有符号的频率?

好的,所以,我说有一个文本文件(不一定包含所有可能的符号),我想计算每个符号的频率,并且在计算频率之后,我需要从最常见的频率访问每个符号及其频率至少频繁.符号不一定是ASCII字符,它们可以是任意字节序列,尽管长度相同.

我正在考虑做这样的事情(伪代码):

function add_to_heap (symbol)
    freq = heap.find(symbol).frequency
    if (freq.exists? == true)
        freq++
    else
        symbol.freq = 1
        heap.insert(symbol)

MaxBinaryHeap heap
while somefile != EOF
    symbol = read_byte(somefile)
    heap.add_to_heap(symbol)
heap.sort_by_frequency()

while heap.root != empty
    root = heap.extract_root()
    do_stuff(root)
Run Code Online (Sandbox Code Playgroud)

我想知道:有没有更好,更简单的方法来计算和存储文件中每个符号出现的次数?

algorithm pseudocode binary-heap

6
推荐指数
1
解决办法
195
查看次数

伪代码检查.需要验证分配

我已经把它变成了这样你就不会帮我欺骗.只是想知道这看起来是否正确:

作业:输入员工姓名和工资清单,并确定平均(平均)工资以及平均值之上和之下的工资数量.

计划:允许输入姓名和工资计算平均排序值计算高于平均计数值低于平均值的值

//This program will allow a user to input an employee name and salary
//The output will contain the mean salary 
//as well as the number of salaries above and below the mean
//
//Arrays Used:
//Name(K) = Array for employee names
//Salary(K) = Array for salaries
//
//Variables Used:
//Mean = Mean of all employees Salaries
//UpMean = Number of Employees making more than the mean
//DwnMean = Number of Employees making less than the mean
//Sum …
Run Code Online (Sandbox Code Playgroud)

vb.net pseudocode

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

实施震动事件?

我正在为游戏制作一个gui API,一个请求的功能是震动事件.基本上是一个非常类似于Windows 7的Aero Shake的事件.当鼠标按下时,如果它快速地单向来回移动,则会触发该事件.我只是不确定什么类型的psedocode会进入这个?

c++ algorithm pseudocode

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

从两个变量计算任务的紧急程度

我试图找出一个公式来计算一组任意任务的紧急程度,根据"截止日期"之前的天数和任务已完成的完成百分比.

到目前为止,我有一个'函数',它给出了代表:

U = ((dd * 25) - (100 - cp))

Where: 
dd = Day difference from deadline to current date (in an integer value)
cp = current completion % (in an integer value - in increments of 5 currently)
Run Code Online (Sandbox Code Playgroud)

这给了我一个线性函数,函数中的25表示每天25%的任务进度.

所以在任何给定日期:

Where U <0 task is urgent
Where U =0 task is on schedule
Where U >0 task is ahead of schedule
(The actual display on if a task is on schedule (within a range) would be handled separately) …
Run Code Online (Sandbox Code Playgroud)

javascript mysql math statistics pseudocode

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

复合赋值运算符,如果修改了值(同时)会发生什么?

考虑以下伪代码(语言不可知):

int f(reference int y) {
   y++;

   return 2;
}

int v = 1;

v += f(v);
Run Code Online (Sandbox Code Playgroud)

当评估时功能f发生变化y(即v)时v += f(v),v"冻结" 的原始值是否变为v"丢失"?

v += f(v); // Compute the address of v (l-value)
           // Evaluate v (1)
           // Execute f(v), which returns 2
           // Store 1 + 2 
printf(v); // 3
Run Code Online (Sandbox Code Playgroud)

language-agnostic programming-languages pseudocode operators compound-assignment

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

如何检测是否存在重复模式

我的问题不是特定于语言的……我可能会用C#或Python来实现,除非语言的特定功能可以帮助我获得所需的信息。

有没有人知道的某种算法可以帮助我确定数字列表是否包含重复模式?

假设我有几个数字清单...

[12, 4, 5, 7, 1, 2]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 1, 1, 1, 1, 1]
[ 1, 2, 4, 12, 13, 1, 2, 4, 12, 13]
Run Code Online (Sandbox Code Playgroud)

我需要检测每个列表中是否有重复的模式...例如,列表1返回false,但是列表2、3和4返回true。

我在想也许要对列表中出现的每个值进行计数,如果val 1 == val 2 == val n ...那就可以了。还有更好的主意吗?

algorithm pseudocode pattern-matching

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

背包重复算法

我正在尝试为背包算法设计一个伪代码,其中可以多次选择单个项目。经典算法是

OPT(i, w) = max(OPT(i-1, w) or vi + OPT(i-1, w-wi))
Run Code Online (Sandbox Code Playgroud)

为了满足要求,我修改为

k=1;
max = maximum(OPT(i-1, w))
while(OPT(i-1, w - k*wi) > 0) {
  maximum = max(maximum, k*vi + OPT(i-1, w - k*wi))
  k++
}
OPT(i, w) = maximum
Run Code Online (Sandbox Code Playgroud)

这似乎是一个合适的解决方案吗?或者还有更好的解决方案吗?如果需要任何其他信息,请告诉我。其余保持不变,vi表示第i个元素的值,wi表示第i个元素的权重。

algorithm pseudocode knapsack-problem dynamic-programming

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

出队时将节点标记为在BFS上已访问

关于图上的BFS遍历只是一个简单而愚蠢的问题

我在许多站点上发现了BFS的伪代码几乎是这样的:

BFS (Graph, root):

create empty set S
create empty queue Q      
add root to S //mark as visited here
Q.enqueue(root)                      
while Q is not empty:
    current = Q.dequeue()
    if current is the goal:
        return current
    for each node n that is adjacent to current:
        if n is not in S:
            add n to S //mark as visited here
            Q.enqueue(n)
Run Code Online (Sandbox Code Playgroud)

我只是发现在给定节点出队列后将其标记为已访问,而不是在对一个给定节点进行标记时,将其标记为访问的节点稍微简单些,因为在后一种方法中,我们将需要编写一个额外的步骤。我知道这不是一件大事,但我想将一个节点标记为在一个地方而不是两个地方访问更有意义。更简洁,更容易记住甚至学习。

修改后的版本将如下所示:

BFS (Graph, root):

create empty set S
create empty queue Q      
Q.enqueue(root)                      
while Q is not empty: …
Run Code Online (Sandbox Code Playgroud)

graph-theory pseudocode breadth-first-search

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

从种子中产生闪电秘密

我正在尝试在以下部分中实现该功能:Per-commitment Secret Requirements

generate_from_seed(seed, I):
    P = seed
    for B in 47 down to 0:
        if B set in I:
            flip(B) in P
            P = SHA256(P)
    return P
Run Code Online (Sandbox Code Playgroud)

其中“flip(B)”替换值 P 中的第 B 个最低有效位。

根据这个定义,如果我们有seed=0x0101010101010101010101010101010101010101010101010101010101010101I=1,我希望结果是

>>> from hashlib import sha256
>>> from binascii import hexlify

>>> hexlify(sha256(int(("00000001"*31)+"00000000",2).to_bytes(length=32,byteorder="big")).digest())
b'79356295f56e69998b9140cb77c63d3d80c93874259793a38d1dbd8678809ca9'
Run Code Online (Sandbox Code Playgroud)

因为flip函数执行一次,将第 0 个 LSB(最右边的位)设置为 0。

相反,结果是(测试向量):

>>> hexlify(sha256(int("00000000"+("00000001"*31),2).to_bytes(length=32,byteorder="big")).digest())
b'915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c'
Run Code Online (Sandbox Code Playgroud)

查看一个实现,很明显人们正在使用:

output[lp / 8] ^= (1 << (lp % 8));
Run Code Online (Sandbox Code Playgroud)

这在我看来是错误的,因为它正在改变字节的 LSB,如果 …

pseudocode endianness bitwise-operators bitcoin lightning-network

5
推荐指数
0
解决办法
133
查看次数

这些元组可以以某种方式排列吗?

我正在编写一个程序,以将元组列表作为输入,并根据它们是否可以以某种方式排列来返回一些内容。通常我在编码之前就知道如何解决问题,但在这种情况下,我很难想出一个好的方法来解决这个问题。

这个想法是输入一个这样的列表.. [(5, 2), (3, 5), (3, 3), (1, 3)] 并验证是否可以以某种方式排列,以便最后number 匹配下一个元组的开头。所以在这种情况下是可能的,比如:[(1, 3), (3, 3), (3, 5), (5, 2)]。于是验证为真。元组也可以颠倒。

我想遍历列表并将有效的元组对组合在一起,但是如果它们没有以正确的方式分组以与其余的对一起工作怎么办?也可能太费时间了。

有任何想法吗?

谢谢!

python sorting tuples pseudocode data-structures

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