So i have a h/w assignment due tomorrow, and i have tried to work on it, but as usual, python frustrates me. Here is the assignment: http://www.cs.gonzaga.edu/depalma/courses/cpsc121/assignPython/asgn5.pdf

and for part two, here is the link for example26 it says to use: http://www.cs.gonzaga.edu/depalma/courses/cpsc121/examplesPython/example26.py

So i tried working out the first part and here is what i have so far:

def hasSpaces(myString):
    myString.find(' ')
    if myString.find(' ')==-1:
        print('False')
    else:
        print('True')
    return myString

def stripSpaces(myString):
    myString.replace(' ',"")
    return myString

and for the second part, i'm a bit confused b/c our teacher didn't really cover much into encrypting and decrypting so i don't know really where to start, although it doesnt look too hard.

any help would be very much appreciated, i have a big chem test tomorrow that i have to study for aswell. Thank you

Recommended Answers

All 2 Replies

Member Avatar for masterofpuppets

hi
here's something i came up with just now :)

###########################################
#Part 1:
def stripSpaces( myString ):
    s = ""
    for w in myString:
        if w != " ":
            s += w
    return s

def hasSpaces( myString ):
    if " " in myString:
        return True
    else:
        return False

###########################################
#This is just using the code in the example - nothing new...
#Part 2:
def rfEncrypt( plainText ):
    evenChars = ''
    oddChars = ''
    charCt = 0

    for ch in plainText:
        if charCt % 2 == 0:
            evenChars = evenChars + ch
        else:
            oddChars = oddChars + ch
        charCt = charCt + 1           #notice that if plainText has an odd 
    cipherText = oddChars + evenChars #number of characters, there is one more
    return cipherText                 #even character than odd

def rfDecrypt(cipherText):
    half = len(cipherText) // 2
    oddChars = cipherText[:half]
    evenChars = cipherText[half:]
    plainText = ''

    for i in range (half):
        plainText = plainText + evenChars[i]
        plainText = plainText + oddChars[i]

    if len(oddChars) < len(evenChars):   #odd number of characters in plain text
            plainText = plainText + evenChars[-1] # paste last even char onto the string

    return plainText

def main():
    command = raw_input( "Enter command ( encrypt or decrypt ): " )
    if command == "encrypt":   
        plainText = raw_input( 'Enter a string: ' )
        plainText = stripSpaces( plainText )
        cipherText = rfEncrypt( plainText )
        print cipherText
    elif command == "decrypt":
        ciphered = raw_input( 'Enter a cipher text: ' )
        if hasSpaces( ciphered ):
            print "Illegal spaces"
            return
        else:
            decrypt = rfDecrypt( ciphered )
            print decrypt

main()

Note that you might want to modify it a bit because I am using python 2.5 and i think the version you are using is newer :) hope this helps!

hi
here's something i came up with just now :)

###########################################
#Part 1:
def stripSpaces( myString ):
    s = ""
    for w in myString:
        if w != " ":
            s += w
    return s

def hasSpaces( myString ):
    if " " in myString:
        return True
    else:
        return False

###########################################
#This is just using the code in the example - nothing new...
#Part 2:
def rfEncrypt( plainText ):
    evenChars = ''
    oddChars = ''
    charCt = 0

    for ch in plainText:
        if charCt % 2 == 0:
            evenChars = evenChars + ch
        else:
            oddChars = oddChars + ch
        charCt = charCt + 1           #notice that if plainText has an odd 
    cipherText = oddChars + evenChars #number of characters, there is one more
    return cipherText                 #even character than odd

def rfDecrypt(cipherText):
    half = len(cipherText) // 2
    oddChars = cipherText[:half]
    evenChars = cipherText[half:]
    plainText = ''

    for i in range (half):
        plainText = plainText + evenChars[i]
        plainText = plainText + oddChars[i]

    if len(oddChars) < len(evenChars):   #odd number of characters in plain text
            plainText = plainText + evenChars[-1] # paste last even char onto the string

    return plainText

def main():
    command = raw_input( "Enter command ( encrypt or decrypt ): " )
    if command == "encrypt":   
        plainText = raw_input( 'Enter a string: ' )
        plainText = stripSpaces( plainText )
        cipherText = rfEncrypt( plainText )
        print cipherText
    elif command == "decrypt":
        ciphered = raw_input( 'Enter a cipher text: ' )
        if hasSpaces( ciphered ):
            print "Illegal spaces"
            return
        else:
            decrypt = rfDecrypt( ciphered )
            print decrypt

main()

Note that you might want to modify it a bit because I am using python 2.5 and i think the version you are using is newer :) hope this helps!

yea i have 3.1 or something like that. thank you very much, i will try and modify it like you said. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.