lunes, 13 de octubre de 2014

Para el reloj!

Juego "parar el reloj" cuando los milisegundos lleguen a 0. En python, probar codigo en CodeSkulptor, Numero de aciertos/Numero de intentos. try it!



# template for "Stopwatch: The Game"
import simplegui

# define global variables
output = "0:00.0"
record = "0/0"
interval=100
t=0
aciertos=0
count=0
flag = False

# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    minutes = 0
    seconds = 0
    
    tenthS = (int(t)%10)
    if t>9:
        seconds = (int(t)/10)%60
    if t>599:
        minutes = (int(t)/60)/10
    if seconds <= 9:
        seconds=str(0)+str(seconds)
    
    return str(minutes)+":"+str(seconds)+"."+str(tenthS)
    
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
    timer.start()
    
def stop():
    global output,record,count,aciertos,flag
    
    timer.stop()
    
    if flag == False:
        if output[5] == "0":
            aciertos=aciertos+1
           
        count=count+1
        record=str(aciertos)+"/"+str(count)
    flag=True
    
def reset():
    global record,output,t
    
    timer.stop()
    record="0/0"
    output=format("0")
    t=0
    
# define event handler for timer with 0.1 sec interval
def timer():
    global t,output,flag
    flag=False
    t=t+1;
    
    output=format(t)
    

# define draw handler
def draw(canvas):
    global output,record
    canvas.draw_text(output,[100,100],24,"White")
    canvas.draw_text(record,[366,24],24,"Green")
    
# create frame
frame  = simplegui.create_frame("StopWatch!",400,200)

# register event handlers
frame.set_draw_handler(draw)
frame.add_button("Start",start,100)
frame.add_button("Stop",stop,100)
frame.add_button("Reset",reset,100)
timer=simplegui.create_timer(interval,timer)
# start frame
frame.start()

sábado, 27 de septiembre de 2014

Rock, Scissor, Paper, Lizard, Spock RSPLS

Implementar la logica del juego piedra, papel, tijera, lizard, Spock. En python, probar codigo en CodeSkulptor

# Rock-paper-scissors-lizard-Spock template import random # The key idea of this program is to equate the strings # "rock", "paper", "scissors", "lizard", "Spock" to numbers # as follows: # # 0 - rock # 1 - Spock # 2 - paper # 3 - lizard # 4 - scissors

# helper functions

def name_to_number(name):
    # delete the following pass statement and fill in your code below
    if name == 'rock':
        return 0
    elif name == 'Spock':
        return 1
    elif name == 'paper':
        return 2
    elif name == 'lizard':
        return 3
    elif name == 'scissors':
        return 4
    else:
        return 'Error: name does not match any options.'

    # convert name to number using if/elif/else
    # don't forget to return the result!


def number_to_name(number):
    # delete the following pass statement and fill in your code below
    if number == 0:
        return 'rock'
    elif number == 1:
        return 'Spock'
    elif number == 2:
        return 'paper'
    elif number == 3:
        return 'lizard'
    elif number == 4:
        return 'scissors'
    else:
        return 'Error: number does not match any options'
    # convert number to a name using if/elif/else
    # don't forget to return the result!
    

def rpsls(player_choice):
    
    # delete the following pass statement and fill in your code below
    
   
    # print a blank line to separate consecutive games
    print ""

    # print out the message for the player's choice
    print "Player chooses ",player_choice

    # convert the player's choice to player_number using the function name_to_number()
    player_number = name_to_number(player_choice)
    # compute random guess for comp_number using random.randrange()
    comp_number = random.randrange(0,5)
    # convert comp_number to comp_choice using the function number_to_name()
    comp_choice = number_to_name(comp_number)
    # print out the message for computer's choice
    print "Computer chooses ",comp_choice
    # compute difference of comp_number and player_number modulo five
    
    # use if/elif/else to determine winner, print winner message
       
    if comp_number - player_number == 1 or comp_number - player_number == 2:
        print "Computer wins!"
    elif comp_number - player_number == 3 or comp_number - player_number == 4:
        print "Player wins!"
    elif comp_number - player_number == -3 or comp_number - player_number == -4:
        print "Computer wins!"
    elif comp_number - player_number < 0:
        print "Player wins!"
    elif player_number - comp_number < 0:
        print "Computer wins!"
    else:
        print "Player and computer tie!"
    
# test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")

viernes, 1 de marzo de 2013

es un semordnilap?


def semordnilapWrapper(str1, str2):
    # A single-length string cannot be semordnilap
    if len(str1) == 1 or len(str2) == 1:
        return False

    # Equal strings cannot be semordnilap
    if str1 == str2:
        return False

    return semordnilap(str1, str2)

def semordnilap(str1, str2):
    '''
    str1: a string
    str2: a string
    
    returns: True if str1 and str2 are semordnilap;
             False otherwise.
    '''
    # If strings aren't the same length, they cannot be semordnilap
    if len(str1) != len(str2):
        return False

    # Base case: if the strings are each of length 1, check if they're equal
    if len(str1) == 1:
        return str1 == str2

    # Recursive case: check if the first letter of str1 equals the last letter
    # of str2
    if str1[0] == str2[-1]:
        return semordnilap(str1[1:], str2[:-1])
    else:
        return False

sábado, 27 de octubre de 2012

diccionarios en phyton how many elements are in


def howMany(aDict):
    '''
    aDict: A dictionary, where all the values are lists.

    returns: int, how many values are in the dictionary.
    '''
    sum=0
    for e in aDict:
        sum+=len(aDict[e])
       
    return sum

jueves, 25 de octubre de 2012

oddTuples, listas o vectores en phyton


def oddTuples(aTup):
    '''
    aTup: a tuple
   
    returns: tuple, every other element of aTup.
    '''
    x=0
    ini =()
    if aTup==():
        return ()
    else:
        for x in range(0,len(aTup),+2):
            ini+=(aTup[x],)
           
    return ini

Engorroza funcion pero hice el intento y funciona, binarysearch


def isIn(char, aStr):
    '''
    char: a single character
    aStr: an alphabetized string
   
    returns: True if char is in aStr; False otherwise
    '''
    mid = len(aStr)/2
    print mid
    print ' ' + aStr[mid]


    if len(aStr)== 0:
        return False

    if mid == 1:
        if char == aStr[1]:
            return True
        else:
            return False
       
        if char == aStr[0]:
            return True
        else:
            return False
       
    else:
        if char == aStr[mid]:
            return True
        else:
            if char < aStr[mid]:
                return isIn(char,aStr[0:mid])
            else:
                return isIn(char,aStr[mid:len(aStr)])

funcion Calcular longitud de cadena string , recursivo Phyton


def lenRecur(aStr):
    '''
    aStr: a string
   
    returns: int, the length of aStr
    '''

    if aStr == '':
        return 0

    if aStr!= '':
        return lenRecur(aStr[:-1])+1