Mainly Tech projects on Python and Electronic Design Automation.

Tuesday, March 27, 2007

Wheel of time...

Just read Invasion Of The Dynamic Language Weenies it seems that what goes around, comes around, in that here we have a Java programmer decrying an article on dynamic languages, when years earlier I could read similar articles from C++ adherents about this new Java thing from Sun. java must be cool. Java had its culture. Interminable articles on run-anywhere code and late night drinking (of high strength coffee).

Excuse me while I fetch a lemon - some crocodile tears are called for. ;-)

"The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again".

Robert Jordan - "The Wheel of Time".

Saturday, March 24, 2007

Bash style environment variable expander in Python

At work we have a certain config file that has bash/tcsh style embedded environment variable references of two styles:
${VARNAME}
and
$VARNAME

I thought I would try my hand at a concise python expander for the syntax and came up with:


bash$ python -c '
import sys,os,re
s=sys.stdin.read()
s1 = re.subn(r"\$([a-z_A-Z][A-Za-z_0-9\-]*)", r"%(\1)s", s)[0]
s2 = re.subn(r"\${([^}]+)}", r"%(\1)s", s1)[0]
print s2 % os.environ ' < in_file > expanded_file

Sunday, March 04, 2007

Fizz-Buzz.py

I am having to set some extra work at home for my ten year old son. I found information on a game Fizz-Buzz and introduced it to my son, only to find that he had already played a variant at school.

Oh well.

By that time I had already created a python program to check his answers (written in simplistic style as I intend one day to get all of my kids to learn Python).

'''
Fizz-Buzz.py

Fizz-Buzz Game.
Instructions for two players, 3-5 game. (Easiest).

1. Players take it in turns counting from one to ninety-nine.
(Don't take too much or too little time between numbers
- try for a steady pace)
2. Player must say Fizz-Buzz instead of their number if the number is a
multiple of three and of five.
3. Otherwise, player must say Fizz instead of their number if the number
is a multiple of three.
4. Otherwise, player must say Buzz instead of their number if the number
is a multiple of Five.

Digit version
Use basic instructions 1-4 as above, then add the following rules:

5. Otherwise, player must say Fizz for each digit in the number that is
a three,
And player must also say Buzz for each digit in the number that is a
five.
(for example, say Fizz-one instead of 31, Fizz-two instead of thirty
two)
6. Remember, the rules must be applied in order so thirty three is
actually Fizz and not Fizz-Fizz as it is first a multiple of three
'''

fizz = 3
buzz = 5
digits_version = True

for tens in (0,1,2,3,4,5,6,7,8,9):
for units in (0,1,2,3,4,5,6,7,8,9):
number = tens*10 + units
if number == 0:
pass
else:
print "For", number, "Say",
if (number % fizz) == 0 and (number % buzz) == 0:
print "FizzBuzz"
elif (number % fizz) == 0:
print "Fizz"
elif (number % buzz) == 0:
print "Buzz"
elif digits_version:
saytens = tens
sayunits = units
digits_as_fizzbuzz = False
if tens == fizz:
saytens = "Fizz"
digits_as_fizzbuzz = True
if units == fizz:
sayunits = "Fizz"
digits_as_fizzbuzz = True
if tens == buzz:
saytens = "Buzz"
digits_as_fizzbuzz = True
if units == buzz:
sayunits = "Buzz"
digits_as_fizzbuzz = True
if digits_as_fizzbuzz:
print saytens,sayunits
else:
print number
else:
print number

Followers

Subscribe Now: google

Add to Google Reader or Homepage

Go deh too!

whos.amung.us

Blog Archive