Saturday, February 1, 2014

Oh Snap! Hello, World!

In programming, the first program that many people write in a new language is Hello, World! A simple program that displays "Hello, World!" on the screen.

I think we should change from Hello, World! to Oh, Snap!

The Oh, Snap! program is based on the Oh, Snap! flowchat.


Here is Oh, Snap! in three languages, Python, C++, and Prolog.

Python

#ohSnap.py
#Clinton Woods
#Python 3, ver: 3.3.2
#Wing IDE 101, 5.0.1-1 (rev 30370)
#Program determines if an "Oh, snap!" is appropriately warranted.

def ohSnap():
    YES = ["YES", "Y"]
    NO = ["NO", "N"]
    YNOPTIONS = YES + NO

 
    while True:
        toldCheck = input("Did someone get told? ")
        if toldCheck.upper() in YNOPTIONS:
            if toldCheck.upper() in YES:
                print("Oh, snap!\n")
                break
            else:
                print("Tell them.\n")
     
        else:
            print("Invalid input. You got told.")
         
#__main__
if __name__ == "__main__":
    ohSnap()


C++

//ohSnap.cpp
//Clinton Woods
//C++
//Microsoft Visual C++ 2010 Express Ver. 10.0.30319.1
//Determines if Oh, Snap! is warranted.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void ohSnap (){
string told;
string yes = "yes";
string no = "no";

cout << "Was someone told? ";
getline (cin, told);
if(told == yes){
cout << "Oh snap!" << endl;
}
else if(told == no){
cout << "Tell them." << endl;
}
else{
cout << "Invalid input. You got told." << endl;
}
return;

}

int main(){
ohSnap();
return 0;
}


Prolog

%ohSnap.pl
%
%Clinton Woods
%Created: February 1, 2014
%
%Written in Microsoft Windows Notepad
%Developed for and tested using SWI-Prolog version 7.1.5
%
%This program runs on query.
%
%This program query's the user to determine if the use of Oh Snap is warranted.

ohSnap(oh_snap) :-
told(yes),
        !.

ohSnap(tell_them) :-
told(no),
        !.

ohSnap(invalid_input_you_got_told) :-
        told(C).

getTold :-
write('Did someone get told?: '),
read(A),
nl,
assert(told(A)).

query :-
retractall(told(A)),
getTold,
ohSnap(B),
write(B),
nl.

No comments: