24
Jump to navigation
Jump to search
24 is an esoteric programming language that only works if the last 2 digits of the current year are before or equal to 24. All it does is print "ham n eggs" if the year fits the previously talked about requirements.
Scratch interpreter
-
The interpreter
Common Lisp implementation
The following implementation in Common Lisp can be optionally driven by a user input or an automatic detection of the current year:
(defun interpret-24 (&key (interactive-p NIL)) "Launches the 24 interpreter, either evaluating the current year, or if INTERACTIVE-P is true, queries the standard input for a year to indagate." (declare (type boolean interactive-p)) (let ((probed-year (if interactive-p (loop do (format T "~&Please enter a year: ") (finish-output) (let ((input (read-line NIL NIL ""))) (declare (type string input)) (clear-input) (handler-case (return (parse-integer input)) (error () (format T "~&The input ~s is no valid year." input))))) (nth-value 5 (get-decoded-time))))) (declare (type integer probed-year)) (let ((year-as-string (format NIL "~a" probed-year))) (declare (type string year-as-string)) (let ((last-two-year-digits (parse-integer (subseq year-as-string (max 0 (- (length year-as-string) 2)) (length year-as-string))))) (declare (type (integer 0 99) last-two-year-digits)) (when (<= last-two-year-digits 24) (format T "~&ham n eggs"))))))
Python Implementation
from datetime import * year=datetime.now().year if year%100<25: print('ham n eggs')
An alternative that allows the user to specify the year using the -a
argument.
import sys from datetime import * year=datetime.now().year if len(sys.argv)>1 and sys.argv[-1]=='-a': year=input('Enter year: ') if not year.isdigit(): print('Invalid year') sys.exit(1) if year%100<25: print('ham n eggs')