Public feed #727413

Mine

public python v1 · immutable
Raw Edit
·published 2007-10-05 23:56 UTC
rendered paste body
# ############################################################################ ## Assignment #2# Part 1: warmup.py# Author: Hardeep Singh and Melissa # Date: October 05, 2007## Description:#     Several list/file related warmup functions:#       - whereis(str, str)#       - evens(list)#       - reverse(list)#       - cat(file)#       - double_my_digits(str)#       - fix_capitalization(list)## ############################################################################ #def whereis(s1,s2):    '''whereis(str, str)-> int/boolean        Returns the index number of the first occurence of the single letter     string s2 in the string s1. Returns False if the parameters given are not     strings or s2 is not a single letter in length.'''     if ( type(s1) != str ) or ( type(s2) != str ):        return False        if ( len(s2) != 1 ):        return False        return s1.find(s2)def evens(the_list):    '''evens(list) -> list/boolean        Given a list, returns a new list consisting only of those elements with    an even index. Returns False if the parameter given is not a list.'''        if ( type(the_list) != list ):        return False        new_list = []        for index in range(0, len(the_list)):        if ( index % 2 == 0 ):            new_list.append(the_list[index])        return new_listdef reverse(original_list):    '''reverse(list) - > list/boolean        Given a list, returns a new list in which the elements of the original    list have been reversed. Returns False if the parameter given is not a list.     '''        if ( type(original_list) != list ):        return False     new_list = []    for element in original_list:        new_list.insert(0,element)        return new_listdef cat(text_file):    '''cat(file) -> Boolean        Opens and displays a given text file on screen, line by line. Returns False    if the parameter given is not a file.'''        if ( type(text_file) != file ):        return False        for line in text_file:        # Since python also prints out the carriage return at the end of each        # line of the file, there is no need for us to print out another one        # of our own.        print line,        # Just to make it look neat at the end. Otherwise the console gets a bit    # messy looking.    print ''def double_my_digits(input):    '''double_my_digits(str) -> str/boolean        Given a string consisting only of numbers, will return a string obtained by    doubling each digit in the input string. Will return False if the input is    not a string, or contains something other than what is allowed in an int.'''        if ( type(input) != str ):        return False        try:        input = str(int(input))    except ValueError:        return False        new_string = ''    for digit in input:        new_string += str(int(digit) * 2)            return new_stringdef reverse(a_list):    '''reverse(list)->list/boolean        Given a list, returns a new list in which the elements of the original    list have been reversed. Returns False if the parameter given is not a list.     '''        if ( type(a_list) != list ) :        return False    new_list = []    for element in a_list:        new_list.insert(0,element)    return new_listdef fix_capitalization(the_list):    '''fix_capitalization(list) -> list/boolean        Given a list of strings returns a new list of strings in which the    capitalization of the original strings has been corrected. Returns False if    the parameter given is not a list.'''        if ( type(the_list) != list ):        return False        new_list = []    for string in the_list:        new_list.append(string[0] + string[1:].lower())            return new_list