forked from Sophrinix/rubyquiz-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rss
175 lines (175 loc) · 33.6 KB
/
index.rss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Ruby Quiz</title>
<link>http://www.rubyquiz.com/</link>
<description>The source for weekly Ruby programming challenges.</description>
<item>
<title>Internal Rate of Return (#156)</title>
<link>http://www.rubyquiz.com/quiz156.html</link>
<description><p>by Harrison Reiser</p>
<p>Internal Rate of Return (IRR – http://en.wikipedia.org/wiki/Internal_rate_of_return) is a common financial metric, used by investment firms to predict the profitability of a company or project. Finding the IRR of a company amounts to solving for it in the equation for Net Present Value (NPV – http://en.wikipedia.org/wiki/Net_present_value), another valuable decision-making metric:</p>
<p class="example"> N C_t<br />NPV = Σ ------------<br /> t=0 (1 + IRR)**t</p>
<p>This week's quiz is to calculate the IRR for any given variable-length list of numbers, which represent yearly cash flows, the C_t's in the formula above: C_0, C_1, etc. (C_0 is typically a negative value, corresponding to the initial investment into the project.) From the example in the Wikipedia article (http://en.wikipedia.org/wiki/Internal_rate_of_return), for instance, you should be able to produce a rate of 17.09% (to four decimal places, let's say) from this or a similar command:</p>
<div class="code"><span class="type">ruby</span><div class="body"> irr([-100,+30,+35,+40,+45])<br /> =&gt; 0.1709...<br /><br /></div></div>
<p>Keep in mind that an IRR greater than 100% is possible. Extra credit if you can also correctly handle input that produces negative rates, disregarding the fact that they make no sense.</p></description>
<pubDate>Fri, 08 Feb 2008 08:01:18 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz156.html</guid>
</item>
<item>
<title>Parsing JSON (#155)</title>
<link>http://www.rubyquiz.com/quiz155.html</link>
<description><p>There has been a lot of talk recently about parsing with Ruby. We're seeing some parser generator libraries pop up that make the task that much easier and they've been stirring up interest.</p>
<p>In honor of that, this week's Ruby Quiz is to write a parser for JSON.</p>
<p>JSON turns out to turns out to be a great little example for writing parsers for two reasons. First, it's pretty easy stuff. You can hand-roll a JSON parser in under 100 lines of Ruby. The second advantage is that the data format is wonderfully documented:</p>
<p><a href="http://json.org/">JSON</a></p>
<p>Since JSON is just a data format and Ruby supports all of the data types, I vote we just use Ruby itself as the abstract syntax tree produced by the parse.</p>
<p>Feel free to show off your favorite parser generator, if you don't want to roll your own. Anything goes.</p>
<p>Here are a few tests to get you started:</p>
<div class="code"><span class="type">ruby</span><div class="body"> require <span class="string">"test/unit"</span><br /><br /> <span class="keyword">class</span> TestJSONParser &lt; Test::Unit::TestCase<br /> <span class="keyword">def</span> setup<br /> <span class="variable">@parser</span> = JSONParser.new<br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_keyword_parsing<br /> assert_equal(<span class="keyword">true</span>, <span class="variable">@parser</span>.parse(<span class="string">"true"</span>))<br /> assert_equal(<span class="keyword">false</span>, <span class="variable">@parser</span>.parse(<span class="string">"false"</span>))<br /> assert_equal(<span class="keyword">nil</span>, <span class="variable">@parser</span>.parse(<span class="string">"null"</span>))<br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_number_parsing<br /> assert_equal(42, <span class="variable">@parser</span>.parse(<span class="string">"42"</span>))<br /> assert_equal(-13, <span class="variable">@parser</span>.parse(<span class="string">"-13"</span>))<br /> assert_equal(3.1415, <span class="variable">@parser</span>.parse(<span class="string">"3.1415"</span>))<br /> assert_equal(-0.01, <span class="variable">@parser</span>.parse(<span class="string">"-0.01"</span>))<br /><br /> assert_equal(0.2e1, <span class="variable">@parser</span>.parse(<span class="string">"0.2e1"</span>))<br /> assert_equal(0.2e+1, <span class="variable">@parser</span>.parse(<span class="string">"0.2e+1"</span>))<br /> assert_equal(0.2e-1, <span class="variable">@parser</span>.parse(<span class="string">"0.2e-1"</span>))<br /> assert_equal(0.2E1, <span class="variable">@parser</span>.parse(<span class="string">"0.2e1"</span>))<br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_string_parsing<br /> assert_equal(String.new, <span class="variable">@parser</span>.parse(<span class="string">%Q{""}</span>))<br /> assert_equal(<span class="string">"JSON"</span>, <span class="variable">@parser</span>.parse(<span class="string">%Q{"JSON"}</span>))<br /><br /> assert_equal( <span class="string">%Q{nested "quotes"}</span>,<br /> <span class="variable">@parser</span>.parse(<span class="string">'"nested \"quotes\""'</span>) )<br /> assert_equal(<span class="string">"\n"</span>, <span class="variable">@parser</span>.parse(<span class="string">%Q{"\\n"}</span>))<br /> assert_equal( <span class="string">"a"</span>,<br /> <span class="variable">@parser</span>.parse(<span class="string">%Q{"\\u#{"%04X" % ?a}"}</span>) )<br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_array_parsing<br /> assert_equal(Array.new, <span class="variable">@parser</span>.parse(<span class="string">%Q{[]}</span>))<br /> assert_equal( [<span class="string">"JSON"</span>, 3.1415, <span class="keyword">true</span>],<br /> <span class="variable">@parser</span>.parse(<span class="string">%Q{["JSON", 3.1415, true]}</span>) )<br /> assert_equal([1, [2, [3]]], <span class="variable">@parser</span>.parse(<span class="string">%Q{[1, [2, [3]]]}</span>))<br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_object_parsing<br /> assert_equal(Hash.new, <span class="variable">@parser</span>.parse(<span class="string">%Q{{}}</span>))<br /> assert_equal( {<span class="string">"JSON"</span> =&gt; 3.1415, <span class="string">"data"</span> =&gt; <span class="keyword">true</span>},<br /> <span class="variable">@parser</span>.parse(<span class="string">%Q{{"JSON": 3.1415, "data": true}}</span>) )<br /> assert_equal( { <span class="string">"Array"</span> =&gt; [1, 2, 3],<br /> <span class="string">"Object"</span> =&gt; {<span class="string">"nested"</span> =&gt; <span class="string">"objects"</span>} },<br /> <span class="variable">@parser</span>.parse(<span class="string">&lt;&lt;-END_OBJECT) )<br /> {"Array": [1, 2, 3], "Object": {"nested": "objects"}}<br /> END_OBJECT</span><br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_parse_errors<br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"{"</span>) }<br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">%q{{"key": true false}}</span>) }<br /><br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"["</span>) }<br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"[1,,2]"</span>) }<br /><br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">%Q{"}</span>) }<br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">%Q{"\\i"}</span>) }<br /><br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"$1,000"</span>) }<br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"1_000"</span>) }<br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"1K"</span>) }<br /><br /> assert_raise(RuntimeError) { <span class="variable">@parser</span>.parse(<span class="string">"unknown"</span>) }<br /> <span class="keyword">end</span><br /> <span class="keyword">end</span><br /></div></div></description>
<pubDate>Thu, 07 Feb 2008 08:08:52 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz155.html</guid>
</item>
<item>
<title>Making Change (#154)</title>
<link>http://www.rubyquiz.com/quiz154.html</link>
<description><p>In "Practical Ruby Projects," the author includes a couple of chapters involving coin simulations. These simulators are used to explore the possibilities of replacing a certain coin or adding a new coin.</p>
<p>One interesting subproblem of these simulations is that of making change. For example, if we need to give 39 cents change in the United States (where there are 25, 10, 5, and 1 cent pieces), we can give:</p>
<div class="code"><span class="type">ruby</span><div class="body"> &gt;&gt; make_change(39)<br /> =&gt; [25, 10, 1, 1, 1, 1]<br /><br /></div></div>
<p>What if the coins were 10, 7, and 1 cent pieces though and we wanted to make 14 cents change? We would probably want to do:</p>
<div class="code"><span class="type">ruby</span><div class="body"> &gt;&gt; make_change(14, [10, 7, 1])<br /> =&gt; [7, 7]<br /><br /></div></div>
<p>This week's Ruby Quiz is to complete a change making function with this skeleton:</p>
<div class="code"><span class="type">ruby</span><div class="body"> <span class="keyword">def</span> make_change(amount, coins = [25, 10, 5, 1])<br /><br /> <span class="keyword">end</span><br /><br /></div></div>
<p>Your function should always return the optimal change with optimal being the least amount of coins involved. You can assume you have an infinite number of coins to work with.</p></description>
<pubDate>Thu, 31 Jan 2008 08:13:51 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz154.html</guid>
</item>
<item>
<title>Longest Repeated Substring (#153)</title>
<link>http://www.rubyquiz.com/quiz153.html</link>
<description><p>This week's Ruby Quiz is to write a script that finds the longest repeated substring in a given text.</p>
<p>Your program will be passed some text on STDIN and is expected to print the longest repeated substring within that text to STDOUT.</p>
<p>Repeated substrings may not overlap. If more than one substring is repeated with the same length, you may print any of them. If there is no repeated substring, the result is an empty string (print nothing).</p>
<p>Example:</p>
<p class="example">$ echo banana | ruby longest_repeated_substring.rb<br />an<br /><br />OR<br /><br />$ echo banana | ruby longest_repeated_substring.rb<br />na</p>
<p>Make sure your code runs efficiently when passed a large text.</p></description>
<pubDate>Thu, 24 Jan 2008 10:11:45 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz153.html</guid>
</item>
<item>
<title>Counting Cards (#152)</title>
<link>http://www.rubyquiz.com/quiz152.html</link>
<description><p>Learning to count cards is much easier than Hollywood or the casinos would have us believe. Some systems only require you to track a single running total in your head.</p>
<p>One such system, called the Knock-out system of card counting, is extra easy. You start your count at 4 - (4 x number_of_decks). That gives us an initial running count of 0, -4, -20, or -28 for the common casino shoe sizes of 1, 2, 6, or 8 decks. From there, you add one each time you see a 2, 3, 4, 5, 6 or 7 and subtract one when you see a 10, jack, queen, king, or ace. The 8 and 9 cards do not affect the count. Once you learn to track the running count, you can make strategy decisions and vary your bets based on the times when the count is in your favor.</p>
<p>That's not a lot to remember, but it does take practice to get fast. You really need to get to where you can count a deck in 20 to 30 seconds if you are going to keep up with those fast moving casinos dealers.</p>
<p>This week's Ruby Quiz is to build a training program for helping you learn to count cards.</p>
<p>The program needs to show you one or more cards at a time, running through a Blackjack shoe. As it goes, the program should track the running count. Have it pause at random intervals, ask you the count, and notify you if you are right or wrong.</p>
<p>Both the time to go through the deck and the number of cards displayed at a time should be configurable. It's important to practice with seeing multiple cards at once because you learn to cancel out pairs of high and low cards. It might even be nice to provide a mixed mode, which varies the number of cards shown at a time.</p>
<p>You can show cards as simple Strings, ASCII art, or full graphics as you prefer. You may wish to make cards fully disappear after their display time though, to make the conditions more like they would be in a casino.</p></description>
<pubDate>Thu, 17 Jan 2008 16:17:49 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz152.html</guid>
</item>
<item>
<title>Studying Blackjack (#151)</title>
<link>http://www.rubyquiz.com/quiz151.html</link>
<description><p>The majority of the strategy in Blackjack hinges around the dealer's hand. The reasons are likely obvious to most of you: that's the hand you have to beat and the dealer plays by fixed rules we can predict.</p>
<p>For those unfamiliar with Blackjack, you only need to know a tiny bit about the game for the purposes of this exercise. The goal for both the player and the dealer is to draw cards to make a hand with the highest total possible, without going over 21. Going over 21 is called "busting" and it means you lose the hand. Face cards count for ten, aces are one or eleven (whichever is better for the hand), and all other cards count for their face value. You start with two cards and, if they happen to be a ten valued card and an ace (a count of 21), the hand is called a "natural." A natural is an automatic win in most cases.</p>
<p>The dealer begins with one of his two cards face up and one face down. We call the former the "upcard." The dealer will "hit" or take more cards until he reaches a count of 17 or higher. After that he will "stand" or leave the hand where it is. That tells us that there are only seven possible outcomes for the dealer: get dealt a natural, bust, or hit to a total of 17, 18, 19, 20, or 21.</p>
<p>We start every hand knowing half of what the dealer holds thanks to the upcard. Believe it or not, you can make pretty reliable guesses about how the hand will go with just that knowledge.</p>
<p>Write a Ruby program that shows the percent chance of a dealer reaching each possible outcome based on the upcard showing.</p>
<p>I'll give you some hints to verify your results. Basic Blackjack strategy teaches that we should assume the dealer "has a ten in the hole" (as the face down card). It's not always true, of course, but 17 is a common outcome for a dealer with an upcard of seven. Finally, we call five and six "the dealer's bust cards" for reasons that will become obvious if you are outputting correct percentages.</p>
<p>In the casinos Blackjack is often played with more than one deck shuffled together. One, two, six, and eight deck games are common. You may want to offer the option to adjust the deck size your program uses. Either way, let's default to two decks as an average of what a player will face.</p></description>
<pubDate>Fri, 11 Jan 2008 07:54:07 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz151.html</guid>
</item>
<item>
<title>Programmer Ping-Pong (#150)</title>
<link>http://www.rubyquiz.com/quiz150.html</link>
<description><p>This is a non-traditional Ruby Quiz that changes the rules of the contest. Please read the entire message before playing along. We will be back to normal quizzes next time, for those that end up missing them.</p>
<p class="example">The Game</p>
<p>Eric Hodel described Programmer Ping-Pong in his RubyConf 2007 presentation. I wasn't familiar with the concept before that and it sounds like fun, so let's all try it out together.</p>
<p>The rules are:</p>
<p class="example">* This quiz does not have a no-spoiler period so you may<br /> submit at anytime after reading this message<br />* I'll make the initial serve, starting the quiz off with<br /> a single failing test<br />* Anyone can return the ball at anytime by doing exactly<br /> two things, in order: make all tests pass including the<br /> recently added failure and then add a new failing test<br /> of your own</p>
<p>I want to see if we can build an entire library using just that process.</p>
<p class="example">The Task</p>
<p>Let's build a pure Ruby binary AVL tree. An AVL tree is a self-balancing binary tree data structure where insertion, deletion, and lookup all take O(log n) time to execute. This is handy to have for many search problems that must run quickly. It can also be used to build constructs like an OrderedHash.</p>
<p>You can read more about AVL trees on Wikipedia:</p>
<p><a href="http://en.wikipedia.org/wiki/Avl_tree">AVL tree</a></p>
<p>There's also a handy document describing their rotations in detail:</p>
<p><a href="http://fortheloot.com/public/AVLTreeTutorial.rtf">AVL tree Rotations Tutorial</a></p>
<p>What features our AVL tree will support will be decided by you as you write tests. Here are some suggestions of things we might try though, just to get you thinking:</p>
<p class="example">* Support for custom Ruby objects as nodes of the tree<br />* The ability to customize the comparison code, perhaps with a block<br />* A String output visualizer, possibly for the inspect() method<br />* Any other great features you can think of to add</p>
<p class="example">The Details</p>
<p>We will have two files: avl_tree.rb and test_avl_tree.rb. Please pass both files in each submission email you make for this quiz. Let's not complicate this with directory structures or zip files.</p>
<p>Please don't add any external dependencies, unless it's a standard library. We want everyone to be able to easily run this code and play along.</p>
<p>We are using Test::Unit instead of RSpec, or any other tool, for similar reasons.</p>
<p>Please keep your tests short. Under 10 lines is preferred, but don't go over 24.</p>
<p>Also try to test just one aspect of the implementation with each test. I did purposely say "aspect" and not "method." I do test more than one method in the serve and I can imagine other scenarios where it could be useful, like checking support for a handful of the standard Enumerator methods.</p>
<p>You can refactor any code as needed provided you do not change its function and all tests still pass after you do so.</p>
<p>Adds comments if you need to, but writing code that needs no comment would be even better.</p>
<p>Let's use some simple spacing conventions to keep all of us on the same page. Indent two space and do not use tabs. Break up long lines so they do not exceed 80 characters.</p>
<p>Finally, this quiz has the potential to be very chaotic. Take pity on your quizmaster who must track this process and on the rest of the community who may be bothered by a highly active thread. I suggest good email manners:</p>
<p class="example">* Use your client's "Reply" feature and make sure you are replying to<br /> the message that contains the test you made pass<br />* Trim any unneeded context from the reply, including the previous<br /> version of the code since you will be including the current copy<br /> of the whole thing<br />* Kindness to your fellow programmers trumps any listed guidelines</p>
<p class="example">The Serve</p>
<p>The initial contents of avl_tree.rb are:</p>
<div class="code"><span class="type">ruby</span><div class="body"> <span class="comment">#!/usr/bin/env ruby -wKU</span><br /><br /> <span class="keyword">class</span> AVLTree<br /><br /> <span class="keyword">end</span><br /><br /></div></div>
<p>The test file, test_avl_tree.rb, begins as:</p>
<div class="code"><span class="type">ruby</span><div class="body"> <span class="comment">#!/usr/bin/env ruby -wKU</span><br /><br /> require <span class="string">"test/unit"</span><br /><br /> require <span class="string">"avl_tree"</span><br /><br /> <span class="keyword">class</span> TestAVLTree &lt; Test::Unit::TestCase<br /> <span class="keyword">def</span> setup<br /> <span class="variable">@tree</span> = AVLTree.new<br /> <span class="keyword">end</span><br /><br /> <span class="keyword">def</span> test_tree_membership<br /> assert_equal(<span class="keyword">true</span>, <span class="variable">@tree</span>.empty?)<br /> assert_equal(<span class="keyword">false</span>, <span class="variable">@tree</span>.include?(3))<br /><br /> <span class="variable">@tree</span> &lt;&lt; 3<br /><br /> assert_equal(<span class="keyword">false</span>, <span class="variable">@tree</span>.empty?)<br /> assert_equal(<span class="keyword">true</span>, <span class="variable">@tree</span>.include?(3))<br /> <span class="keyword">end</span><br /> <span class="keyword">end</span><br /></div></div></description>
<pubDate>Thu, 20 Dec 2007 07:36:47 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz150.html</guid>
</item>
<item>
<title>Word Loop (#149)</title>
<link>http://www.rubyquiz.com/quiz149.html</link>
<description><p>Here's a fun little challenge from the Educational Computing Organization of Ontario.</p>
<p>Given a single word as input try to find a repeated letter inside of it such that you can loop the text around and reuse that letter. For example:</p>
<p class="example">$ ruby word_loop.rb Mississippi<br /> i<br /> p<br /> p<br />Mis<br /> ss<br /> si</p>
<p>or:</p>
<p class="example">$ ruby word_loop.rb Markham<br />Ma<br />ar<br />hk</p>
<p>or:</p>
<p class="example">$ ruby word_loop.rb yummy<br />yu<br />mm</p>
<p>If a loop cannot be made, your code can just print an error message:</p>
<p class="example">$ ruby word_loop.rb Dana<br />No loop.</p></description>
<pubDate>Thu, 13 Dec 2007 07:16:13 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz149.html</guid>
</item>
<item>
<title>Postfix to Infix (#148)</title>
<link>http://www.rubyquiz.com/quiz148.html</link>
<description><p>There are many different ways to write mathematical equations. Infix notation is probably the most popular and yields expressions like:</p>
<p class="example">2 * (3 + 5)</p>
<p>Some people like to work with a postfix notation (often called Reverse Polish Notation or just RPN) though, which doesn't require parentheses for the same equation:</p>
<p class="example">2 3 5 + *</p>
<p>You can compare the results of these equations using the Unix utilities bc (infix) and dc (postfix):</p>
<p class="example">$ bc &lt;&lt;&lt; '2 * (3 + 5)'<br />16<br />$ dc &lt;&lt;&lt; '2 3 5 + * p'<br />16</p>
<p>The "p" instruction tacked onto the end of the expression for dc just tells it to print the result.</p>
<p>This week's quiz is to write a script that translates postfix expressions into the equivalent infix expression. In the simplest form, your script should function as such:</p>
<p class="example">$ ruby postfix_to_infix.rb '2 3 +'<br />2 + 3</p>
<p>At minimum, try to support the four basic math operators: +, -, *, and /. Feel free to add others though. For numbers, remember to accept decimal values.</p>
<p>You can count on the postfix expressions having spaces between each term, if you like. While dc is content with 2 3+p, you don't have to support it unless you want to.</p>
<p>For an added bonus, try to keep the parentheses added to infix expressions to the minimum of what is needed. For example, prefer these results:</p>
<p class="example">$ ruby postfix_to_infix.rb '56 34 213.7 + * 678 -'<br />56 * (34 + 213.7) - 678<br />$ ruby postfix_to_infix.rb '1 56 35 + 16 9 - / +'<br />1 + (56 + 35) / (16 - 9)</p>
<p>to these:</p>
<p class="example">$ ruby postfix_to_infix.rb '56 34 213.7 + * 678 -'<br />((56 * (34 + 213.7)) - 678)<br />$ ruby postfix_to_infix.rb '1 56 35 + 16 9 - / +'<br />(1 + ((56 + 35) / (16 - 9)))</p>
<p>Posting equations and your output is not a spoiler.</p></description>
<pubDate>Sat, 08 Dec 2007 13:34:41 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz148.html</guid>
</item>
<item>
<title>Goedel (#147)</title>
<link>http://www.rubyquiz.com/quiz147.html</link>
<description><p>by Hugh Sasse</p>
<p>In the book "Starburst" by Frederik Pohl ISBN 0-345-27537-3, page 56, without really spoiling the plot, some characters complain about the verbosity of communications and encode a message by Gödelizing it (detailed on page 58).</p>
<p>The encoding works by taking each successive character of a message and raising each successive prime to some function of that character, and multiplying these powers of primes together. So for example we could use the ASCII code + 1 to allow for nulls to be encoded. Then "Ruby\n" would end up as:</p>
<p class="example">(2 ** R) * (3 ** u) * (5 ** b)....<br /><br />10992805522291106558517740012022207329045811217010725353610920778<br />28664749233402453985379760678149866991742205982820039955872246774<br />86029159248495553882158351479922840433375701904296875000000000000<br />00000000000000000000000000000000000000000000000000000000000000000<br />000000</p>
<p>The idea is partly to obscure the message by the amount of factorization needed. This quiz is to write a program to Gödelize a message, and a program to deGödelize it.</p>
<p>The funtion used to map characters described in the book is "A" =&gt; 1, "B" =&gt; 2, etc and an example is given where spaces are 0. Nothing further is said about punctuation, or lower case. The message sent in the book is:</p>
<p class="example">msg = (3.875 * (12 ** 26)) +<br /> (1973 ** 854) + (331 ** 852) +<br /> (17 ** 2008) + (3 ** 9707) + (2 ** 88) - 78</p>
<p>which it turns out has lots of 0 powers in it, so I strictly don't need the ASCII + 1 I've used in my example, I could use just ASCII, and the nulls would not increase the size of the resulting number. This further means that if a list of characters is sent in decreasing frequency order with the message, the most frequent could be encoded as 0 and the number would be that much smaller. In English it is likely to be an "e" or " " which ends up coded as 0.</p>
<p>Interesting things arising from this:</p>
<p class="example">1 Finding the power once a prime is selected<br />2 Getting the list of primes in the first place<br />3 encoding of characters, as mentioned above<br />4 representing the number that results from encoding.</p></description>
<pubDate>Sat, 08 Dec 2007 13:34:41 -0600</pubDate>
<guid>http://www.rubyquiz.com/quiz147.html</guid>
</item>
</channel>
</rss>