[Verba] Meeting Summary

Stuart D. Gathman stuart at gathman.org
Thu Jan 19 00:10:22 EST 2006


I am copying Carol Brown, because Julie says she wants to know what is
going on in the Computer Club.  (If you want to get all the club email,
you can sign up at http://gathman.org/mailman/listinfo/verba or just ask
me to do it for you.)

Only Jillian could make it today.  First, we learned about unit testing, 
and added doctest specifications to our bal() balanced ternary function.  
It handled positive numbers just fine, but flunked negative and zero.  
Jillian fixed the program so that it passed the doctests, and the
result is attached.

Notice how the positive and negative cases are handled exactly the same.
You take the negative of a balanced ternary number by changing all '+' to
'-', all '-' to '+' and leaving '0' alone.  Ternary logic is three valued.
'+' = TRUE, '-' = FALSE, '0' = UNKNOWN.  Ternary computers need fewer opcodes
than binary, because, for instance, signed and unsigned arithmetic are the
same, and logical complement is the same as arithmetic complement.

Next, we looked at a "pharoh" game written by one of my daughters in 2001.
(They couldn't spell very well yet.)  After reading the program, and 
playing the game until she understood the logic, Jillian added a new
variable: wisemen, who eat more and don't directly produce food, but
increase yields and reduce shrinkage.  The result is attached.

We then looked at tuples, the simplest data structure element in python.
A tuple is a fixed sequence of values of any type.

>>> a,b,c = 3,'abc',(4,5)
>>> b
'abc'
>>> d,e = c
>>> d
4
>>> f = (6,7)
>>> f
(6,7)

Finally, we looked at lists.

Our junk PC overheated at that point, but it was time to go home anyway.

A list is a sequence of values that can be inserted, deleted, appended,
and rearranged.  Here is the shuffle example that got interrupted:

>>> import random
>>> deck = range(52)
>>> deck
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
>>> random.shuffle(deck)
>>> deck
[39, 23, 5, 28, 31, 1, 36, 29, 11, 43, 42, 41, 22, 45, 37, 30, 18, 26, 16, 4,
47, 25, 38, 14, 6, 12, 8, 48, 50, 44, 20, 10, 51, 33, 0, 49, 24, 9, 17, 27, 21,
 34, 40, 3, 35, 32, 7, 15, 46, 2, 19, 13]
>>> deck.sort()
>>> deck
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.

-------------- next part --------------
def bal(b):
  """Convert a number to a balanced ternary string representation
  >>> bal(15)
  '+--0'
  >>> bal(0)
  '0'
  >>> bal(-15)
  '-++0'
  """
  if b == 0: 
    return "0"
  s = []
  while b != 0:
    c = "0+-"[b%3]
    if c == '-': b += 1
    s.append(c)
    b = b/3
  s.reverse()
  return ''.join(s)

def _test():
  import doctest,bal2
  return doctest.testmod(bal2)

if __name__ == '__main__':
  _test()
  print "Enter a number."
  n = input()
  print bal(n)
-------------- next part --------------
from random import randrange

deaths = [
  "The survivers revolt and put a poisinous snake in your \
bed; you die.",
  "The survivers put poison in your drink; you die.",
  """You were taking a walk in the night, suddenly hands grab your shoulders...
 your funerel is very small only your wife is there.""",
  """You wake up in the middle of the night when you hear your wife scream 
the assasins are here you knew they would come - there was no food. You 
see a glint in the air, you try to hide but its to late you and your wife are 
buried the next morning.""",
"The survivors revolt, you are thrown out of a window.",
"""The survivers revolt, they starve you, whip you till your half dead, 
then chop your head off.""",
"""you are riding your horse by a cliff suddenly a stone flys through 
the air and hits your horse. He rears and you fall into the cliff...""",
""" Their is a war because of the lack of food, half the city is on yor 
side, and the other half is against you, you die fighting.""" 
]

grain = 2000
people = 500
wisemen = 0

while True:
  print "There are %d peasants in your city." % people
  print "You have %d bushels of grain." % grain
  print "You have %d wisemen." % wisemen
  
  promotions = input ("How many peasants do you want to promote to wisemen?")
  if promotions > people:
    print "You don't have that many peasants."
    continue
  wisemen += promotions
  people -= promotions
  print "You now have %d peasants and %d wisemen." % (people, wisemen)
  plant=input("How much will you plant?")
  if plant > people:
    print "With only %d peasants, you can plant at most %d bushels." % (
    	people,people )
    continue
  food=input("How much food will you give the people?")
  if food + plant > grain:
    print "We don't have that much grain."
    continue
    
  yld=randrange(0,11)
  harvest=yld*plant
  store=grain-plant-food
  shrinkage=randrange(10,50)- wisemen/15
  if shrinkage < 0:
    shrinkage = 0
  loss=store*shrinkage/100
  print "You harvested %d bushels." % harvest
  print "You stored %d bushels." % store
  print "the rats ate %d bushels." % loss
  grain = store - loss + harvest
  req = people + 2 * wisemen # food required for subsistence
  if food > req:
    migration = randrange(0,50) * (food - people) / 100
    print "%d peasants came to the city." % migration
    people = people + migration
  elif food < req:
    starved = max(req - food,people)
    print "%d peasants starved." % starved
    people = people - starved
    if starved > people /2:
        print deaths[randrange(0,len(deaths))]
	break 



More information about the Verba mailing list