From 4a32c58221e6f1f5b885c09f60340911068c2b9d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 17 Jan 2010 15:58:44 -0500 Subject: [PATCH] added bentley's chapter from beautiful code to the examples/tests -- quicksort runtime analysis --- examples/beautiful_code/quicksort.coffee | 13 +++++++++++++ .../regular_expression_matcher.coffee | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 examples/beautiful_code/quicksort.coffee diff --git a/examples/beautiful_code/quicksort.coffee b/examples/beautiful_code/quicksort.coffee new file mode 100644 index 00000000..bbba0504 --- /dev/null +++ b/examples/beautiful_code/quicksort.coffee @@ -0,0 +1,13 @@ +# Beautiful Code, Chapter 3. +# Produces the expected runtime of Quicksort, for every integer from 1 to N. + +runtime: N => + [sum, t]: [0, 0] + for n in [1..N] + sum += 2 * t + t: n - 1 + sum / n + t + +print(runtime(3) is 2.6666666666666665) +print(runtime(5) is 7.4) +print(runtime(8) is 16.92142857142857) diff --git a/examples/beautiful_code/regular_expression_matcher.coffee b/examples/beautiful_code/regular_expression_matcher.coffee index d36f1d30..da6bd696 100644 --- a/examples/beautiful_code/regular_expression_matcher.coffee +++ b/examples/beautiful_code/regular_expression_matcher.coffee @@ -1,5 +1,5 @@ -# Beautiful Code, chapter 1. -# Implements a regular expression matcher that supports characters, +# Beautiful Code, Chapter 1. +# Implements a regular expression matcher that supports character matches, # '.', '^', '$', and '*'. # Search for the regexp anywhere in the text.