This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,16 @@
#include <iostream>
int main()
{
bool is_open[100] = { false };
// do the 100 passes
for (int pass = 0; pass < 100; ++pass)
for (int door = pass; door < 100; door += pass+1)
is_open[door] = !is_open[door];
// output the result
for (int door = 0; door < 100; ++door)
std::cout << "door #" << door+1 << (is_open[door]? " is open." : " is closed.") << std::endl;
return 0;
}

View file

@ -0,0 +1,19 @@
#include <iostream>
int main()
{
int square = 1, increment = 3;
for (int door = 1; door <= 100; ++door)
{
std::cout << "door #" << door;
if (door == square)
{
std::cout << " is open." << std::endl;
square += increment;
increment += 2;
}
else
std::cout << " is closed." << std::endl;
}
return 0;
}

View file

@ -0,0 +1,7 @@
#include <iostream> //compiled with "Dev-C++" , from RaptorOne
int main()
{
for(int i=1; i*i<=100; i++)
std::cout<<"Door "<<i*i<<" is open!"<<std::endl;
}

View file

@ -0,0 +1,14 @@
using System;
class Program
{
static void Main()
{
//To simplify door numbers, uses indexes 1 to 100 (rather than 0 to 99)
bool[] doors = new bool[101];
for (int pass = 1; pass <= 100; pass++)
for (int current = pass; current <= 100; current += pass)
doors[current] = !doors[current];
for (int i = 1; i <= 100; i++)
Console.WriteLine("Door #{0} " + (doors[i] ? "Open" : "Closed"), i);
}
}

View file

@ -0,0 +1,20 @@
using System;
class Program
{
static void Main()
{
int door = 1, inrementer = 0;
for (int current = 1; current <= 100; current++)
{
Console.Write("Door #{0} ", current);
if (current == door)
{
Console.WriteLine("Open");
inrementer++;
door += 2 * inrementer + 1;
}
else
Console.WriteLine("Closed");
}
}
}

View file

@ -0,0 +1,10 @@
using System;
class Program
{
static void Main()
{
double n;
for (int t = 1; t <= 100; ++t)
Console.WriteLine(t + ": " + (((n = Math.Sqrt(t)) == (int)n) ? "Open" : "Closed"));
}
}

View file

@ -0,0 +1,41 @@
using System;
class Program
{
static void Main(string[] args)
{
bool[] Doors = new bool[101];
//Close all doors to start
for (int g=1;g<101;g++) Doors[g] = false;
for (int i = 1; i < 101; i++)//number of passes
{
for (int d = 1; d < 101; d++)//door number
{
if ((d % i) == 0)
{
if (Doors[d]) Doors[d] = false;
else Doors[d] = true;
}
}
}
Console.WriteLine("Passes Completed!!! Here are the results: \r\n");
for (int p = 1; p < 101; p++)
{
if (Doors[p])
{
string doorStatus = String.Format("Door #{0} is \'OPENED\'.", p.ToString());
Console.WriteLine(doorStatus);
}
else
{
string doorStatus = String.Format("Door #{0} is \'CLOSED\'.", p.ToString());
Console.WriteLine(doorStatus);
}
}
}
}

View file

@ -0,0 +1 @@
100_doors

View file

@ -0,0 +1,64 @@
(deffacts initial-state
(door-count 100)
)
(deffunction toggle
(?state)
(switch ?state
(case "open" then "closed")
(case "closed" then "open")
)
)
(defrule create-doors-and-visits
(door-count ?count)
=>
(loop-for-count (?num 1 ?count) do
(assert (door ?num "closed"))
(assert (visit-from ?num ?num))
)
(assert (doors initialized))
)
(defrule visit
(door-count ?max)
?visit <- (visit-from ?num ?step)
?door <- (door ?num ?state)
=>
(retract ?visit)
(retract ?door)
(assert (door ?num (toggle ?state)))
(if
(<= (+ ?num ?step) ?max)
then
(assert (visit-from (+ ?num ?step) ?step))
)
)
(defrule start-printing
(doors initialized)
(not (visit-from ? ?))
=>
(printout t "These doors are open:" crlf)
(assert (print-from 1))
)
(defrule print-door
(door-count ?max)
?pf <- (print-from ?num)
(door ?num ?state)
=>
(retract ?pf)
(if
(= 0 (str-compare "open" ?state))
then
(printout t ?num " ")
)
(if
(< ?num ?max)
then
(assert (print-from (+ ?num 1)))
else
(printout t crlf "All other doors are closed." crlf)
)
)

View file

@ -0,0 +1,20 @@
(deffacts initial-state
(door-count 100)
)
(deffunction is-square
(?num)
(= (sqrt ?num) (integer (sqrt ?num)))
)
(defrule check-doors
(door-count ?count)
=>
(printout t "These doors are open:" crlf)
(loop-for-count (?num 1 ?count) do
(if (is-square ?num) then
(printout t ?num " ")
)
)
(printout t crlf "All other doors are closed." crlf)
)

View file

@ -0,0 +1,31 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. 100Doors.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Current PIC 9(3) VALUE ZEROES.
01 StepSize PIC 9(3) VALUE ZEROES.
01 DoorTable.
02 Doors PIC 9(1) OCCURS 100 TIMES.
01 Idx PIC 9(3).
PROCEDURE DIVISION.
Begin.
MOVE 1 TO StepSize
PERFORM 100 TIMES
MOVE StepSize TO Current
PERFORM UNTIL Current > 100
SUBTRACT Doors(Current) FROM 1 GIVING Doors(Current)
ADD StepSize TO Current GIVING Current
END-PERFORM
ADD 1 TO StepSize GIVING StepSize
END-PERFORM
PERFORM VARYING Idx FROM 1 BY 1
UNTIL Idx > 100
IF Doors(Idx) = 0
DISPLAY Idx " is closed."
ELSE
DISPLAY Idx " is open."
END-IF
END-PERFORM
STOP RUN.

View file

@ -0,0 +1,25 @@
doorCount = 1;
doorList = "";
// create all doors and set all doors to open
while (doorCount LTE 100) {
doorList = ListAppend(doorList,"1");
doorCount = doorCount + 1;
}
loopCount = 2;
doorListLen = ListLen(doorList);
while (loopCount LTE 100) {
loopDoorListCount = 1;
while (loopDoorListCount LTE 100) {
testDoor = loopDoorListCount / loopCount;
if (testDoor EQ Int(testDoor)) {
checkOpen = ListGetAt(doorList,loopDoorListCount);
if (checkOpen EQ 1) {
doorList = ListSetAt(doorList,loopDoorListCount,"0");
} else {
doorList = ListSetAt(doorList,loopDoorListCount,"1");
}
}
loopDoorListCount = loopDoorListCount + 1;
}
loopCount = loopCount + 1;
}

View file

@ -0,0 +1,11 @@
doorCount = 1;
doorList = "";
loopCount = 1;
while (loopCount LTE 100) {
if (Sqr(loopCount) NEQ Int(Sqr(loopCount))) {
doorList = ListAppend(doorList,0);
} else {
doorList = ListAppend(doorList,1);
}
loopCount = loopCount + 1;
}

View file

@ -0,0 +1,11 @@
// Display all doors
<cfloop from="1" to="100" index="x">
Door #x# Open: #YesNoFormat(ListGetAt(doorList,x))#<br />
</cfloop>
// Output only open doors
<cfloop from="1" to="100" index="x">
<cfif ListGetAt(doorList,x) EQ 1>
#x#<br />
</cfif>
</cfloop>

View file

@ -0,0 +1,38 @@
(defun visit-door (doors doornum value1 value2)
"visits a door, swapping the value1 to value2 or vice-versa"
(let ((d (copy-list doors))
(n (- doornum 1)))
(if (eql (nth n d) value1)
(setf (nth n d) value2)
(setf (nth n d) value1))
d))
(defun visit-every (doors num iter value1 value2)
"visits every 'num' door in the list"
(if (> (* iter num) (length doors))
doors
(visit-every (visit-door doors (* num iter) value1 value2)
num
(+ 1 iter)
value1
value2)))
(defun do-all-visits (doors cnt value1 value2)
"Visits all doors changing the values accordingly"
(if (< cnt 1)
doors
(do-all-visits (visit-every doors cnt 1 value1 value2)
(- cnt 1)
value1
value2)))
(defun print-doors (doors)
"Pretty prints the doors list"
(format T "~{~A ~A ~A ~A ~A ~A ~A ~A ~A ~A~%~}~%" doors))
(defun start (&optional (size 100))
"Start the program"
(let* ((open "_")
(shut "#")
(doors (make-list size :initial-element shut)))
(print-doors (do-all-visits doors size open shut))))

