Interesting Programs

Below, you will find one Python function, and one Python program. Once I had created them, I was amused by their combination of simplicity, efficiency, and effectiveness.

#######################
# Name: CDT Brendan Babcock
# File Name: hw6.py
#######################
# Problem 1
#
# Program Explanation: This function computes the sum of the neighboring numbers of a number in a given list, for all numbers within the list.
# The function then returns a new list (of the same length), containing the summed numbers. 
#
# Test Case 1:
# listOfNums = [10, 20, 30, 40, 50]
# newListOfNums = [30, 60, 90, 120, 90]
#
# Test Case 2:
# listOfNums = [20, 30, 40, 50, 60, 70, 80]
# newListOfNums = [50, 90, 120, 150, 180, 210, 150]

def sumOfNeighbors(old_list):
    lengthOld = len(old_list)
    new_list = []
    newNumber = 0
    newNumber2 = 0
    newNumber3 = 0
    for i in range (0, lengthOld):
        if i==0:
            newNumber = old_list[i]+old_list[i+1]
            new_list.append(newNumber)
        if i >0 and i < lengthOld-1:
            newNumber2 = old_list[i-1]+old_list[i]+old_list[i+1]
            new_list.append(newNumber2)
        if i==lengthOld-1:
            newNumber3 = old_list[lengthOld-1]+old_list[lengthOld-2]
            new_list.append(newNumber3)
    return new_list

if __name__=="__main__":
    listOfNums = [20, 30, 40, 50, 60, 70, 80]
    newListOfNums = sumOfNeighbors(listOfNums)
    print newListOfNums

# Problem 2
#
# Program Explanation: This program takes as input a file containing a phrase, and encrypts it. This is done through transcription to an intermediary list, encryption, and finally re-writing the 
# original file to contain the encrypted phrase.  
#
# Test Case 1:
# Old Phrase: THE EAGLE FLIES AT DAWN.
# New Phrase: WKH HDJOH IOLHV DW GDZQ. 
#
# Test Case 2:
# Old Phrase = the eagle flies at dawn.
# New Phrase = WKH HDJOH IOLHV DW GDZQ. 

def caesarCipher():
        userFile = ""
        userFile = raw_input("Please enter the name of the file you will be utilizing.")
        inputFile = open(userFile,"r")
        encodedList = []
        encodedList2 = []
        overLength = 0
        for lines in inputFile:
                for char in lines:
                    encodedList.append(char.capitalize())
        for chars in encodedList:
                if char == " ":
                    encodedList2.append(chars)
                if ord(chars) >=65 and ord(chars) <=87:
                    chars = chr(ord(chars)+3)
                    encodedList2.append(chars)
                elif ord(chars) == 88:
                    overLength = 65
                    encodedList2.append(chr(overLength))
                elif ord(chars) == 89:
                    overLength = 66
                    encodedList2.append(chr(overLength))
                elif ord(char) ==90:
                    overLength = 67
                    encodedList2.append(chr(overLength))
                else:
                    encodedList2.append(chars)
                        
        lengthList = len(encodedList)
        out_file = open(userFile,"w")
        for i in range (0, lengthList):
            out_file.write(encodedList2[i])
        out_file.close()
caesarCipher()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s