`
buliedian
  • 浏览: 1259193 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

伪代码(Pseudocode)

阅读更多

伪代码(Pseudocode)

原文:《Pseudocode 》by Naomi Nishimura
刘建文略译(http://blog.csdn.net/keminlau

In the text and lectures, algorithms will often be expressed in pseudocode, a mixture of code and English (for specific not necessarily good examples of particular pseudocodes, see p. 17 of the course text, or the examples in the books The Design and Analysis of Computer Algorithms by Aho, Hopcroft, and Ullman, Addison-Wesley, 1974, Computer Algorithms: Introduction to Design and Analysis by Baase, 1978, and Fundamentals of Computer Algorithms by Horowitz and Sahni, Computer Science Press, 1984). While understanding pseudocode is usually not difficult, writing it can be a challenge.

伪代码是英语与代码的混合物,理解伪代码不难,但是编写伪代码则很具有挑战性。

Why use pseudocode at all? Pseudocode strikes a sometimes precarious balance between the understandability and informality of English and the precision of code. If we write an algorithm in English, the description may be at so high a level that it is difficult to analyze the algorithm and to transform it into code.

If instead we write the algorithm in code, we have invested a lot of time in determining the details of an algorithm we may not choose to implement (as we typically wish to analyze algorithms BEFORE deciding which one to implement). The goal of writing pseudocode, then, is to provide a high-level description of an algorithm which facilitates analysis and eventual coding (should it be deemed to be a “good” algorithm) but at the same time suppresses many of the details that vanish with asymptotic notation. Finding the right level in the tradeoff between readability and precision can be tricky……

为什么一定要使用伪代码?(因为)伪代码平衡了英语 (自然语言)的易理解性和非形式所带来的模糊性与代码的精确性之的矛盾。如果我们使用英语(自然语言)编写算法,那么算法描述可能太过高层而很难对算法进 行分析并转化成实际代码;如果我们用代码编写算法,我们得花大量的时间在一些我们可能最终不会实现的代码细节上(缘于我们一般在实现算法之前看对算法逻辑 进行分析)。编写伪代码(以一个适当的表述高度描述算法)的目的是为了便于算法分析并最终实现编码,但同时抑制住在渐近记法(asymptotic notation)被抹去的大量细节。在可读性与精确性之间找到一个适当的平衡点是相当的困难(tricky:问题难以处理,需要莫大的心力)的……

Just as a proof is written with a type of reader in mind (hence proofs in undergraduate textbooks tend to have more details than those in journal papers), algorithms written for different audiences may be written at different levels of detail. In assignments and exams for the course, you need to demonstrate your knowledge without obscuring使变模糊 the big picture with unneeded detail. Here are a few general guidelines for checking your pseudocode:

好比一个(数学)证明有预定的读者类型一样,面对不同的读者,算法的编写详细程度也不同。以下是指导伪代码编写的参考原则:
1. Mimic模仿 good code and good English. Using aspects of both systems means adhering to the style rules of both to some degree. It is still important that variable names be mnemonic, comments be included where useful, and English phrases be comprehensible (full sentences are usually not necessary).

仿照好代码或好英文(的表述方式)。意思是说在某种程度上吸收二者在表述上的优点。比如,使用易记的变量名、适当的使用注释和使用好理解的英语短语等。 2. Ignore unnecessary details. If you are worrying about the placement of commas, you are using too much detail. It is a good idea to use some convention to group statements (begin/end, brackets, or whatever else is clear), but you shouldn't obsess about syntax.

忽略不必要的细节。如果你频繁地使用逗号,意味着你写得太细了。使用begin/end或括号给组合多条语句是不错的主意,不过不能被语法分心。
3. Don't belabour无必要的解释 the obvious. In many cases, the type of a variable is clear from context; unless it is critical that it is specified to be an integer or real, it is often unnecessary to make it explicit.

不要对很显然的东西作解释。很多时候,变量的类型在上下文中是很明显的,除非它的特定类型(比如是整数或实数)是至关重要,不然没必要显式说明它的类型。

4. Take advantage of programming shorthands. Using if-then-else or looping structures is more concise than writing out the equivalent in English; general constructs that are not peculiar to a small number of languages are good candidates for use in pseudocode. Using parameters in specifying procedures is concise, clear, and accurate, and hence should not be omitted from pseudocode.

使用编程的短记(shorthands)工具。使用if-then-else 或循环结构比用英语表达更简洁;一些通用的语言构造也可在伪代码中使用(FIXME)。给函数指定参数是简洁、明了和准确的,不应该在伪代码中去除。

5. Consider the context. If you are writing an algorithm for quicksort, the statement "use quicksort to sort the values" is hiding too much detail; if we have already studied quicksort in class and later use it as a subroutine in another algorithm, the statement would be appropriate to use.

考虑上下文。如果你正为一个快速排序写一个算法,那么语句“use quicksort to sort the value”就隐藏了太多的细节了;如果你已经学习过快速排序算法,而现在把它作为一个另一个算法的子过程,那么可以用那句语句来表述。

6. Don't lose sight of the underlying model. It should be possible to “see through” your pseudocode to the model below; if not (that is, you are not able to analyze the algorithm easily), it is written at too high a level.

不迷惑于算法底下的模型。看透你的伪代码底下的模型是可能的,如是不行,证明编写的太高层次了(不够详细),那样你很难分析算法。

7. Check for balance. If the pseudocode is hard for a person to read or difficult to translate into working code (or worse yet, both!), then something is wrong with the level of detail you have chosen to use.

检验(可读性与精确性)平衡性。如果伪代码很难读懂或很难翻译成源代码,那么你的伪代码编写的详细程度必定在什么地方出了问题了。

(author: Naomi Nishimura)

Kemin said:
b.弱智的人玩IDE,聪明的人玩编程语言,智慧的人玩伪代码,当然还有更高的级别,想知道?去问上帝吧。
Kemin said:
b. 看来伪代码是亲自然语言多一点,还是亲源代码多一点是没准的,也没必须太准,太受限,因为只要代码读者能花点时间进入状态,进入问题的上下文,那即便是很 “烦人”很magic的源代码也看得懂,不然自然语言并且有人讲解也听不懂看不明。所以伪代码的抽象level去到哪不是一成不变的,因人而异。

参考

分享到:
评论

相关推荐

    pseudocode.js:Web的漂亮伪代码

    pseudocode.js是一个JavaScript库,可以很好地将伪代码类型化为HTML。 直观的语法: Pseudocode.js采用LaTeX样式的输入,该输入支持LaTeX的算法包中的算法构造。 无论有没有LaTeX经验,用户都应该觉得语法很直观。...

    docsify-pseudocode:在 docsify 中渲染伪代码的插件

    文档化伪代码这是什么docsify-伪代码是基于插件呈现在docsify伪代码。 使用它,你可以轻松地在 markdown 文档中编写类似 Latex 的算法包的伪代码。特征所有功能都来自pseudocode.js。 直观的语法: docsify-...

    伪代码的使用规范 Usage of Pseudocode(清晰文字版pdf)

    ### 伪代码的使用规范详解 #### 一、伪代码定义及目的 **伪代码**是一种介于自然语言和编程语言之间的算法描述语言。它的主要目的是为了方便算法的设计与理解,使得描述的算法能更容易地转换成实际的编程语言如...

    伪代码生成器PseudoCodeTool

    适用于java/C/C++等代码,源代码为java代码通过运行代码将代码输入程序框中D盘自动生成伪代码文件PseudoCode.txt。(注意:由于是简单代码只可适用于糊弄老师,实验报告等,不可用于学术研究)。

    Atom-language-pseudocode,一个atom包,它帮助软件工程师编写伪代码,以简单、干净的方式解释他们的算法。.zip

    Atom-language-pseudocode 是一个专为 Atom 文本编辑器设计的插件,旨在方便软件工程师以伪代码的形式清晰地表达他们的算法。这个插件的核心功能是提供了一个强大的环境,允许开发者在编写实际代码之前,先用简洁...

    伪代码的使用规范_Usage_of_Pseudocode_202109051415461.docx

    伪代码(Pseudocode)是一种算法描述语言,旨在以结构清晰、代码简单、可读性好的方式描述算法,使其可以容易地以任何一种编程语言(如Pascal、C、Java等)实现。 语法规则: 1. 每一条指令占一行,指令后不跟任何...

    伪代码的使用 (txt)

    介绍伪代码的使用 Usage of Pseudocode

    latex Algorithms伪代码规范

    这些实例对于理解LaTeX算法伪代码规范来说是十分有益的,通过学习和模仿这些例子,用户可以快速掌握如何在自己的LaTeX文档中插入和自定义算法伪代码。 此外,文档中还提及了算法描述的版本信息,这有助于用户了解该...

    generic-pseudocode-vscode:通用伪代码语法突出显示的简单扩展

    一个简单的扩展,用于突出显示通用伪代码的语法。 常见的伪代码关键字的多个变体都具有语法突出显示功能,使您可以使用自己的样式,而不必将其限制为特定格式。 目录: 特征 语法高亮 以下各项具有语法突出显示。 ...

    Pseudocode_to_Python:这是python的伪代码

    **伪代码到Python的转换详解** 在编程领域,伪代码是一种非正式的、近似人类语言的表达方式,用于描述算法或程序设计思路。它不拘泥于特定编程语言的语法,而是注重逻辑清晰和易懂。对于初学者来说,先用伪代码规划...

    This repository contains the pseudocode(pdf) of various al.zip

    标题中的“This repository contains the pseudocode(pdf) of various al.zip”表明这是一个包含各种算法伪代码的PDF文件集合,被压缩成一个名为“al.zip”的文件。描述中的信息简洁,同样指出这个资源是关于不同...

    caie-pseudocode:剑桥伪代码规范的解释器

    CAIE伪代码解释器 剑桥伪代码规范的解释器。 尚未准备好使用。 开发几乎每天都在和上进行流式传输 使用,安装cargo和rust编译器,并运行cargo run example_src2.txt 项目完成后,安装和使用将变得更加容易

    PseudoCode_Anagrams_

    标题 "PseudoCode_Anagrams_" 暗示我们即将探讨的是使用伪代码解决与字母异位词(Anagrams)相关的编程问题。字母异位词是指两个单词由相同的字符组成,但字符顺序不同。例如,“listen”和“silent”就是一对字母异...

    psudo:伪代码可视化

    伪代码可视化 测试编译器:运行 psudo.py。 我在bubble.txt 中使用伪代码编写了一个冒泡排序算法,它将由psudo.py 加载。 测试web前端部分:在浏览器中打开psudo/visualize/release1.1/PseudoCode.html(Chrome除外...

    STK8312_PseudoCode.rar_STK8312 Sensors_stk8312

    在提供的"STK8312_PseudoCode.rar"压缩包中,包含了一个名为"STK8312_PseudoCode.c"的源代码文件,这很可能是用来驱动STK8312传感器的伪代码或示例代码。此代码可能涵盖了以下关键知识点: 1. **初始化配置**:在...

    算法伪代码:此存储库包含面试准备和竞争编码所需的各种算法和数据结构的伪代码(pdf)

    在编程中,算法的伪代码描述了步骤的逻辑,但不涉及具体的编程语法。常见的算法包括排序(如冒泡排序、快速排序)、搜索(如二分查找)、图算法(如Dijkstra算法、Floyd-Warshall算法)和动态规划等。 2. **数据...

    Introduction to Programming in C++ flowchart and pseudocode.pdf

    ### 关于C++编程中的流程图与伪代码 在计算机科学与编程领域中,了解算法的设计、流程图的绘制及伪代码的编写是至关重要的基础技能。本文将基于提供的文件信息,详细介绍流程图的标准画法及其与伪代码的关系,并...

    regular-impressions:使用类似于伪代码的语言编写正则表达式

    使用类伪代码语言编写正则表达式的简单工具。 演示: : 例子: 目录 如何使用 建议命令每行写一个命令,除非命令是嵌套的。 符号和标点符号不需要转义。 使用换行符连接多个命令。 制表符和空格可用于缩进以...

    PPO

    **PPO算法详解** PPO(Proximal Policy Optimization)是一种在强化学习中广泛使用的策略优化算法,由OpenAI在2017年的研究论文中提出。它在保证策略更新的稳定性的同时,最大化了每一步的策略改进。...

    Python-To-AQA-Pseudocode:一个用python编写的简单的散列在一起的转换器,用于将python程序转换为AQA Computer Science Psudocode建议的语法

    Python到AQA伪代码 一个用python编写的简单的散列在一起的转换器,用于将python程序转换为AQA Computer Science伪代码推荐的语法。 这是最初为我自己的程序编写的,因此某些部分可能无法正常工作,请随时进行派生和...

Global site tag (gtag.js) - Google Analytics