From b732580e43fa0f73511f25622f5de95ccc3eee84 Mon Sep 17 00:00:00 2001 From: Dario Clavijo Date: Thu, 13 Jan 2011 04:55:14 +0000 Subject: Add life, factorial, and jeringoso samples Co-authored-by: James Cameron --- (limited to 'data') diff --git a/data/graphics/life b/data/graphics/life new file mode 100644 index 0000000..7ddb4c2 --- /dev/null +++ b/data/graphics/life @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +# This is the game life http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life + +import os, time, random + +# +# we need a function to load cells in the neighborhood +# + +def LoadCells(rows, cols): + grid = [] + col = [0] * cols + # first we load an empty grid + for i in range(rows): + col = [0] * cols + grid.append(col) + # then we load some cells + for x in range(rows): + for y in range(cols): + cell = random.randint(0, random.randint(0, 1)) + grid[x][y] = cell + return grid + +# +# here we draw the grid +# + +def DrawGrid(grid): + rows = len(grid) + cols = len(grid[1]) + for x in range(rows): + for y in range(cols): + if grid[x][y] != 1: + print ".", + else: + print "o", + print "\n", + +# +# count neighbors arround a single cell +# + +def CountNeighbors(grid, x, y): + neighbors = 0 + rows = len(grid) + cols = len(grid[1]) + + if x < rows-1 and grid[x+1][y] == 1: + neighbors += 1 + if x > 0 and grid[x-1][y] == 1: + neighbors += 1 + if y < cols-1 and grid[x][y+1] == 1: + neighbors += 1 + if y > 0 and grid[x][y-1] == 1: + neighbors += 1 + if x < rows-1 and y < cols-1 and grid[x+1][y+1] == 1: + neighbors += 1 + if x > 0 and y > 0 and grid[x-1][y-1] == 1: + neighbors += 1 + if x > 0 and y < cols-1 and grid[x-1][y+1] == 1: + neighbors += 1 + if x < rows-1 and y > 0 and grid[x+1][y-1] == 1: + neighbors += 1 + + return neighbors + +# here we define a single iteration +# if we have between 3 and 6 neighbors the single cell lives +# in other case the cell dies + +def Iteration(grid): + rows = len(grid) + cols = len(grid[1]) + neighbors = 0 + for x in range(rows): + for y in range(cols): + neighbors = CountNeighbors(grid, x, y) + if grid[x][y] == 1: + if neighbors < 2 or neighbors > 3: + grid[x][y] = 0 + else: + if neighbors == 3: + grid[x][y] = 1 + +# +# iterate n pulses and draws the result of each one +# + +def Iterator(rows, cols, pulses): + pulse = 1 + grid = LoadCells(rows, cols) + while pulse <= pulses: + os.system('clear') + print "Pulse: ", pulse + Iteration(grid) + DrawGrid(grid) + pulse += 1 + time.sleep(0.2) + +number = input("Please input the number of rows and cols (unique number):") +pulses = input("Please input the number of pulses:") +Iterator(number, number, pulses) diff --git a/data/math/factorial b/data/math/factorial new file mode 100644 index 0000000..42731d6 --- /dev/null +++ b/data/math/factorial @@ -0,0 +1,34 @@ +import time + +# define a factorial function in recursive flavor +def factorial_recursive(number): + result = 1 + if n > 0: + result = n * factorial_recursive(number-1) + print "factorizing: ", number, " result: ", result + return result + +# define a factorial function in iterative flavor +def factorial_iterative(number): + result = 1 + for i in range(1, number+1): + result = result * i + print "factorizing: ", i, " result: ", result + return result + +def calculate(number, type): + start = time.time() + if type == 0: + type_s = "recursive" + result = factorial_recursive(number) + else: + type_s = "iterative" + result = factorial_iterative(number) + delta = time.time() - start + print "Type: ", type_s, " in: ", 1/delta + +# ask for a number to factorize +number = input("Please input a number:") +print "Calculating..." +calculate(number, 0) +calculate(number, 1) diff --git a/data/string/jeringoso b/data/string/jeringoso new file mode 100644 index 0000000..e24c069 --- /dev/null +++ b/data/string/jeringoso @@ -0,0 +1,21 @@ +# jeringoso is an innocent spanish way to talk used by kids, +# you need to add an "p" before earch spanish vowel (a,e,i,o,u) and +# the same vowel after the "p" +# then you got a funny way to talk that parents will not understand. +# ref: http://en.wikipedia.org/wiki/Jeringonza + +# first we define the spanish vowels +vowels = ['a', 'e', 'i', 'o', 'u'] + +# then we ask for a string to make the replacements +text = raw_input("Please input your text to translate to jeringoso:") +length = len(text) +jeringoso = "" +for i in range(length): + char = text[i] + if char in vowels: + jeringoso = jeringoso + char + "p" + char + else: + jeringoso = jeringoso + char + +print jeringoso -- cgit v0.9.1