Programming Assignment 2

Python Programming Assignments


This is the second set of python assignments I found.

Assignment 8



def getText(filename):
    "OPENS FILE AND RETURN CONTENT AS A LIST"

    # Opens file
    fo = open(filename, "r")

    # Reads content
    content = fo.read()

    if len(content) == 0:
        return NAMEERROR
    else:
        line = content.split("\n")

    # Closes file
    fo.close()

    # Return value
    return line


def show(output):
    "DISPLAYS THE OUTPUT"
    
    # Prints given string
    print str(output)


def convert(text):
    "CIPER THE CODE AND RETURN THE STRING"

    if len(text) > 20:
        return None
    
    else:
        # Create a variable to store the ciper
        dummy_str = ""

        # Loop through every letter
        for letter in text:
            
            add_to = ord(letter)

            # If it's a letter, process under these conditions        
            if (ord(letter) >= 65 and ord(letter) <= 85) or (ord(letter) >= 97 and ord(letter) <= 117):
                add_to = ord(letter) + 5
            if (ord(letter) >= 118 and ord(letter) <= 122) or (ord(letter) >= 86 and ord(letter) <= 90):
                add_to = ord(letter) - 21

            # Fill up the resulting string
            dummy_str += chr(add_to)

        # Return it
        return dummy_str


def saveFile(fobj, text):
    "WRITES DATA TO AN OPEN FILE"

    # Writes 'text' to the 'fobj' file
    fobj.write(text + "\n")
    

def main():
    "MAIN PROGRAMME"

    # Opens writing file
    fw = open("Result.txt", "w")
    
    try:
        # Assign the content to a list
        code = getText("FileIn.txt")

        # It can contain only 50 or less lines
        if len(code) < 50:

            for sentence in code:
                
                try:
                    ciper = convert(sentence)
                    saveFile(fw, ciper)
                    
                except (TypeError):
                    print "There are more than 20 characters in the line... :("
                    saveFile(fw, "There are more than 20 characters in the line... :(")
                    
                else:
                    show(ciper)

        else:
            print "There are more than 50 lines to ciper... :("
            saveFile(fw, "There are more than 50 lines to ciper... :(")
            
    except (IOError):
        print "No file to read from... :("
        saveFile(fw, "No file to read from... :(")
        # Closes writing file
        fw.close()

    except (NameError):
        print "File is empty... :("
        saveFile(fw, "File is empty... :(")
        # Closes writing file
        fw.close()
        
    # Closes writing file
    fw.close()

main()

Assignment 9



def getText(filename):
    "Reads a file and return content as a list"
    # Trick : Returns an ZeroDivisionError if the list content is over 50
    
    # Opens file
    fo = open(filename, "r")
    
    # Reads file content as a list
    data = fo.read().split("\n")

    # Closes file
    fo.close()

    if len(data) < 50:
        return data
    else:
        return 10/0

def process(String):
    "Count number of times letters are present and returns a dictionary"
    
    # Empty dictionary to store counts
    dic = {}

    # Lower case every letter
    String = String.lower()
    
    # Go through every letter in String
    for letter in String:

        # If the letter is not inside the dictionary, adds an entry
        if letter not in dic:
            dic[letter] = 1
        # If it is present already, add one up
        else:
            dic[letter] += 1
            
    # Return the dictionary
    return dic

def result(String, Dictionary):
    "Displays count in a line"

    # Returns the complete string output
    Return_str = []

    # Prints the count and letters
    for key in Dictionary:
        if Dictionary[key] == 1:
            Return_str.append("'" + key + "'")

    # Returns the resulting string
    if len(Return_str) != 0:
        return ", ".join(sorted(Return_str))
    else:
        return "Nothing"

def show(fileobj, String):
    "Displays String on screen and file both"
    
    # Displays on the screen
    print String

    # Writes to the file
    fileobj.write(String)
    fileobj.write("\n")

def main():
    "MAIN PROGRAMME"
    
    # Opens a file to write data
    fw = open("Result.txt", "w")
    
    try:
        # Gets a list of words in the file
        words = getText("FileIn.txt")

        # Go through every word in words
        for word in words:
            
            # Word length is limited to 20
            if len(word) < 20:

                # Processed dictionary
                word_processed = process(word)

                # Result string
                Result = result(word, word_processed)

                # Build up the resultant string
                Resultant = word + " -> " + Result + " appear exactly once"
                
                # Display result
                show(fw, Resultant)
                
            else:
                print "Word length is limited to 20 characters... :("

    except (IOError):
        print "No file to read from... :("
        fw.write("No file to read from... :(")
        fw.close()

    except (ZeroDivisionError):
        print "File contains more than 50 words... :("
        fw.write("File contains more than 50 words... :(")
        fw.close()
        
    # Closes the file
    fw.close()

"""MAIN PROGRAMME EXECUTION"""

# Calling the main function
main()

Comments

Popular posts from this blog

Mocking Point Clouds in ROS Rviz

Python Laboratory Excersices

Find Maximum Number in a Nested List Recursively