diff options
author | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
commit | 7fafbc95c0c963438197c9a43fe893c4ea6fe759 (patch) | |
tree | 5c9ceda4bdc5260236a230554b9ed56b8c0cdbd3 /Demo/scripts | |
parent | 6f17e2df29a865a29447531e89fb22be710e382d (diff) | |
download | cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.zip cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.gz cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.bz2 |
More cleanup: Move some demos into a dedicated Tools/demo dir, move 2to3 demo to Tools, and remove all the other Demo content.
Diffstat (limited to 'Demo/scripts')
-rw-r--r-- | Demo/scripts/README | 22 | ||||
-rwxr-xr-x | Demo/scripts/beer.py | 20 | ||||
-rwxr-xr-x | Demo/scripts/fact.py | 49 | ||||
-rwxr-xr-x | Demo/scripts/markov.py | 121 | ||||
-rwxr-xr-x | Demo/scripts/queens.py | 85 | ||||
-rwxr-xr-x | Demo/scripts/unbirthday.py | 106 |
6 files changed, 0 insertions, 403 deletions
diff --git a/Demo/scripts/README b/Demo/scripts/README deleted file mode 100644 index b80704c..0000000 --- a/Demo/scripts/README +++ /dev/null @@ -1,22 +0,0 @@ -This directory contains a collection of executable Python scripts. - -See also the Tools/scripts directory! - -beer.py Print the classic 'bottles of beer' list -eqfix.py Fix .py files to use the correct equality test operator -fact.py Factorize numbers -find-uname.py Search for Unicode characters using regexps -from.py Summarize mailbox -lpwatch.py Watch BSD line printer queues -makedir.py Like mkdir -p -markov.py Markov chain simulation of words or characters -mboxconvert.py Convert MH or MMDF mailboxes to unix mailbox format -morse.py Produce morse code (as an AIFF file) -newslist.py List all newsgroups on a NNTP server as HTML pages -pi.py Print all digits of pi -- given enough time and memory -pp.py Emulate some Perl command line options -primes.py Print prime numbers -queens.py Dijkstra's solution to Wirth's "N Queens problem" -script.py Equivalent to BSD script(1) -- by Steen Lumholt -unbirthday.py Print unbirthday count -update.py Update a bunch of files according to a script diff --git a/Demo/scripts/beer.py b/Demo/scripts/beer.py deleted file mode 100755 index 56eec7b..0000000 --- a/Demo/scripts/beer.py +++ /dev/null @@ -1,20 +0,0 @@ -#! /usr/bin/env python3 - -# By GvR, demystified after a version by Fredrik Lundh. - -import sys - -n = 100 -if sys.argv[1:]: - n = int(sys.argv[1]) - -def bottle(n): - if n == 0: return "no more bottles of beer" - if n == 1: return "one bottle of beer" - return str(n) + " bottles of beer" - -for i in range(n, 0, -1): - print(bottle(i), "on the wall,") - print(bottle(i) + ".") - print("Take one down, pass it around,") - print(bottle(i-1), "on the wall.") diff --git a/Demo/scripts/fact.py b/Demo/scripts/fact.py deleted file mode 100755 index 2a3bef2..0000000 --- a/Demo/scripts/fact.py +++ /dev/null @@ -1,49 +0,0 @@ -#! /usr/bin/env python3 - -# Factorize numbers. -# The algorithm is not efficient, but easy to understand. -# If there are large factors, it will take forever to find them, -# because we try all odd numbers between 3 and sqrt(n)... - -import sys -from math import sqrt - -def fact(n): - if n < 1: - raise ValueError('fact() argument should be >= 1') - if n == 1: - return [] # special case - res = [] - # Treat even factors special, so we can use i += 2 later - while n % 2 == 0: - res.append(2) - n //= 2 - # Try odd numbers up to sqrt(n) - limit = sqrt(n+1) - i = 3 - while i <= limit: - if n % i == 0: - res.append(i) - n //= i - limit = sqrt(n+1) - else: - i += 2 - if n != 1: - res.append(n) - return res - -def main(): - if len(sys.argv) > 1: - source = sys.argv[1:] - else: - source = iter(input, '') - for arg in source: - try: - n = int(arg) - except ValueError: - print(arg, 'is not an integer') - else: - print(n, fact(n)) - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/markov.py b/Demo/scripts/markov.py deleted file mode 100755 index 7c08bdb..0000000 --- a/Demo/scripts/markov.py +++ /dev/null @@ -1,121 +0,0 @@ -#! /usr/bin/env python3 - -class Markov: - def __init__(self, histsize, choice): - self.histsize = histsize - self.choice = choice - self.trans = {} - - def add(self, state, next): - self.trans.setdefault(state, []).append(next) - - def put(self, seq): - n = self.histsize - add = self.add - add(None, seq[:0]) - for i in range(len(seq)): - add(seq[max(0, i-n):i], seq[i:i+1]) - add(seq[len(seq)-n:], None) - - def get(self): - choice = self.choice - trans = self.trans - n = self.histsize - seq = choice(trans[None]) - while True: - subseq = seq[max(0, len(seq)-n):] - options = trans[subseq] - next = choice(options) - if not next: - break - seq += next - return seq - - -def test(): - import sys, random, getopt - args = sys.argv[1:] - try: - opts, args = getopt.getopt(args, '0123456789cdwq') - except getopt.error: - print('Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0]) - print('Options:') - print('-#: 1-digit history size (default 2)') - print('-c: characters (default)') - print('-w: words') - print('-d: more debugging output') - print('-q: no debugging output') - print('Input files (default stdin) are split in paragraphs') - print('separated blank lines and each paragraph is split') - print('in words by whitespace, then reconcatenated with') - print('exactly one space separating words.') - print('Output consists of paragraphs separated by blank') - print('lines, where lines are no longer than 72 characters.') - sys.exit(2) - histsize = 2 - do_words = False - debug = 1 - for o, a in opts: - if '-0' <= o <= '-9': histsize = int(o[1:]) - if o == '-c': do_words = False - if o == '-d': debug += 1 - if o == '-q': debug = 0 - if o == '-w': do_words = True - if not args: - args = ['-'] - - m = Markov(histsize, random.choice) - try: - for filename in args: - if filename == '-': - f = sys.stdin - if f.isatty(): - print('Sorry, need stdin from file') - continue - else: - f = open(filename, 'r') - if debug: print('processing', filename, '...') - text = f.read() - f.close() - paralist = text.split('\n\n') - for para in paralist: - if debug > 1: print('feeding ...') - words = para.split() - if words: - if do_words: - data = tuple(words) - else: - data = ' '.join(words) - m.put(data) - except KeyboardInterrupt: - print('Interrupted -- continue with data read so far') - if not m.trans: - print('No valid input files') - return - if debug: print('done.') - - if debug > 1: - for key in m.trans.keys(): - if key is None or len(key) < histsize: - print(repr(key), m.trans[key]) - if histsize == 0: print(repr(''), m.trans['']) - print() - while True: - data = m.get() - if do_words: - words = data - else: - words = data.split() - n = 0 - limit = 72 - for w in words: - if n + len(w) > limit: - print() - n = 0 - print(w, end=' ') - n += len(w) + 1 - print() - print() - -if __name__ == "__main__": - test() diff --git a/Demo/scripts/queens.py b/Demo/scripts/queens.py deleted file mode 100755 index ffd4bea..0000000 --- a/Demo/scripts/queens.py +++ /dev/null @@ -1,85 +0,0 @@ -#! /usr/bin/env python3 - -"""N queens problem. - -The (well-known) problem is due to Niklaus Wirth. - -This solution is inspired by Dijkstra (Structured Programming). It is -a classic recursive backtracking approach. - -""" - -N = 8 # Default; command line overrides - -class Queens: - - def __init__(self, n=N): - self.n = n - self.reset() - - def reset(self): - n = self.n - self.y = [None] * n # Where is the queen in column x - self.row = [0] * n # Is row[y] safe? - self.up = [0] * (2*n-1) # Is upward diagonal[x-y] safe? - self.down = [0] * (2*n-1) # Is downward diagonal[x+y] safe? - self.nfound = 0 # Instrumentation - - def solve(self, x=0): # Recursive solver - for y in range(self.n): - if self.safe(x, y): - self.place(x, y) - if x+1 == self.n: - self.display() - else: - self.solve(x+1) - self.remove(x, y) - - def safe(self, x, y): - return not self.row[y] and not self.up[x-y] and not self.down[x+y] - - def place(self, x, y): - self.y[x] = y - self.row[y] = 1 - self.up[x-y] = 1 - self.down[x+y] = 1 - - def remove(self, x, y): - self.y[x] = None - self.row[y] = 0 - self.up[x-y] = 0 - self.down[x+y] = 0 - - silent = 0 # If true, count solutions only - - def display(self): - self.nfound = self.nfound + 1 - if self.silent: - return - print('+-' + '--'*self.n + '+') - for y in range(self.n-1, -1, -1): - print('|', end=' ') - for x in range(self.n): - if self.y[x] == y: - print("Q", end=' ') - else: - print(".", end=' ') - print('|') - print('+-' + '--'*self.n + '+') - -def main(): - import sys - silent = 0 - n = N - if sys.argv[1:2] == ['-n']: - silent = 1 - del sys.argv[1] - if sys.argv[1:]: - n = int(sys.argv[1]) - q = Queens(n) - q.silent = silent - q.solve() - print("Found", q.nfound, "solutions.") - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/unbirthday.py b/Demo/scripts/unbirthday.py deleted file mode 100755 index b3c7d23..0000000 --- a/Demo/scripts/unbirthday.py +++ /dev/null @@ -1,106 +0,0 @@ -#! /usr/bin/env python3 - -# Calculate your unbirthday count (see Alice in Wonderland). -# This is defined as the number of days from your birth until today -# that weren't your birthday. (The day you were born is not counted). -# Leap years make it interesting. - -import sys -import time -import calendar - -def main(): - if sys.argv[1:]: - year = int(sys.argv[1]) - else: - year = int(input('In which year were you born? ')) - if 0 <= year < 100: - print("I'll assume that by", year, end=' ') - year = year + 1900 - print('you mean', year, 'and not the early Christian era') - elif not (1850 <= year <= time.localtime()[0]): - print("It's hard to believe you were born in", year) - return - - if sys.argv[2:]: - month = int(sys.argv[2]) - else: - month = int(input('And in which month? (1-12) ')) - if not (1 <= month <= 12): - print('There is no month numbered', month) - return - - if sys.argv[3:]: - day = int(sys.argv[3]) - else: - day = int(input('And on what day of that month? (1-31) ')) - if month == 2 and calendar.isleap(year): - maxday = 29 - else: - maxday = calendar.mdays[month] - if not (1 <= day <= maxday): - print('There are no', day, 'days in that month!') - return - - bdaytuple = (year, month, day) - bdaydate = mkdate(bdaytuple) - print('You were born on', format(bdaytuple)) - - todaytuple = time.localtime()[:3] - todaydate = mkdate(todaytuple) - print('Today is', format(todaytuple)) - - if bdaytuple > todaytuple: - print('You are a time traveler. Go back to the future!') - return - - if bdaytuple == todaytuple: - print('You were born today. Have a nice life!') - return - - days = todaydate - bdaydate - print('You have lived', days, 'days') - - age = 0 - for y in range(year, todaytuple[0] + 1): - if bdaytuple < (y, month, day) <= todaytuple: - age = age + 1 - - print('You are', age, 'years old') - - if todaytuple[1:] == bdaytuple[1:]: - print('Congratulations! Today is your', nth(age), 'birthday') - print('Yesterday was your', end=' ') - else: - print('Today is your', end=' ') - print(nth(days - age), 'unbirthday') - -def format(date): - (year, month, day) = date - return '%d %s %d' % (day, calendar.month_name[month], year) - -def nth(n): - if n == 1: return '1st' - if n == 2: return '2nd' - if n == 3: return '3rd' - return '%dth' % n - -def mkdate(date): - # January 1st, in 0 A.D. is arbitrarily defined to be day 1, - # even though that day never actually existed and the calendar - # was different then... - (year, month, day) = date - days = year*365 # years, roughly - days = days + (year+3)//4 # plus leap years, roughly - days = days - (year+99)//100 # minus non-leap years every century - days = days + (year+399)//400 # plus leap years every 4 centirues - for i in range(1, month): - if i == 2 and calendar.isleap(year): - days = days + 29 - else: - days = days + calendar.mdays[i] - days = days + day - return days - -if __name__ == "__main__": - main() |