View file

@ -0,0 +1,9 @@
(define-modify-macro toggle () not)
(defun 100-doors ()
(let ((doors (make-array 100 :initial-element nil)))
(dotimes (i 100)
(loop for j from i below 100 by (1+ i)
do (toggle (svref doors j))))
(dotimes (i 100)
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))

View file

@ -0,0 +1,21 @@
(defun perfect-square-list (n)
"Generates a list of perfect squares from 0 up to n"
(loop for i from 1 to (sqrt n) collect (expt i 2)))
(defun open-door (doors num open)
"Sets door at num to open"
(setf (nth (- num 1) doors) open)
doors)
(defun visit-all (doors vlist open)
"Visits and opens all the doors indicated in vlist"
(if (null vlist)
doors
(visit-all (open-door doors (car vlist) open)
(cdr vlist)
open)))
(defun start2 (&optional (size 100))
"Start the program"
(print-doors (visit-all (make-list size :initial-element "#")
(perfect-square-list size) "_")))

View file

@ -0,0 +1,5 @@
(let ((i 0))
(mapcar (lambda (x)
(if (zerop (mod (sqrt (incf i)) 1))
"_" "#"))
(make-list 100)))

View file

@ -0,0 +1,36 @@
import std.stdio;
enum DoorState { Closed, Open }
alias DoorState[] Doors;
Doors flipUnoptimized(Doors doors) {
doors[] = DoorState.Closed;
foreach (i; 0 .. doors.length)
for (int j = i; j < doors.length; j += i+1)
if (doors[j] == DoorState.Open)
doors[j] = DoorState.Closed;
else
doors[j] = DoorState.Open;
return doors;
}
Doors flipOptimized(Doors doors) {
doors[] = DoorState.Closed;
for (int i = 1; i*i <= doors.length; i++)
doors[i*i - 1] = DoorState.Open;
return doors;
}
// test program
void main() {
auto doors = new Doors(100);
foreach (i, door; flipUnoptimized(doors))
if (door == DoorState.Open)
write(i+1, " ");
writeln();
foreach (i, door; flipOptimized(doors))
if (door == DoorState.Open)
write(i+1, " ");
writeln();
}

View file

@ -0,0 +1,11 @@
var doors : array [1..100] of Boolean;
var i, j : Integer;
for i := 1 to 100 do
for j := i to 100 do
if (j mod i) = 0 then
doors[j] := not doors[j];
for i := 1 to 100 do
if doors[i] then
PrintLn('Door '+IntToStr(i)+' is open');

View file

@ -0,0 +1,7 @@
main() {
for (var k = 1, x = new List(101); k <= 100; k++) {
for (int i = k; i <= 100; i += k)
x[i] = !x[i];
if (x[k]) print("$k open");
}
}

View file

@ -0,0 +1,4 @@
main() {
for(int i=1,s=3;i<=100;i+=s,s+=2)
print("door $i is open");
}

View file

@ -0,0 +1,45 @@
#!/usr/bin/env rune
var toggles := []
var gets := []
# Set up GUI (and data model)
def frame := <swing:makeJFrame>("100 doors")
frame.getContentPane().setLayout(<awt:makeGridLayout>(10, 10))
for i in 1..100 {
def component := <import:javax.swing.makeJCheckBox>(E.toString(i))
toggles with= fn { component.setSelected(!component.isSelected()) }
gets with= fn { component.isSelected() }
frame.getContentPane().add(component)
}
# Set up termination condition
def done
frame.addWindowListener(def _ {
to windowClosing(event) {
bind done := true
}
match _ {}
})
# Open and close doors
def loop(step, i) {
toggles[i] <- ()
def next := i + step
timer.whenPast(timer.now() + 10, fn {
if (next >= 100) {
if (step >= 100) {
# Done.
} else {
loop <- (step + 1, step)
}
} else {
loop <- (step, i + step)
}
})
}
loop(1, 0)
frame.pack()
frame.show()
interp.waitAtTop(done)

View file

@ -0,0 +1,22 @@
program OneHundredDoors
function main()
doors boolean[] = new boolean[100];
n int = 100;
for (i int from 1 to n)
for (j int from i to n by i)
doors[j] = !doors[j];
end
end
for (i int from 1 to n)
if (doors[i])
SysLib.writeStdout( "Door " + i + " is open" );
end
end
end
end

View file

@ -0,0 +1,11 @@
open generic
type Door = Open | Closed
deriving Show
gate [] _ = []
gate (x::xs) (y::ys)
| x == y = Open :: gate xs ys
| else = Closed :: gate xs ys
run n = gate [1..n] [& k*k \\ k <- [1..]]

View file

@ -0,0 +1,2 @@
open list
run n = takeWhile (<n) [& k*k \\ k <- [1..]]

View file

@ -0,0 +1,83 @@
(defun create-doors ()
"Returns a list of closed doors
Each door only has two status: open or closed.
If a door is closed it has the value 0, if it's open it has the value 1."
(let ((return_value '(0))
;; There is already a door in the return_value, so k starts at 1
;; otherwise we would need to compare k against 99 and not 100 in
;; the while loop
(k 1))
(while (< k 100)
(setq return_value (cons 0 return_value))
(setq k (+ 1 k)))
return_value))
(defun toggle-single-door (doors)
"Toggle the stat of the door at the `car' position of the DOORS list
DOORS is a list of integers with either the value 0 or 1 and it represents
a row of doors.
Returns a list where the `car' of the list has it's value toggled (if open
it becomes closed, if closed it becomes open)."
(if (= (car doors) 1)
(cons 0 (cdr doors))
(cons 1 (cdr doors))))
(defun toggle-doors (doors step original-step)
"Step through all elements of the doors' list and toggle a door when step is 1
DOORS is a list of integers with either the value 0 or 1 and it represents
a row of doors.
STEP is the number of doors we still need to transverse before we arrive
at a door that has to be toggled.
ORIGINAL-STEP is the value of the argument step when this function is
called for the first time.
Returns a list of doors"
(cond ((null doors)
'())
((= step 1)
(cons (car (toggle-single-door doors))
(toggle-doors (cdr doors) original-step original-step)))
(t
(cons (car doors)
(toggle-doors (cdr doors) (- step 1) original-step)))))
(defun main-program ()
"The main loop for the program"
(let ((doors_list (create-doors))
(k 1)
;; We need to define max-specpdl-size and max-specpdl-size to big
;; numbers otherwise the loop reaches the max recursion depth and
;; throws an error.
;; If you want more information about these variables, press Ctrl
;; and h at the same time and then press v and then type the name
;; of the variable that you want to read the documentation.
(max-specpdl-size 5000)
(max-lisp-eval-depth 2000))
(while (< k 101)
(setq doors_list (toggle-doors doors_list k k))
(setq k (+ 1 k)))
doors_list))
(defun print-doors (doors)
"This function prints the values of the doors into the current buffer.
DOORS is a list of integers with either the value 0 or 1 and it represents
a row of doors.
"
;; As in the main-program function, we need to set the variable
;; max-lisp-eval-depth to a big number so it doesn't reach max recursion
;; depth.
(let ((max-lisp-eval-depth 5000))
(unless (null doors)
(insert (int-to-string (car doors)))
(print-doors (cdr doors)))))
;; Returns a list with the final solution
(main-program)
;; Print the final solution on the buffer
(print-doors (main-program))

View file

@ -0,0 +1,11 @@
>function Doors () ...
$ doors:=zeros(1,100);
$ for i=1 to 100
$ for j=i to 100 step i
$ doors[j]=!doors[j];
$ end;
$ end;
$ return doors
$endfunction
>nonzeros(Doors())
[ 1 4 9 16 25 36 49 64 81 100 ]

View file

@ -0,0 +1,23 @@
-- doors.ex
include std/console.e
sequence doors
doors = repeat( 0, 100 ) -- 1 to 100, initialised to false
for pass = 1 to 100 do
for door = pass to 100 by pass do
--printf( 1, "%d", doors[door] )
--printf( 1, "%d", not doors[door] )
doors[door] = not doors[door]
end for
end for
sequence oc
for i = 1 to 100 do
if doors[i] then
oc = "open"
else
oc = "closed"
end if
printf( 1, "door %d is %s\n", { i, oc } )
end for

View file

@ -0,0 +1,98 @@
#include <random>
#include <iostream>
#include <stack>
#include <set>
#include <string>
#include <functional>
using namespace std;
class RPNParse
{
public:
stack<double> stk;
multiset<int> digits;
void op(function<double(double,double)> f)
{
if(stk.size() < 2)
throw "Improperly written expression";
int b = stk.top(); stk.pop();
int a = stk.top(); stk.pop();
stk.push(f(a, b));
}
void parse(char c)
{
if(c >= '0' && c <= '9')
{
stk.push(c - '0');
digits.insert(c - '0');
}
else if(c == '+')
op([](double a, double b) {return a+b;});
else if(c == '-')
op([](double a, double b) {return a-b;});
else if(c == '*')
op([](double a, double b) {return a*b;});
else if(c == '/')
op([](double a, double b) {return a/b;});
}
void parse(string s)
{
for(int i = 0; i < s.size(); ++i)
parse(s[i]);
}
double getResult()
{
if(stk.size() != 1)
throw "Improperly written expression";
return stk.top();
}
};
int main()
{
random_device seed;
mt19937 engine(seed());
uniform_int_distribution<> distribution(1, 9);
auto rnd = bind(distribution, engine);
multiset<int> digits;
cout << "Make 24 with the digits: ";
for(int i = 0; i < 4; ++i)
{
int n = rnd();
cout << " " << n;
digits.insert(n);
}
cout << endl;
RPNParse parser;
try
{
string input;
getline(cin, input);
parser.parse(input);
if(digits != parser.digits)
cout << "Error: Not using the given digits" << endl;
else
{
double r = parser.getResult();
cout << "Result: " << r << endl;
if(r > 23.999 && r < 24.001)
cout << "Good job!" << endl;
else
cout << "Try again." << endl;
}
}
catch(char* e)
{
cout << "Error: " << e << endl;
}
return 0;
}

View file

@ -0,0 +1,36 @@
(define-condition choose-digits () ())
(define-condition bad-equation (error) ())
(defun 24-game ()
(let (chosen-digits)
(labels ((prompt ()
(format t "Chosen digits: ~{~D~^, ~}~%~
Enter expression (or `bye' to quit, `!' to choose new digits): "
chosen-digits)
(read))
(lose () (error 'bad-equation))
(choose () (setf chosen-digits (loop repeat 4 collecting (random 10))))
(check (e)
(typecase e
((eql bye) (return-from 24-game))
((eql !) (signal 'choose-digits))
(atom (lose))
(cons (check-sub (car e) (check-sub (cdr e) chosen-digits)) e)))
(check-sub (sub allowed-digits)
(typecase sub
((member nil + - * /) allowed-digits)
(integer
(if (member sub allowed-digits)
(remove sub allowed-digits :count 1)
(lose)))
(cons (check-sub (car sub) (check-sub (cdr sub) allowed-digits)))
(t (lose))))
(win ()
(format t "You win.~%")
(return-from 24-game)))
(choose)
(loop
(handler-case
(if (= 24 (eval (check (prompt)))) (win) (lose))
(error () (format t "Bad equation, try again.~%"))
(choose-digits () (choose)))))))

