literate program: Count Luck

http://cs.mcgill.ca/~vfoley1/literate/count-luck.html


literate program 是什么鬼!!!谁能指导下小编。自从进入了Rust社区,为什么小编觉得每天都有学不完的新知识!!!


不管怎样,觉得写得不错,就摘来了。


1 Preface

This file is a literate program solving the Count Luck problem from HackerRank. The solution is written using Org mode and Rust.

  • The PDF version of this program is available here

  • The Org mode version of this program is available here

  • The Rust version of this program is available here

I have known about literate programming for a few years, but recent events have made me want to look at it again: Howard Abrams posted a video on Literate Devops, Donald Knuth gave a keynote at UseR! on literate programming, John D. Cook wrote a blog post on literate programming, and I saw some people mention it in discussions on Hacker News and Lobsters. This has made me want to gain a deeper insight about the subject: the main goal of this program (essay?) is as a vehicle to explore this style of programming, to get some practical experience, and to understand some of the pros and cons.

I must mention that this is only my second attempt at writing a literate program, and as a reader, you will notice that the style of both the prose and the code is a little bit awkward. In addition, the structure of the document is most likely going to be biased towards a regular programming style.

With that caveat out of the way, let’s look at the problem.

2 Overview

(I assume that you’ve skimmed the problem at this point.) The problem is set in the Harry Potter universe: Hermione and Ron are in the woods, and they must make their way to a portkey. They can only move up, down, left, or right (no diagonal moves) and they cannot move through trees, nor off the map. I have replicated a sample scene below. This scene is one of the inputs provided to our program: the X’s represent trees, the periods are the ground that Hermione and Ron can walk on, the M is their starting location and the asterisk is the portkey.

.X.X......X
.X*.X.XXX.X
.XX.X.XM...
......XXXX.

Hermione and Ron walk along the road until they reach a fork: at that point, Hermione zaps her wand to indicate which path they should pick to reach the portkey. In the example above, she will zap her wand 3 times. The diagram below shows why: when Hermione and Ron are at (2,9), they can either go up to (1,9) or right to (2,10); Hermione zaps her wand and it indicates that they should go to (1,9), and that is the first usage. (Note that only the unexplored roads were considered: going back to (2,8) was not an option, otherwise Hermione would need to zap her wand at every step.) The second and third zaps of the wand occur at the points (0,5) and (3,3).

    0 1 2 3 4 5 6 7 8 9 10
0   . X . X .←.←.←.←.←. X
              ↓       ↑
1   . X *←. X . X X X . X
          ↑   ↓       ↑
2   . X X . X . X M→.→.→.
          ↑   ↓
3   . . .←.←.←. X X X X .


完整内容,请看原文。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值