The practice of programming


Chapter 3 Design and Implementation

In this section, we focus on one kind problem: generate random English text that reads well.

The solution is called 'The Markov Chain Algorithm', and its main idea is shown as follows:

  First we scan an article and save it in a special chart. For example, we use two adjacent words as prefix, by searching which we can get all the possible following words. We use 'hashtable', 'list', 'vector' to realize this function. The details are left out.

  Then we are to generate a paragraph based on the data above. The first several words are picked up randomly. And each time, we choose one of its suffix randomly. To ensure the random in just one scanning, we use a stractage, that is, 'use a variable "n" to count the current number of suffixes, use a string to save the previous chosen suffix. each time the present suffix can replace the previous suffix by the possibilition 1/n. When the scan is over, the suffix chosen is determined.'

  Last, to limit to total length of the generated paragraph, we add a symbol of ending to each prefix's map. Also, use a variable to count to total length we have generated.

The book introduce the solving programs in C, JAVA, C++, Awk, Perl. Their different realizations are all anchored in the same main idea. By comparison, we can found that C has the longest code lines and the highest efficience. Awk and Perl are the easiest. The quickest is C, then is Perl. Their adjustence is also different. If the number of prefix is changed, C, C++, Java is easy to modefy, while Perl and Awk are not.

最新文章

  1. 自用的基于Emit的C#下DataTable转实体类方法
  2. IPv6进阶
  3. (JS实现顾客商品浏览记录以及购物车)Cookie的保存与删除
  4. 安卓WebView中接口隐患与手机挂马利用(远程命令执行)
  5. Objc基础学习记录2
  6. PHP开发APP接口---返回数据的封装类
  7. 数据结构练习 00-自测4. Have Fun with Numbers (20)
  8. 一个简单的Verilog计数器模型
  9. poj 1715 Hexadecimal Numbers 排列组合
  10. Dojo的UI框架bootstrap for dojo和Dojo-Bootstrap简介
  11. Android实现多条Toast快速显示(强制中止上一条Toast的显示)
  12. June. 27th 2018, Week 26th. Wednesday
  13. 7.侧滑、ViewDragHelper、属性动画
  14. vue路由vue-router的使用
  15. php password_hash
  16. Python的特殊成员
  17. laravel 连表查询数据库
  18. linux一些命令的介绍
  19. mysql定位慢查询
  20. Entity Framework应用:根据实体的EntityState状态实现增删改查

热门文章

  1. 用MathType编辑带点星号的流程
  2. style标签进行实时编辑及修改css(转)
  3. Oracle之完全卸载
  4. iOS-setNeedsLayout等布局方法
  5. django用户认证系统——重置密码7
  6. iOS 导航栏rgb值与设置的有差异
  7. 通用且常用的Java正则匹配工具,用以检查邮箱名、电话号码、用户密码、邮政编码等合法性
  8. Oracle Schema Objects——Tables——Overview of Tables
  9. Powershell 脚本调用方法
  10. JavaScript 学习(3)核心对象