View file

@ -0,0 +1,104 @@
(defconstant +ops+ '(* / + -))
(defun expr-numbers (e &optional acc)
"Return all the numbers in argument positions in the expression."
(cond
((numberp e) (cons e acc))
((consp e)
(append (apply #'append
(mapcar #'expr-numbers (cdr e)))
acc))))
(defun expr-well-formed-p (e)
"Return non-nil if the given expression is well-formed."
(cond
((numberp e) t)
((consp e)
(and (member (car e) +ops+)
(every #'expr-well-formed-p (cdr e))))
(t nil)))
(defun expr-valid-p (e available-digits)
"Return non-nil if the expression is well-formed and uses exactly
the digits specified."
(and (expr-well-formed-p e)
(equalp (sort (copy-seq available-digits) #'<)
(sort (expr-numbers e) #'<))))
(defun expr-get (&optional using)
(emit "Enter lisp form~@[ using the digit~P ~{~D~^ ~}~]: "
(when using
(length using)) using)
(let (*read-eval*)
(read)))
(defun digits ()
(sort (loop repeat 4 collect (1+ (random 9))) #'<))
(defun emit (fmt &rest args)
(format t "~&~?" fmt args))
(defun prompt (digits)
(emit "Using only these operators:~%~%~
~2T~{~A~^ ~}~%~%~
And exactly these numbers \(no repetition\):~%~%~
~2T~{~D~^ ~}~%~%~
~A"
+ops+ digits (secondary-prompt)))
(defun secondary-prompt ()
(fill-to 50 "Enter a lisp form which evaluates to ~
the integer 24, or \"!\" to get fresh ~
digits, or \"q\" to abort."))
(defun fill-to (n fmt &rest args)
"Poor-man's text filling mechanism."
(loop with s = (format nil "~?" fmt args)
for c across s
and i from 0
and j = 0 then (1+ j) ; since-last-newline ctr
when (char= c #\Newline)
do (setq j 0)
else when (and (not (zerop j))
(zerop (mod j n)))
do (loop for k from i below (length s)
when (char= #\Space (schar s k))
do (progn
(setf (schar s k) #\Newline
j 0)
(loop-finish)))
finally (return s)))
(defun 24-game ()
(loop with playing-p = t
and initial-digits = (digits)
for attempts from 0
and digits = initial-digits then (digits)
while playing-p
do (loop for e = (expr-get (unless (zerop attempts)
digits))
do
(case e
(! (loop-finish))
(Q (setq playing-p nil)
(loop-finish))
(R (emit "Current digits: ~S" digits))
(t
(if (expr-valid-p e digits)
(let ((v (eval e)))
(if (eql v 24)
(progn
(emit "~%~%---> A winner is you! <---~%~%")
(setq playing-p nil)
(loop-finish))
(emit "Sorry, the form you entered ~
computes to ~S, not 24.~%~%"
v)))
(emit "Sorry, the form you entered did not ~
compute.~%~%")))))
initially (prompt initial-digits)))

37
Task/24-game/D/24-game.d Normal file
View file

@ -0,0 +1,37 @@
import std.stdio, std.random, std.math, std.algorithm, std.range,
std.typetuple;
void main() {
void op(char c)() {
if (stack.length < 2)
throw new Exception("Wrong expression.");
stack[$ - 2] = mixin("stack[$ - 2]" ~ c ~ "stack[$ - 1]");
stack.popBack();
}
const problem = iota(4).map!(_ => uniform(1, 10))().array();
writeln("Make 24 with the digits: ", problem);
double[] stack;
int[] digits;
foreach (const char c; readln())
switch (c) {
case ' ', '\t', '\n': break;
case '1': .. case '9':
stack ~= c - '0';
digits ~= c - '0';
break;
foreach (o; TypeTuple!('+', '-', '*', '/')) {
case o: op!o(); break;
}
break;
default: throw new Exception("Wrong char: " ~ c);
}
if (!digits.sort().equal(problem.dup.sort()))
throw new Exception("Not using the given digits.");
if (stack.length != 1)
throw new Exception("Wrong expression.");
writeln("Result: ", stack[0]);
writeln(abs(stack[0] - 24) < 0.001 ? "Good job!" : "Try again.");
}

View file

@ -0,0 +1,13 @@
#include <iostream>
using namespace std;
int main()
{
int bottles = 99;
do {
cout << bottles << " bottles of beer on the wall" << endl;
cout << bottles << " bottles of beer" << endl;
cout << "Take one down, pass it around" << endl;
cout << --bottles << " bottles of beer on the wall\n" << endl;
} while (bottles > 0);
}

View file

@ -0,0 +1,28 @@
#include <iostream>
template<int max, int min> struct bottle_countdown
{
static const int middle = (min + max)/2;
static void print()
{
bottle_countdown<max, middle+1>::print();
bottle_countdown<middle, min>::print();
}
};
template<int value> struct bottle_countdown<value, value>
{
static void print()
{
std::cout << value << " bottles of beer on the wall\n"
<< value << " bottles of beer\n"
<< "Take one down, pass it around\n"
<< value-1 << " bottles of beer\n\n";
}
};
int main()
{
bottle_countdown<100, 1>::print();
return 0;
}

View file

@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
void rec(int bottles)
{
if ( bottles!=0)
{
cout << bottles << " bottles of beer on the wall" << endl;
cout << bottles << " bottles of beer" << endl;
cout << "Take one down, pass it around" << endl;
cout << --bottles << " bottles of beer on the wall\n" << endl;
rec(bottles);
}
}
int main()
{
rec(99);
system("pause");
return 0;
}

View file

@ -0,0 +1,30 @@
#include <iostream>
#include <ostream>
#define BOTTLE(nstr) nstr " bottles of beer"
#define WALL(nstr) BOTTLE(nstr) " on the wall"
#define PART1(nstr) WALL(nstr) "\n" BOTTLE(nstr) \
"\nTake one down, pass it around\n"
#define PART2(nstr) WALL(nstr) "\n\n"
#define MIDDLE(nstr) PART2(nstr) PART1(nstr)
#define SONG PART1("100") CD2 PART2("0")
#define CD2 CD3("9") CD3("8") CD3("7") CD3("6") CD3("5") \
CD3("4") CD3("3") CD3("2") CD3("1") CD4("")
#define CD3(pre) CD4(pre) MIDDLE(pre "0")
#define CD4(pre) MIDDLE(pre "9") MIDDLE(pre "8") MIDDLE(pre "7") \
MIDDLE(pre "6") MIDDLE(pre "5") MIDDLE(pre "4") MIDDLE(pre "3") \
MIDDLE(pre "2") MIDDLE(pre "1")
int main()
{
std::cout << SONG;
return 0;
}

View file

@ -0,0 +1,37 @@
//>,_
//Beer Song>,_
#include <iostream>
using namespace std;
int main(){ for( int
b=-1; b<99; cout <<
'\n') for ( int w=0;
w<3; cout << ".\n"){
if (w==2) cout << ((
b--) ?"Take one dow"
"n and pass it arou"
"nd":"Go to the sto"
"re and buy some mo"
"re"); if (b<0) b=99
; do{ if (w) cout <<
", "; if (b) cout <<
b; else cout << (
(w) ? 'n' : 'N') <<
"o more"; cout <<
" bottle" ; if
(b!=1) cout <<
's' ; cout <<
" of beer";
if (w!=1)
cout <<
" on th"
"e wall"
;} while
(!w++);}
return
0
;
}
//
// by barrym 2011-05-01
// no bottles were harmed in the
// making of this program!!!

View file

@ -0,0 +1,29 @@
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 99; i > -1; i--)
{
if (i == 0)
{
Console.WriteLine("No more bottles of beer on the wall, no more bottles of beer.");
Console.WriteLine("Go to the store and buy some more, 99 bottles of beer on the wall.");
break;
}
if (i == 1)
{
Console.WriteLine("1 bottle of beer on the wall, 1 bottle of beer.");
Console.WriteLine("Take one down and pass it around, no more bottles of beer on the wall.");
Console.WriteLine();
}
else
{
Console.WriteLine("{0} bottles of beer on the wall, {0} bottles of beer.", i);
Console.WriteLine("Take one down and pass it around, {0} bottles of beer on the wall.", i - 1);
Console.WriteLine();
}
}
}
}

View file

@ -0,0 +1,18 @@
using System;
using System.Linq;
class Program
{
static void Main()
{
var query = from total in Enumerable.Range(0,100).Reverse()
select (total > 0)
? string.Format("{0} bottles of beer on the wall\n{0} bottles of beer\nTake one down, pass it around", total)
: string.Format("{0} bottles left", total);
foreach (var item in query)
{
Console.WriteLine(item);
}
}
}

View file

@ -0,0 +1,28 @@
class songs
{
static void Main(string[] args)
{
beer(5);
}
private static void beer(int bottles)
{
for (int i = bottles; i > 0; i--)
{
if (i > 1)
{
Console.Write("{0}\n{1}\n{2}\n{3}\n\n",
i + " bottles of beer on the wall",
i + " bottles of beer",
"Take one down, pass it around",
(i - 1) + " bottles of beer on the wall");
}
else
Console.Write("{0}\n{1}\n{2}\n{3}\n\n",
i + " bottle of beer on the wall",
i + " bottle of beer",
"Take one down, pass it around",
(i - 1) + " bottles of beer on the wall....");
}
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.Linq;
class Program
{
static void Main()
{
BeerBottles().Take(99).ToList().ForEach(Console.WriteLine);
}
static IEnumerable<String> BeerBottles()
{
int i = 100;
String f = "{0}, {1}. Take one down, pass it around, {2}";
Func<int, bool, String> booze = (c , b) =>
String.Format("{0} bottle{1} of beer{2}", c > 0 ? c.ToString() : "no more", (c == 1 ? "" : "s"), b ? " on the wall" : "");
while (--i >= 1)
yield return String.Format(f, booze(i, true), booze(i, false), booze(i - 1, true));
}
}

View file

@ -0,0 +1,131 @@
string[] bottles = { "80 Shilling",
"Abita Amber",
"Adams Broadside Ale",
"Altenmünster Premium",
"August Schell's SnowStorm",
"Bah Humbug! Christmas Ale",
"Beck's Oktoberfest",
"Belhaven Wee Heavy",
"Bison Chocolate Stout",
"Blue Star Wheat Beer",
"Bridgeport Black Strap Stout",
"Brother Thelonius Belgian-Style Abbey Ale",
"Capital Blonde Doppelbock",
"Carta Blanca",
"Celis Raspberry Wheat",
"Christian Moerlein Select Lager",
"Corona",
"Czechvar",
"Delirium Tremens",
"Diamond Bear Southern Blonde",
"Don De Dieu",
"Eastside Dark",
"Eliot Ness",
"Flying Dog K-9 Cruiser Altitude Ale",
"Fuller's London Porter",
"Gaffel Kölsch",
"Golden Horseshoe",
"Guinness Pub Draught",
"Hacker-Pschorr Weisse",
"Hereford & Hops Black Spring Double Stout",
"Highland Oatmeal Porter",
"Ipswich Ale",
"Iron City",
"Jack Daniel's Amber Lager",
"Jamaica Sunset India Pale Ale",
"Killian's Red",
"König Ludwig Weiss",
"Kronenbourg 1664",
"Lagunitas Hairy Eyball Ale",
"Left Hand Juju Ginger",
"Locktender Lager",
"Magic Hat Blind Faith",
"Missing Elf Double Bock",
"Muskoka Cream Ale ",
"New Glarus Cherry Stout",
"Nostradamus Bruin",
"Old Devil",
"Ommegang Three Philosophers",
"Paulaner Hefe-Weizen Dunkel",
"Perla Chmielowa Pils",
"Pete's Wicked Springfest",
"Point White Biere",
"Prostel Alkoholfrei",
"Quilmes",
"Rahr's Red",
"Rebel Garnet",
"Rickard's Red",
"Rio Grande Elfego Bock",
"Rogue Brutal Bitter",
"Roswell Alien Amber Ale",
"Russian River Pliny The Elder",
"Samuel Adams Blackberry Witbier",
"Samuel Smith's Taddy Porter",
"Schlafly Pilsner",
"Sea Dog Wild Blueberry Wheat Ale",
"Sharp's",
"Shiner 99",
"Sierra Dorada",
"Skullsplitter Orkney Ale",
"Snake Chaser Irish Style Stout",
"St. Arnold Bock",
"St. Peter's Cream Stout",
"Stag",
"Stella Artois",
"Stone Russian Imperial Stout",
"Sweetwater Happy Ending Imperial Stout",
"Taiwan Gold Medal",
"Terrapin Big Hoppy Monster",
"Thomas Hooker American Pale Ale",
"Tie Die Red Ale",
"Toohey's Premium",
"Tsingtao",
"Ugly Pug Black Lager",
"Unibroue Qatre-Centieme",
"Victoria Bitter",
"Voll-Damm Doble Malta",
"Wailing Wench Ale",
"Warsteiner Dunkel",
"Wellhead Crude Oil Stout",
"Weyerbacher Blithering Idiot Barley-Wine Style Ale",
"Wild Boar Amber",
"Würzburger Oktoberfest",
"Xingu Black Beer",
"Yanjing",
"Younger's Tartan Special",
"Yuengling Black & Tan",
"Zagorka Special",
"Zig Zag River Lager",
"Zywiec" };
int bottlesLeft = 99;
const int FIRST_LINE_SINGULAR = 98;
const int FINAL_LINE_SINGULAR = 97;
string firstLine = "";
string finalLine = "";
for (int i = 0; i < 99; i++)
{
firstLine = bottlesLeft.ToString() + " bottle";
if (i != FIRST_LINE_SINGULAR)
firstLine += "s";
firstLine += " of beer on the wall, " + bottlesLeft.ToString() + " bottle";
if (i != FIRST_LINE_SINGULAR)
firstLine += "s";
firstLine += " of beer";
Console.WriteLine(firstLine);
Console.WriteLine("Take the " + bottles[i] + " down, pass it around,");
bottlesLeft--;
finalLine = bottlesLeft.ToString() + " bottle";
if (i != FINAL_LINE_SINGULAR)
finalLine += "s";
finalLine += " of beer on the wall!";
Console.WriteLine(finalLine);
Console.WriteLine();
Console.ReadLine();
}

View file

@ -0,0 +1,19 @@
(deffacts beer-bottles
(bottles 99))
(deffunction bottle-count
(?count)
(switch ?count
(case 0 then "No more bottles of beer")
(case 1 then "1 more bottle of beer")
(default (str-cat ?count " bottles of beer"))))
(defrule stanza
?bottles <- (bottles ?count)
=>
(retract ?bottles)
(printout t (bottle-count ?count) " on the wall," crlf)
(printout t (bottle-count ?count) "." crlf)
(printout t "Take one down, pass it around," crlf)
(printout t (bottle-count (- ?count 1)) " on the wall." crlf crlf)
(if (> ?count 1) then (assert (bottles (- ?count 1)))))

View file

@ -0,0 +1,122 @@
identification division.
program-id. ninety-nine.
environment division.
data division.
working-storage section.
01 counter pic 99.
88 no-bottles-left value 0.
88 one-bottle-left value 1.
01 parts-of-counter redefines counter.
05 tens pic 9.
05 digits pic 9.
01 after-ten-words.
05 filler pic x(7) value spaces.
05 filler pic x(7) value "Twenty".
05 filler pic x(7) value "Thirty".
05 filler pic x(7) value "Forty".
05 filler pic x(7) value "Fifty".
05 filler pic x(7) value "Sixty".
05 filler pic x(7) value "Seventy".
05 filler pic x(7) value "Eighty".
05 filler pic x(7) value "Ninety".
05 filler pic x(7) value spaces.
01 after-ten-array redefines after-ten-words.
05 atens occurs 10 times pic x(7).
01 digit-words.
05 filler pic x(9) value "One".
05 filler pic x(9) value "Two".
05 filler pic x(9) value "Three".
05 filler pic x(9) value "Four".
05 filler pic x(9) value "Five".
05 filler pic x(9) value "Six".
05 filler pic x(9) value "Seven".
05 filler pic x(9) value "Eight".
05 filler pic x(9) value "Nine".
05 filler pic x(9) value "Ten".
05 filler pic x(9) value "Eleven".
05 filler pic x(9) value "Twelve".
05 filler pic x(9) value "Thirteen".
05 filler pic x(9) value "Fourteen".
05 filler pic x(9) value "Fifteen".
05 filler pic x(9) value "Sixteen".
05 filler pic x(9) value "Seventeen".
05 filler pic x(9) value "Eighteen".
05 filler pic x(9) value "Nineteen".
05 filler pic x(9) value spaces.
01 digit-array redefines digit-words.
05 adigits occurs 20 times pic x(9).
01 number-name pic x(15).
01 stringified pic x(30).
01 outline pic x(50).
01 other-numbers.
03 n pic 999.
03 r pic 999.
procedure division.
100-main section.
100-setup.
perform varying counter from 99 by -1 until no-bottles-left
move spaces to outline
perform 100-show-number
string stringified delimited by "|", space, "of beer on the wall" into outline end-string
display outline end-display
move spaces to outline
string stringified delimited by "|", space, "of beer" into outline end-string
display outline end-display
move spaces to outline
move "Take" to outline
if one-bottle-left
string outline delimited by space, space, "it" delimited by size, space, "|" into outline end-string
else
string outline delimited by space, space, "one" delimited by size, space, "|" into outline end-string
end-if
string outline delimited by "|", "down and pass it round" delimited by size into outline end-string
display outline end-display
move spaces to outline
subtract 1 from counter giving counter end-subtract
perform 100-show-number
string stringified delimited by "|", space, "of beer on the wall" into outline end-string
display outline end-display
add 1 to counter giving counter end-add
display space end-display
end-perform.
display "No more bottles of beer on the wall"
display "No more bottles of beer"
display "Go to the store and buy some more"
display "Ninety-Nine bottles of beer on the wall"
stop run.
100-show-number.
if no-bottles-left
move "No more|" to stringified
else
if counter < 20
string function trim( adigits( counter ) ), "|" into stringified
else
if counter < 100
move spaces to number-name
string atens( tens ) delimited by space, space delimited by size, adigits( digits ) delimited by space into number-name end-string
move function trim( number-name) to stringified
divide counter by 10 giving n remainder r end-divide
if r not = zero
inspect stringified replacing first space by "-"
end-if
inspect stringified replacing first space by "|"
end-if
end-if
end-if.
if one-bottle-left
string stringified delimited by "|", space, "bottle|" delimited by size into stringified end-string
else
string stringified delimited by "|", space, "bottles|" delimited by size into stringified end-string
end-if.
100-end.
end-program.

View file

@ -0,0 +1,104 @@
identification division.
program-id. ninety-nine.
environment division.
data division.
working-storage section.
01 counter pic 99.
88 no-bottles-left value 0.
88 one-bottle-left value 1.
01 parts-of-counter redefines counter.
05 tens pic 9.
05 digits pic 9.
01 after-ten-words.
05 filler pic x(7) value spaces.
05 filler pic x(7) value "Twenty".
05 filler pic x(7) value "Thirty".
05 filler pic x(7) value "Forty".
05 filler pic x(7) value "Fifty".
05 filler pic x(7) value "Sixty".
05 filler pic x(7) value "Seventy".
05 filler pic x(7) value "Eighty".
05 filler pic x(7) value "Ninety".
05 filler pic x(7) value spaces.
01 after-ten-array redefines after-ten-words.
05 atens occurs 10 times pic x(7).
01 digit-words.
05 filler pic x(9) value "One".
05 filler pic x(9) value "Two".
05 filler pic x(9) value "Three".
05 filler pic x(9) value "Four".
05 filler pic x(9) value "Five".
05 filler pic x(9) value "Six".
05 filler pic x(9) value "Seven".
05 filler pic x(9) value "Eight".
05 filler pic x(9) value "Nine".
05 filler pic x(9) value "Ten".
05 filler pic x(9) value "Eleven".
05 filler pic x(9) value "Twelve".
05 filler pic x(9) value "Thirteen".
05 filler pic x(9) value "Fourteen".
05 filler pic x(9) value "Fifteen".
05 filler pic x(9) value "Sixteen".
05 filler pic x(9) value "Seventeen".
05 filler pic x(9) value "Eighteen".
05 filler pic x(9) value "Nineteen".
05 filler pic x(9) value spaces.
01 digit-array redefines digit-words.
05 adigits occurs 20 times pic x(9).
01 number-name pic x(15).
procedure division.
100-main section.
100-setup.
perform varying counter from 99 by -1 until no-bottles-left
perform 100-show-number
display " of beer on the wall"
perform 100-show-number
display " of beer"
display "Take " with no advancing
if one-bottle-left
display "it " with no advancing
else
display "one " with no advancing
end-if
display "down and pass it round"
subtract 1 from counter giving counter
perform 100-show-number
display " of beer on the wall"
add 1 to counter giving counter
display space
end-perform.
display "No more bottles of beer on the wall"
display "No more bottles of beer"
display "Go to the store and buy some more"
display "Ninety Nine bottles of beer on the wall"
stop run.
100-show-number.
if no-bottles-left
display "No more" with no advancing
else
if counter < 20
display function trim( adigits( counter ) ) with no advancing
else
if counter < 100
move spaces to number-name
string atens( tens ) delimited by space, space delimited by size, adigits( digits ) delimited by space into number-name
display function trim( number-name) with no advancing
end-if
end-if
end-if.
if one-bottle-left
display " bottle" with no advancing
else
display " bottles" with no advancing
end-if.
100-end.
end-program.

View file

@ -0,0 +1,68 @@
/***********************************************************************
* Chapel implementation of "99 bottles of beer"
*
* by Brad Chamberlain and Steve Deitz
* 07/13/2006 in Knoxville airport while waiting for flight home from
* HPLS workshop
* compiles and runs with chpl compiler version 0.3.3211
* for more information, contact: chapel_info@cray.com
*
*
* Notes:
* o as in all good parallel computations, boundary conditions
* constitute the vast bulk of complexity in this code (invite Brad to
* tell you about his zany boundary condition simplification scheme)
* o uses type inference for variables, arguments
* o relies on integer->string coercions
* o uses named argument passing (for documentation purposes only)
***********************************************************************/
// allow executable command-line specification of number of bottles
// (e.g., ./a.out -snumBottles=999999)
config const numBottles = 99;
const numVerses = numBottles+1;
// a domain to describe the space of lyrics
var LyricsSpace: domain(1) = [1..numVerses];
// array of lyrics
var Lyrics: [LyricsSpace] string;
// parallel computation of lyrics array
[verse in LyricsSpace] Lyrics(verse) = computeLyric(verse);
// as in any good parallel language, I/O to stdout is serialized.
// (Note that I/O to a file could be parallelized using a parallel
// prefix computation on the verse strings' lengths with file seeking)
writeln(Lyrics);
// HELPER FUNCTIONS:
fun computeLyric(verseNum) {
var bottleNum = numBottles - (verseNum - 1);
var nextBottle = (bottleNum + numVerses - 1)%numVerses;
return "\n" // disguise space used to separate elements in array I/O
+ describeBottles(bottleNum, startOfVerse=true) + " on the wall, "
+ describeBottles(bottleNum) + ".\n"
+ computeAction(bottleNum)
+ describeBottles(nextBottle) + " on the wall.\n";
}
fun describeBottles(bottleNum, startOfVerse:bool = false) {
// NOTE: bool should not be necessary here (^^^^); working around bug
var bottleDescription = if (bottleNum) then bottleNum:string
else (if startOfVerse then "N"
else "n")
+ "o more";
return bottleDescription
+ " bottle" + (if (bottleNum == 1) then "" else "s")
+ " of beer";
}
fun computeAction(bottleNum) {
return if (bottleNum == 0) then "Go to the store and buy some more, "
else "Take one down and pass it around, ";
}

View file

@ -0,0 +1,225 @@
99 Bottles Of Beer.
Ingredients.
99 bottles
Method.
Loop the bottles.
Put bottles into 1st mixing bowl.
Serve with bottles of beer on the wall.
Clean 1st mixing bowl.
Put bottles into 1st mixing bowl.
Serve with bottles of beer.
Clean 1st mixing bowl.
Serve with Take one down and pass it around.
Clean 1st mixing bowl.
Loop the bottles until looped.
Serve with No more bottles of beer.
Clean 1st mixing bowl.
Pour contents of the 3rd mixing bowl into the 1st baking dish.
Serves 1.
bottles of beer on the wall.
Prints out "n" bottles of beer on the wall.
Ingredients.
108 g lime
97 cups asparagus
119 pinches watercress
32 tablespoons pickles
101 pinches eggplant
104 g huckleberry
116 teaspoons turnip
110 tablespoons nannyberry
111 tablespoons onion
114 tablespoons raspberry
98 g broccoli
102 g feijoa
115 teaspoons squach
10 ml new line
Method.
Put new line into 1st mixing bowl.
Put lime into 2nd mixing bowl.
Put lime into 2nd mixing bowl.
Put asparagus into 2nd mixing bowl.
Put watercress into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put huckleberry into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put nannyberry into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put raspberry into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put broccoli into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put feijoa into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put squach into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put lime into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put broccoli into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Liquify contents of the 2nd mixing bowl.
Pour contents of the 2nd mixing bowl into the baking dish.
Pour contents of the mixing bowl into the baking dish.
Refrigerate for 1 hour.
bottles of beer.
Prints out "n" bottles of beer.
Ingredients.
114 tablespoons raspberry
101 pinches eggplant
98 teaspoons broccoli
32 pinches pickles
102 tablespoons feijoa
111 teaspoons onion
115 cups squach
108 cups lime
116 teaspoons turnip
10 ml new line
Method.
Put new line into 1st mixing bowl.
Put raspberry into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put broccoli into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put feijoa into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put squach into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put lime into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put broccoli into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Liquify contents of the 2nd mixing bowl.
Pour contents of the 2nd mixing bowl into the baking dish.
Pour contents of the mixing bowl into the baking dish.
Refrigerate for 1 hour.
Take one down and pass it around.
Prints out "Take one down and pass it around".
Ingredients.
100 cups dandelion
110 g nannyberry
117 pinches cucumber
111 pinches onion
114 pinches raspberry
97 g asparagus
32 tablespoons pickles
116 pinches turnip
105 g chestnut
115 g squach
112 g pumpkin
119 cups watercress
101 g eggplant
107 g kale
84 cups tomatoe
10 ml new line
Method.
Put new line into 3rd mixing bowl.
Put dandelion into 2nd mixing bowl.
Put nannyberry into 2nd mixing bowl.
Put cucumber into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put raspberry into 2nd mixing bowl.
Put asparagus into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put chestnut into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put squach into 2nd mixing bowl.
Put squach into 2nd mixing bowl.
Put asparagus into 2nd mixing bowl.
Put pumpkin into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put dandelion into 2nd mixing bowl.
Put nannyberry into 2nd mixing bowl.
Put asparagus into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put nannyberry into 2nd mixing bowl.
Put watercress into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put dandelion into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put nannyberry into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put kale into 2nd mixing bowl.
Put asparagus into 2nd mixing bowl.
Put tomatoe into 2nd mixing bowl.
Liquify contents of the 2nd mixing bowl.
Pour contents of the 2nd mixing bowl into the baking dish.
Pour contents of the 3rd mixing bowl into the baking dish.
Refrigerate for 1 hour.
No more bottles of beer.
Prints out "No more bottles of beer".
Ingredients.
114 pinches raspberry
101 teaspoons eggplant
98 cups broccoli
32 tablespoons pickles
102 pinches feijoa
111 cups onion
115 tablespoons squach
108 tablespoons lime
116 pinches turnip
109 cups mushrooms
78 g nectarine
10 ml new line
Method.
Put new line into 3rd mixing bowl.
Put new line into 2nd mixing bowl.
Put raspberry into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put broccoli into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put feijoa into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put squach into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put lime into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put turnip into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put broccoli into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put eggplant into 2nd mixing bowl.
Put raspberry into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put mushrooms into 2nd mixing bowl.
Put pickles into 2nd mixing bowl.
Put onion into 2nd mixing bowl.
Put nectarine into 2nd mixing bowl.
Liquify contents of the 2nd mixing bowl.
Pour contents of the 2nd mixing bowl into the baking dish.
Pour contents of the 3rd mixing bowl into the baking dish.
Refrigerate for 1 hour.

View file

@ -0,0 +1,20 @@
/* A few options here: I could give n type Int; or specify that n is of any
numeric type; but here I just let it go -- that way it'll work with anything
that compares with 1 and that printTo knows how to convert to a string. And
all checked at compile time, remember. */
getRound(n) {
var s = String();
var bottle = if (n == 1) " bottle " else " bottles ";
printTo(s,
n, bottle, "of beer on the wall\n",
n, bottle, "of beer\n",
"take one down, pass it around\n",
n, bottle, "of beer on the wall!\n");
return s;
}
main() {
println(join("\n", mapped(getRound, reversed(range(100)))));
}

View file

@ -0,0 +1,9 @@
<cfoutput>
<cfloop index="x" from="99" to="0" step="-1">
<cfset plur = iif(x is 1,"",DE("s"))>
#x# bottle#plur# of beer on the wall<br>
#x# bottle#plur# of beer<br>
Take one down, pass it around<br>
#iif(x is 1,DE("No more"),"x-1")# bottle#iif(x is 2,"",DE("s"))# of beer on the wall<br><br>
</cfloop>
</cfoutput>

View file

@ -0,0 +1,6 @@
<cfscript>
for (x=99; x gte 1; x--) {
plur = iif(x==1,'',DE('s'));
WriteOutput("#x# bottle#plur# of beer on the wall<br>#x# bottle#plur# of beer<br>Take one down, pass it around<br>#iif(x is 1,DE('No more'),'x-1')# bottle#iif(x is 2,'',DE('s'))# of beer on the wall<br><br>");
}
</cfscript>

View file

@ -0,0 +1,6 @@
(defun bottles (x)
(loop for bottles from x downto 1
do (format t "~a bottle~:p of beer on the wall
~:*~a bottle~:p of beer
Take one down, pass it around
~a bottle~:p of beer on the wall~2%" bottles (1- bottles))))

View file

@ -0,0 +1 @@
(bottles 99)

View file

@ -0,0 +1,20 @@
import std.stdio;
void main() {
int bottles = 99;
while (bottles > 1) {
writeln(bottles, " bottles of beer on the wall,");
writeln(bottles, " bottles of beer.");
writeln("Take one down, pass it around,");
if (--bottles > 1) {
writeln(bottles, " bottles of beer on the wall.\n");
}
}
writeln("1 bottle of beer on the wall.\n");
writeln("No more bottles of beer on the wall,");
writeln("no more bottles of beer.");
writeln("Go to the store and buy some more,");
writeln("99 bottles of beer on the wall.");
}

View file

@ -0,0 +1,9 @@
main() {
for(int x=99;x>0;x--) {
print("$x bottles of beer on the wall");
print("$x bottles of beer");
print("Take one down, pass it around");
print("${x-1} bottles of beer on the wall");
print("");
}
}

View file

@ -0,0 +1,27 @@
program Hundred_Bottles;
{$APPTYPE CONSOLE}
uses SysUtils;
const C_1_Down = 'Take one down, pass it around' ;
Var i : Integer ;
// As requested, some fun : examples of Delphi basic techniques. Just to make it a bit complex
procedure WriteABottle( BottleNr : Integer ) ;
begin
Writeln(BottleNr, ' bottles of beer on the wall' ) ;
end ;
begin
for i := 99 Downto 1 do begin
WriteABottle(i);
Writeln( Format('%d bottles of beer' , [i] ) ) ;
Writeln( C_1_Down ) ;
WriteABottle(i-1);
Writeln ;
End ;
end.

View file

@ -0,0 +1,14 @@
def bottles(n) {
return switch (n) {
match ==0 { "No bottles" }
match ==1 { "1 bottle" }
match _ { `$n bottles` }
}
}
for n in (1..99).descending() {
println(`${bottles(n)} of beer on the wall,
${bottles(n)} of beer.
Take one down, pass it around,
${bottles(n.previous())} of beer on the wall.
`)
}

View file

@ -0,0 +1,22 @@
program TestProgram type BasicProgram {}
function main()
for (count int from 99 to 1 decrement by 1)
SysLib.writeStdout( bottleStr( count ) :: " of beer on the wall." );
SysLib.writeStdout( bottleStr( count ) :: " of beer." );
SysLib.writeStdout( "Take one down, pass it around." );
SysLib.writeStdout( bottleStr( count - 1) :: " of beer on the wall.\n");
end
end
private function bottleStr( count int in) returns( string )
case ( count )
when ( 1 )
return( "1 bottle" );
when ( 0 )
return( "No more bottles" );
otherwise
return( count :: " bottles" );
end
end
end

View file

@ -0,0 +1,9 @@
open list
beer 1 = "1 bottle of beer on the wall\n1 bottle of beer\nTake one down, pass it around"
beer 0 = "better go to the store and buy some more."
beer v = show v ++ " bottles of beer on the wall\n"
++ show v
++" bottles of beer\nTake one down, pass it around\n"
map beer [99,98..0]

View file

@ -0,0 +1,50 @@
include std/console.e
include std/search.e
function one_or_it( atom n )
if n = 1 then
return "it"
else
return "one"
end if
end function
function numberable( atom n )
if n = 0 then
return "no"
else
return sprintf( "%d", n )
end if
end function
function plural( atom n )
if n != 1 then
return "s"
else
return ""
end if
end function
atom stillDrinking = 1
sequence yn
sequence plurality
sequence numerality
while stillDrinking do
for bottle = 99 to 1 by -1 do
plurality = plural( bottle )
numerality = numberable( bottle )
printf( 1, "%s bottle%s of beer on the wall\n%s bottle%s of beer\n",
{ numerality, plurality, numerality, plurality } )
printf( 1, "Take %s down and pass it round\n", { one_or_it( bottle ) } )
printf( 1, "%s bottle%s of beer on the wall\n\n",
{ numberable( bottle - 1 ), plural( bottle - 1 ) } )
end for
puts( 1, "No more bottles of beer on the wall\nNo more bottles of beer\n" )
puts( 1, "Go to the store and buy some more\n99 bottles of beer on the wall\n" )
puts( 1, "\nKeep drinking? " )
yn = gets(0)
stillDrinking = find_any( "yY", yn )
puts( 1, "\n" )
end while

9
Task/A+B/C++/a+b-1.cpp Normal file
View file

@ -0,0 +1,9 @@
// Standard input-output streams
#include <iostream>
using namespace std;
void main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
}

13
Task/A+B/C++/a+b-2.cpp Normal file
View file

@ -0,0 +1,13 @@
// Input file: input.txt
// Output file: output.txt
#include <fstream>
using namespace std;
int main()
{
ifstream in("input.txt");
ofstream out("output.txt");
int a, b;
in >> a >> b;
out << a + b << endl;
return 0;
}

View file

@ -0,0 +1 @@
(write (+ (read) (read)))

11
Task/A+B/D/a+b-1.d Normal file
View file

@ -0,0 +1,11 @@
import std.stdio, std.conv, std.string;
void main() {
string[] r;
try
r = readln().split();
catch (StdioException e)
r = ["10", "20"];
writeln(to!int(r[0]) + to!int(r[1]));
}

8
Task/A+B/D/a+b-2.d Normal file
View file

@ -0,0 +1,8 @@
import std.stdio, std.conv, std.string;
void main() {
auto fin = File("sum_input.txt", "r");
auto r = fin.readln().split();
auto fout = File("sum_output.txt", "w");
fout.writeln(to!int(r[0]) + to!int(r[1]));
}

3
Task/A+B/DMS/a+b.dms Normal file
View file

@ -0,0 +1,3 @@
number a = GetNumber( "Please input 'a'", a, a ) // prompts for 'a'
number b = GetNumber( "Please input 'b'", b, b ) // prompts for 'b'
Result( a + b + "\n" )

View file

@ -0,0 +1,3 @@
var a := StrToInt(InputBox('A+B', 'Enter 1st number', '0'));
var b := StrToInt(InputBox('A+B', 'Enter 2nd number', '0'));
ShowMessage('Sum is '+IntToStr(a+b));

4
Task/A+B/Deja-Vu/a+b.djv Normal file
View file

@ -0,0 +1,4 @@
0
for k in split " " input:
+ to-num k
print

View file

@ -0,0 +1,14 @@
program SUM;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
s1, s2:string;
begin
ReadLn(s1);
Readln(s2);
Writeln(StrToIntDef(s1, 0) + StrToIntDef(s2,0));
end.

18
Task/A+B/EGL/a+b.egl Normal file
View file

@ -0,0 +1,18 @@
package programs;
// basic program
//
program AplusB type BasicProgram {}
function main()
try
arg1 string = SysLib.getCmdLineArg(1);
arg2 string = SysLib.getCmdLineArg(2);
int1 int = arg1;
int2 int = arg2;
sum int = int1 + int2;
SysLib.writeStdout("sum1: " + sum);
onException(exception AnyException)
SysLib.writeStdout("No valid input. Provide 2 integer numbers as arguments to the program.");
end
end
end

3
Task/A+B/Ela/a+b.ela Normal file
View file

@ -0,0 +1,3 @@
open console list string read
readn() |> string.split " " |> map readStr |> sum

11
Task/A+B/Elena/a+b.elena Normal file
View file

@ -0,0 +1,11 @@
#define std'basic'*.
#define ext'io'*.
#symbol Console =>
[
#var anOutput := __wrap(ELineInput, 'program'input).
#var A := anOutput >> Integer.
#var B := anOutput >> Integer.
'program'output << A + B.
].

View file

@ -0,0 +1,7 @@
>s=lineinput("Two numbers seperated by a blank");
Two numbers seperated by a blank? >4 5
>vs=strtokens(s)
4
5
>vs[1]()+vs[2]()
9

View file

@ -0,0 +1,12 @@
include get.e
function snd(sequence s)
return s[2]
end function
integer a,b
a = snd(get(0))
b = snd(get(0))
printf(1," %d\n",a+b)

View file

@ -0,0 +1,7 @@
class Abs {
public:
virtual int method1(double value) = 0;
virtual int add(int a, int b){
return a+b;
}
};

View file

@ -0,0 +1,9 @@
abstract class Class1
{
public abstract void method1();
public int method2()
{
return 0;
}
}

View file

@ -0,0 +1,13 @@
(defgeneric kar (kons)
(:documentation "Return the kar of a kons."))
(defgeneric kdr (kons)
(:documentation "Return the kdr of a kons."))
(defun konsp (object &aux (args (list object)))
"True if there are applicable methods for kar and kdr on object."
(not (or (endp (compute-applicable-methods #'kar args))
(endp (compute-applicable-methods #'kdr args)))))
(deftype kons ()
'(satisfies konsp))

View file

@ -0,0 +1,10 @@
(defmethod kar ((cons cons))
(car cons))
(defmethod kdr ((cons cons))
(cdr cons))
(konsp (cons 1 2)) ; => t
(typep (cons 1 2) 'kons) ; => t
(kar (cons 1 2)) ; => 1
(kdr (cons 1 2)) ; => 2

View file

@ -0,0 +1,11 @@
(defmethod kar ((n integer))
1)
(defmethod kdr ((n integer))
(if (zerop n) nil
(1- n)))
(konsp 45) ; => t
(typep 45 'kons) ; => t
(kar 45) ; => 1
(kdr 45) ; => 44

View file

@ -0,0 +1,27 @@
import std.stdio;
class Foo {
// abstract methods can have an implementation for
// use in super calls.
abstract void foo() {
writeln("Test");
}
}
interface Bar {
void bar();
// Final interface methods are allowed.
final int spam() { return 1; }
}
class Baz : Foo, Bar { // Super class must come first.
override void foo() {
writefln("Meep");
super.foo();
}
void bar() {}
}
void main() {}

View file

@ -0,0 +1,3 @@
TSomeClass = class abstract (TObject)
...
end;

View file

@ -0,0 +1,13 @@
type
TMyObject = class(TObject)
public
procedure AbstractFunction; virtual; abstract; // Your virtual abstract function to overwrite in descendant
procedure ConcreteFunction; virtual; // Concrete function calling the abstract function
end;
implementation
procedure TMyObject.ConcreteFunction;
begin
AbstractFunction; // Calling the abstract function
end;

View file

@ -0,0 +1,3 @@
interface Foo {
to bar(a :int, b :int)
}

View file

@ -0,0 +1,9 @@
interface Foo guards FooStamp {
to bar(a :int, b :int)
}
def x implements FooStamp {
to bar(a :int, b :int) {
return a - b
}
}

View file

@ -0,0 +1,57 @@
#include <iostream>
class Acc
{
public:
Acc(int init)
: _type(intType)
, _intVal(init)
{}
Acc(float init)
: _type(floatType)
, _floatVal(init)
{}
int operator()(int x)
{
if( _type == intType )
{
_intVal += x;
return _intVal;
}
else
{
_floatVal += x;
return static_cast<int>(_floatVal);
}
}
float operator()(float x)
{
if( _type == intType )
{
_floatVal = _intVal + x;
_type = floatType;
return _floatVal;
}
else
{
_floatVal += x;
return _floatVal;
}
}
private:
enum {floatType, intType} _type;
float _floatVal;
int _intVal;
};
int main()
{
Acc a(1);
a(5);
Acc(3);
std::cout << a(2.3f);
return 0;
}

View file

@ -0,0 +1,17 @@
#include <iostream>
#include <functional>
template <typename T>
std::function<T(T)> makeAccumulator(T sum) {
return [=](T increment) mutable {
return sum += increment;
};
}
int main() {
auto acc = makeAccumulator<float>(1);
acc(5);
makeAccumulator(3);
std::cout << acc(2.3) << std::endl;
return 0;
}

View file

@ -0,0 +1,13 @@
acc(n) {
return (m) => {
n = n + m;
return n;
};
}
main() {
var x = acc(1.0);
x(5);
acc(3);
println(x(2.3)); // Prints “8.300000000000001”.
}

View file

@ -0,0 +1,2 @@
var y = acc(Vector[Char]("Hello"));
println(y(" World!")); // Prints "Hello World!”.

View file

@ -0,0 +1,6 @@
[N | Numeric?(N)] acc(n: N) {
return (m) => {
n = n + m;
return n;
};
}

View file

@ -0,0 +1,3 @@
(defun accumulator (sum)
(lambda (n)
(setf sum (+ sum n))))

View file

@ -0,0 +1,4 @@
(defvar x (accumulator 1))
(funcall x 5)
(accumulator 3)
(funcall x 2.3)

View file

@ -0,0 +1,11 @@
void main() {
auto x = acc(1);
x(5);
acc(3);
writeln(x(2.3));
}
auto acc(U = real, T)(T initvalue) { // U is type of the accumulator
auto accum = cast(U)initvalue ;
return (U n) { return accum += n ; } ;
}

View file

@ -0,0 +1,3 @@
def foo(var x) {
return fn y { x += y }
}

View file

@ -0,0 +1,17 @@
#define std'dictionary'*.
#define std'basic'*.
#define sys'dynamics'*.
#symbol NewAccumulator : aValue =
#join (Variable::aValue) { eval : aValue [ $self content'set:($self + aValue). ] }.
#symbol Program =
[
#var x := NewAccumulator::1.
x::5.
NewAccumulator::3.
'program'Output << x::2.3r.
].

View file

@ -0,0 +1,18 @@
#include <iostream>
using namespace std;
long ackermann(long x, long y)
{
if (x == 0) return y+1;
else if (y == 0) return ackermann(x-1, 1);
else return ackermann(x-1, ackermann(x, y-1));
}
int main()
{
long x,y;
cout << "x ve y..:";
cin>>x;
cin>>y;
cout<<ackermann(x,y);
return 0;
}

View file

@ -0,0 +1,32 @@
using System;
class Program
{
public static long Ackermann(long m, long n)
{
if(m > 0)
{
if (n > 0)
return Ackermann(m - 1, Ackermann(m, n - 1));
else if (n == 0)
return Ackermann(m - 1, 1);
}
else if(m == 0)
{
if(n >= 0)
return n + 1;
}
throw new System.ArgumentOutOfRangeException();
}
static void Main()
{
for (long m = 0; m <= 3; ++m)
{
for (long n = 0; n <= 4; ++n)
{
Console.WriteLine("Ackermann({0}, {1}) = {2}", m, n, Ackermann(m, n));
}
}
}
}

View file

@ -0,0 +1,10 @@
(deffunction ackerman
(?m ?n)
(if (= 0 ?m)
then (+ ?n 1)
else (if (= 0 ?n)
then (ackerman (- ?m 1) 1)
else (ackerman (- ?m 1) (ackerman ?m (- ?n 1)))
)
)
)

View file

@ -0,0 +1,68 @@
(deffacts solve-items
(solve 0 4)
(solve 1 4)
(solve 2 4)
(solve 3 4)
)
(defrule acker-m-0
?compute <- (compute 0 ?n)
=>
(retract ?compute)
(assert (ackerman 0 ?n (+ ?n 1)))
)
(defrule acker-n-0-pre
(compute ?m&:(> ?m 0) 0)
(not (ackerman =(- ?m 1) 1 ?))
=>
(assert (compute (- ?m 1) 1))
)
(defrule acker-n-0
?compute <- (compute ?m&:(> ?m 0) 0)
(ackerman =(- ?m 1) 1 ?val)
=>
(retract ?compute)
(assert (ackerman ?m 0 ?val))
)
(defrule acker-m-n-pre-1
(compute ?m&:(> ?m 0) ?n&:(> ?n 0))
(not (ackerman ?m =(- ?n 1) ?))
=>
(assert (compute ?m (- ?n 1)))
)
(defrule acker-m-n-pre-2
(compute ?m&:(> ?m 0) ?n&:(> ?n 0))
(ackerman ?m =(- ?n 1) ?newn)
(not (ackerman =(- ?m 1) ?newn ?))
=>
(assert (compute (- ?m 1) ?newn))
)
(defrule acker-m-n
?compute <- (compute ?m&:(> ?m 0) ?n&:(> ?n 0))
(ackerman ?m =(- ?n 1) ?newn)
(ackerman =(- ?m 1) ?newn ?val)
=>
(retract ?compute)
(assert (ackerman ?m ?n ?val))
)
(defrule acker-solve
(solve ?m ?n)
(not (compute ?m ?n))
(not (ackerman ?m ?n ?))
=>
(assert (compute ?m ?n))
)
(defrule acker-solved
?solve <- (solve ?m ?n)
(ackerman ?m ?n ?result)
=>
(retract ?solve)
(printout t "A(" ?m "," ?n ") = " ?result crlf)
)

View file

@ -0,0 +1,8 @@
ackermann(m, n) {
if(m == 0)
return n + 1;
if(n == 0)
return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n - 1));
}

View file

@ -0,0 +1,4 @@
(defun ackermann (m n)
(cond ((zerop m) (1+ n))
((zerop n) (ackermann (1- m) 1))
(t (ackermann (1- m) (ackermann m (1- n))))))

Some files were not shown because too many files have changed in this diff Show more