Sep 1, 2011

1-Armed Bandits && Demorgan's Law

When simulating the spin of a three-wheel slot machine, how do we detect three of a kind?





The following condition recognizes three of a kind:

var s1 = 'lemon', s2 = 'lemon', s3 = 'lemon';
console.log((s1 == s2) && (s1 == s3) && (s2 == s3));

We can modify it to control a loop that spins the wheels:

       s1 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s2 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s3 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);

       while (!((s1 == s2) && (s1 == s3) && (s2 == s3))) {
           //spin again...
       }

Applying Demorgan's Law we get:

       while (!(s1 == s2) || !(s1 == s3) || !(s2 == s3)) {
           //spin again...
       }

Equivalently:

       while ((s1 != s2) || (s1 != s3) || (s2 != s3)) {
           //spin again...
       }

To hit the jackpot, we must spin the wheels at least once. This leads to a final, simpler version using a do-while:

do {
       s1 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s2 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s3 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
} while ((s1 != s2) || (s1 != s3) || (s2 != s3));

The do...while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested.

It is a post-test loop; the while loop and the for loop are pre-test loops.

=> Use a do-while when an action must be performed at least once.


Jul 1, 2011

Web Apps w/o Programming: If this, then that (IFTTT)

IFImage by alex drennan via Flickr
IFTTT. Put the internet to work for you by creating tasks that fit this simple structure:
if this then that.

 A Web Tool That Lets You Automate the Internet. (NYT)

7 Ways to Automate Your Life With ifttt. (NYT)

Mar 1, 2011

SSH and the Unix Shell's Erase Key

Polished Turtle Shell
SSH sometime uses the Delete key to erase characters, but I prefer to use the Backspace key. How do I set it up?

You have several options, as follows:

  • Windows: Start SSH, select Edit > Settings > keyboard, then select Backspace sends Delete (or whichever key you want to use), then click OK.

  • OS X: Start Applications > Utilities > Terminal, the select Terminal > Window Settings..., then select Keyboardin the drop-down list, then check Delete key sends backspace.

  • Set your Unix shell's Erase key--

    Use Pico or Emacs to edit your login shell's startup file, .bash_profile:

    $ pico .bash_profile

    Add this line to the .bash_profile:

    stty erase '^h'

    save the file and exit pico.

Customizing your Unix Command Line Prompt

UnixImage by p373 via FlickrHow can I get the path for the current directory displayed in my Unix prompt?

Read Generic UNIX Interactive Prompts

Nov 28, 2010

Final Exam Review Problems

final exams
  • (1) Write an alarmClock() function that accepts an integer representing the day of the week (Mon=1, Tue=2, ... Sun=7) and returns a string: On weekdays the clock tells us to get up, and weekends it tells us to snooze more. On Monday, the clock adds some extra harassment to get us out of bed.

    Examples

    alarmClock(2) => Hi ho, hi ho, it's off to school we go...

    alarmClock(6) => Hit the snooze button!

    alarmClock(1) => Daylight in the swamps! Up and at'em!

    Strategy: Use an else-if 

  • _______________________________________________

    (2) Write a bowlingScoreFeedback() function that accepts a bowling score 0 .. 300 and returns one of these messages:

    300: "most triumphant!"
    200..299: "pretty good!"
    50..199: "making progress!"
    0..49: "better take up Scrabble!"

    Examples

    bowlingScoreFeedback(250) => pretty good!


    bowlingScoreFeedback(25) => better take up Scrabble!

    _________________________________

    (3) Suppose you want to get a table at a hip restaurant: The variable "style" represents the stylishness of your clothes and "bribe" represents the bribe presented to the Maitre'd.

    The Maitre'd is satisfied if style is 8 or more and bribe is 5 or more.

    One way to code this up is to write the "satisfied" expression as a straight translation of the problem statement, and then put a ! in front of it:
    //Elevate snoot if not satisfied
    function maitreD(style, bribe){
    if (! ((style >= 8) && (bribe >= 5)) ) {
       return "tant pis pour vous"
    else
       return "Ainsi, s'il vous plaƮt"
    }

Jul 14, 2010

Jimi Hendrix in JavaScript




ifSixWasNine, by Jared (a.k.a. "Jimi" ;-) Hendricks

ifSixWasNine(6) ==> 9
ifSixWasNine(12) ==> 18

Exercise: Write and test this function.