27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

Using Example 26−8 as an inspiration, write a script that emulates a 64−bit shift register as an array.<br />

Implement functions to load the register, shift left, and shift right. Finally, write a function that<br />

interprets the register contents as eight 8−bit ASCII characters.<br />

Determinant<br />

Solve a 4 x 4 determinant.<br />

Hidden Words<br />

Write a "word−find" puzzle generator, a script that hides 10 input words in a 10 x 10 matrix of<br />

random letters. The words may be hidden across, down, or diagonally.<br />

Anagramming<br />

Anagram 4−letter input. For example, the anagrams of word are: do or rod row word. You may use<br />

/usr/share/dict/linux.words as the reference list.<br />

Fog Index<br />

The "fog index" of a passage of text estimates its reading difficulty, as a number corresponding<br />

roughly to a school grade level. For example, a passage with a fog index of 12 should be<br />

comprehensible to anyone with 12 years of schooling.<br />

The Gunning version of the fog index uses the following algorithm.<br />

1. Choose a section of the text at least 100 words in length.<br />

2. Count the number of sentences (a portion of a sentence truncated by the boundary of the text<br />

section counts as one).<br />

3. Find the average number of words per sentence.<br />

AVE_WDS_SEN = TOTAL_WORDS / SENTENCES<br />

4. Count the number of "difficult" words in the segment −− those containing at least 3 syllables.<br />

Divide this quantity by total words to get the proportion of difficult words.<br />

PRO_DIFF_WORDS = LONG_WORDS / TOTAL_WORDS<br />

5. The Gunning fog index is the sum of the above two quantities, multiplied by 0.4, then<br />

rounded to the nearest integer.<br />

G_FOG_INDEX = int ( 0.4 * ( AVE_WDS_SEN + PRO_DIFF_WORDS ) )<br />

Step 4 is by far the most difficult portion of the exercise. There exist various algorithms for estimating<br />

the syllable count of a word. A rule−of−thumb formula might consider the number of letters in a word<br />

and the vowel−consonant mix.<br />

A strict interpretation of the Gunning Fog index does not count compound words and proper nouns as<br />

"difficult" words, but this would enormously complicate the script.<br />

Calculating PI using Buffon's Needle<br />

The Eighteenth Century French mathematician de Buffon came up with a novel experiment.<br />

Repeatedly drop a needle of length "n" onto a wooden floor composed of long and narrow parallel<br />

boards. The cracks separating the equal−width floorboards are a fixed distance "d" apart. Keep track<br />

of the total drops and the number of times the needle intersects a crack on the floor. The ratio of these<br />

two quantities turns out to be a fractional multiple of PI.<br />

In the spirit of Example 12−35, write a script that runs a Monte Carlo simulation of Buffon's Needle.<br />

To simplify matters, set the needle length equal to the distance between the cracks, n = d.<br />

Hint: there are actually two critical variables: the distance from the center of the needle to the nearest<br />

crack to it, and the angle of the needle to that crack. You may use bc to handle the calculations.<br />

Playfair Cipher<br />

Appendix I. Exercises 437

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!