当前位置: 首页 > 工具软件 > Literate > 使用案例 >

literate program: Count Luck

潘哲
2023-12-01

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 .


完整内容,请看原文。


 类似资料:

相关阅读

相关文章

相关问答