📄 writingscripts.html
字号:
></SPAN>, so you don't end up complaining about yourself.</P><P>As necessary, use the appropriate <AHREF="communications.html#COMMUNINFO1">network analysis commands</A>.</P><P>For some ideas, see <AHREF="communications.html#ISSPAMMER">Example 15-40</A> and <AHREF="contributed-scripts.html#ISSPAMMER2">Example A-30</A>.</P><P>Optional: Write a script that searches through a list of e-mail messages and deletes the spam according to specified filters.</P></DD><DT><BCLASS="COMMAND">Creating man pages</B></DT><DD><P>Write a script that automates the process of creating <AHREF="external.html#MANREF">man pages</A>.</P><P>Given a text file which contains information to be formatted into a <ICLASS="FIRSTTERM">man page</I>, the script will read the file, then invoke the appropriate <AHREF="textproc.html#GROFFREF">groff</A> commands to output the corresponding <ICLASS="FIRSTTERM">man page</I> to <TTCLASS="FILENAME">stdout</TT>. The text file contains blocks of information under the standard <ICLASS="FIRSTTERM">man page</I> headings, i.e., <SPANCLASS="QUOTE">"NAME,"</SPAN> <SPANCLASS="QUOTE">"SYNOPSIS,"</SPAN> <SPANCLASS="QUOTE">"DESCRIPTION,"</SPAN> etc.</P><P>See <AHREF="textproc.html#MANVIEW">Example 15-28</A>.</P></DD><DT><BCLASS="COMMAND">Morse Code</B></DT><DD><P>Convert a text file to Morse code. Each character of the text file will be represented as a corresponding Morse code group of dots and dashes (underscores), separated by whitespace from the next. For example: <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 Invoke the "morse.sh" script with "script" 2 as an argument to convert to Morse. 3 4 5 $ sh morse.sh script 6 7 ... _._. ._. .. .__. _ 8 s c r i p t</PRE></TD></TR></TABLE></P></DD><DT><BCLASS="COMMAND">Hex Dump</B></DT><DD><P>Do a hex(adecimal) dump on a binary file specified as an argument. The output should be in neat tabular fields, with the first field showing the address, each of the next 8 fields a 4-byte hex number, and the final field the ASCII equivalent of the previous 8 fields.</P><P>The obvious followup to this is to extend the hex dump script into a disassembler. Using a lookup table, or some other clever gimmick, convert the hex values into 80x86 op codes.</P></DD><DT><BCLASS="COMMAND">Emulating a Shift Register</B></DT><DD><P>Using <AHREF="arrays.html#STACKEX">Example 26-15</A> as an inspiration, write a script that emulates a 64-bit shift register as an <AHREF="arrays.html#ARRAYREF">array</A>. Implement functions to <ICLASS="FIRSTTERM">load</I> the register, <ICLASS="FIRSTTERM">shift left</I>, <ICLASS="FIRSTTERM">shift right</I>, and <ICLASS="FIRSTTERM">rotate</I> it. Finally, write a function that interprets the register contents as eight 8-bit ASCII characters.</P></DD><DT><BCLASS="COMMAND">Determinant</B></DT><DD><P>Solve a 4 x 4 determinant.</P></DD><DT><BCLASS="COMMAND">Hidden Words</B></DT><DD><P>Write a <SPANCLASS="QUOTE">"word-find"</SPAN> puzzle generator, a script that hides 10 input words in a 10 x 10 matrix of random letters. The words may be hidden across, down, or diagonally.</P><P>Optional: Write a script that <SPANCLASS="emphasis"><ICLASS="EMPHASIS">solves</I></SPAN> word-find puzzles. To keep this from becoming too difficult, the solution script will find only horizontal and vertical words. (Hint: Treat each row and column as a string, and search for substrings.)</P></DD><DT><BCLASS="COMMAND">Anagramming</B></DT><DD><P> Anagram 4-letter input. For example, the anagrams of <SPANCLASS="emphasis"><ICLASS="EMPHASIS">word</I></SPAN> are: <SPANCLASS="emphasis"><ICLASS="EMPHASIS">do or rod row word</I></SPAN>. You may use <TTCLASS="FILENAME">/usr/share/dict/linux.words</TT> as the reference list.</P></DD><DT><BCLASS="COMMAND">Word Ladders</B></DT><DD><P>A <SPANCLASS="QUOTE">"word ladder"</SPAN> is a sequence of words, with each successive word in the sequence differing from the previous one by a single letter.</P><P>For example, to <SPANCLASS="QUOTE">"ladder"</SPAN> from <SPANCLASS="emphasis"><ICLASS="EMPHASIS">mark</I></SPAN> to <SPANCLASS="emphasis"><ICLASS="EMPHASIS">vase</I></SPAN>:</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 mark --> park --> part --> past --> vast --> vase 2 ^ ^ ^ ^ ^</PRE></TD></TR></TABLE> </P><P>Write a script that solves word ladder puzzles. Given a starting and an ending word, the script will list all intermediate steps in the <SPANCLASS="QUOTE">"ladder."</SPAN> Note that <SPANCLASS="emphasis"><ICLASS="EMPHASIS">all</I></SPAN> words in the sequence must be legitimate dictionary words.</P></DD><DT><BCLASS="COMMAND">Fog Index</B></DT><DD><P>The <SPANCLASS="QUOTE">"fog index"</SPAN> of a passage of text estimates its reading difficulty, as a number corresponding roughly to a school grade level. For example, a passage with a fog index of 12 should be comprehensible to anyone with 12 years of schooling.</P><P>The Gunning version of the fog index uses the following algorithm.</P><OLTYPE="1"><LI><P>Choose a section of the text at least 100 words in length.</P></LI><LI><P>Count the number of sentences (a portion of a sentence truncated by the boundary of the text section counts as one).</P></LI><LI><P>Find the average number of words per sentence.</P><P>AVE_WDS_SEN = TOTAL_WORDS / SENTENCES</P></LI><LI><P>Count the number of <SPANCLASS="QUOTE">"difficult"</SPAN> words in the segment -- those containing at least 3 syllables. Divide this quantity by total words to get the proportion of difficult words.</P><P>PRO_DIFF_WORDS = LONG_WORDS / TOTAL_WORDS</P></LI><LI><P>The Gunning fog index is the sum of the above two quantities, multiplied by 0.4, then rounded to the nearest integer.</P><P>G_FOG_INDEX = int ( 0.4 * ( AVE_WDS_SEN + PRO_DIFF_WORDS ) )</P></LI></OL><P>Step 4 is by far the most difficult portion of the exercise. There exist various algorithms for estimating the syllable count of a word. A rule-of-thumb formula might consider the number of letters in a word and the vowel-consonant mix.</P><P>A strict interpretation of the Gunning fog index does not count compound words and proper nouns as <SPANCLASS="QUOTE">"difficult"</SPAN> words, but this would enormously complicate the script.</P></DD><DT><BCLASS="COMMAND">Calculating PI using Buffon's Needle</B></DT><DD><P>The Eighteenth Century French mathematician de Buffon came up with a novel experiment. Repeatedly drop a needle of length <SPANCLASS="QUOTE">"n"</SPAN> onto a wooden floor composed of long and narrow parallel boards. The cracks separating the equal-width floorboards are a fixed distance <SPANCLASS="QUOTE">"d"</SPAN> apart. Keep track of the total drops and the number of times the needle intersects a crack on the floor. The ratio of these two quantities turns out to be a fractional multiple of PI.</P><P>In the spirit of <AHREF="mathc.html#CANNON">Example 15-48</A>, write a script that runs a Monte Carlo simulation of Buffon's Needle. To simplify matters, set the needle length equal to the distance between the cracks, <TTCLASS="PARAMETER"><I>n = d</I></TT>.</P><P>Hint: there are actually two critical variables: the distance from the center of the needle to the crack nearest to it, and the angle of the needle to that crack. You may use <AHREF="mathc.html#BCREF">bc</A> to handle the calculations.</P></DD><DT><BCLASS="COMMAND">Playfair Cipher</B></DT><DD><P>Implement the Playfair (Wheatstone) Cipher in a script.</P><P>The Playfair Cipher encrypts text by substitution of <SPANCLASS="QUOTE">"digrams"</SPAN> (2-letter groupings). It is traditional to use a 5 x 5 letter scrambled-alphabet <ICLASS="FIRSTTERM">key square</I> for the encryption and decryption.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 C O D E S 2 A B F G H 3 I K L M N 4 P Q R T U 5 V W X Y Z 6 7 Each letter of the alphabet appears once, except "I" also represents 8 "J". The arbitrarily chosen key word, "CODES" comes first, then all 9 the rest of the alphabet, in order from left to right, skipping letters 10 already used. 11 12 To encrypt, separate the plaintext message into digrams (2-letter 13 groups). If a group has two identical letters, delete the second, and 14 form a new group. If there is a single letter left over at the end, 15 insert a "null" character, typically an "X." 16 17 THIS IS A TOP SECRET MESSAGE 18 19 TH IS IS AT OP SE CR ET ME SA GE 20 21 For each digram, there are three possibilities. 22 ---------------------------------------------- 23 1) Both letters will be on the same row of the key square 24 For each letter, substitute the one immediately to the right, in that 25 row. If necessary, wrap around left to the beginning of the row. 26 27 or 28 29 2) Both letters will be in the same column of the key square 30 For each letter, substitute the one immediately below it, in that 31 row. If necessary, wrap around to the top of the column. 32 33 or 34 35 3) Both letters will form the corners of a rectangle within the key 36 square. For each letter, substitute the one on the other corner the 37 rectangle which lies on the same row. 38 39 40 The "TH" digram falls under case #3. 41 G H 42 M N 43 T U (Rectangle with "T" and "H" at corners) 44 45 T --> U 46 H --> G 47 48 49 The "SE" digram falls under case #1. 50 C O D E S (Row containing "S" and "E") 51 52 S --> C (wraps around left to beginning of row) 53 E --> S 54 55 ========================================================================= 56 57 To decrypt encrypted text, reverse the above procedure under cases #1 58 and #2 (move in opposite direction for substitution). Under case #3, 59 just take the remaining two corners of the rectangle. 60 61 62 Helen Fouche Gaines' classic work, ELEMENTARY CRYPTANALYSIS (1939), gives a 63 fairly detailed rundown on the Playfair Cipher and its solution methods.</PRE></TD></TR></TABLE> </P><P>This script will have three main sections</P><OLTYPE="I"><LI><P>Generating the <SPANCLASS="QUOTE">"key square"</SPAN>, based on a user-input keyword.</P></LI><LI><P>Encrypting a <SPANCLASS="QUOTE">"plaintext"</SPAN> message.</P></LI><LI><P>Decrypting encrypted text.</P></LI></OL><P>The script will make extensive use of <AHREF="arrays.html#ARRAYREF">arrays</A> and <AHREF="functions.html#FUNCTIONREF">functions</A>.</P></DD></DL></DIV><P>--</P><P>Please do not send the author your solutions to these exercises. There are better ways to impress him with your cleverness, such as submitting bugfixes and suggestions for improving this book.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="exercises.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="revisionhistory.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Exercises</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="exercises.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Revision History</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -