Lab Assignment 7, TCSS 142 solution

$25.00

Category:

Description

Rate this product

 

  1. Basic Strings (25%)

 

Go to the website http://codingbat.com/python/String-1  and complete all String-1 exercises:

hello_name H       make_abba H       make_tags

make_out_word     extra_end        first_two

first_half        without_end            combo_string

non_start         left2

 

  1. Strings and Functions (25%)

Answers to the following questions should be implemented as functions in a program called strings.py. While coding, check Python libraries for methods that may make your job easier.

  1. Write a function capitalizer that takes a string as an argument and then returns the new version of that string. The original string that will be passed to the function is a collection of sentences. The returned version changes the first character of each sentence to uppercase. For example, if my name is joe. what is your name?   is passed to this function, the function returns   Hello. My name is joe. What is your name?  Assume a sentence may end with a period, a question mark, or an exclamation.
  2. Write a function splitter that takes a string as an argument and then returns the new version of that string. The original string that will be passed to the function consists of a sentence in which all of the words are run together but the first character of each word is uppercase. The returned version changes the sentence so that the words are separated by spaces and only the first word starts with an uppercase letter. For example, if StopAndSmellTheRoses.  is passed to this functions, the function returns Stop and smell the roses.
  3. Write a function insert that simulates insert behavior on a string. The function takes three arguments: a character to be inserted, its position, and a string into which a character is to be inserted. The function returns the new version of the string. For example, if arguments are: below,  3, and l, then the function returns bellow. If a position passed to the function is outside of the bounds of the original string (in this case <0 or >5), then return the original string, without any changes.

 

  1. Problem solving with strings and functions (30%)

A transposition cipher we encoded in class used a two-rail method, which simply means that the original string was split into two substrings based on alternating positions. Your job is to extend this idea and implement a three-rail method of transposition, so that the original string is split into three substrings based on every third position. For example, if

Hello_there

is entered, your program should return

l_eeohehltr.

Provide both encryption and decryption functions and test them by using the following main function (remember that your program should work for any length of a string and these are just sample tests):

 

def main():

 

cipherText = encrypt(‘123456’)

print(“Transposition cipher: “, cipherText)

plain = decrypt(cipherText)

print(“Checking our decryption: “, plain)

cipherText = encrypt(‘12345’)

print(“Transposition cipher: “, cipherText)

plain = decrypt(cipherText)

print(“Checking our decryption: “, plain)

cipherText = encrypt(‘1234’)

print(“Transposition cipher: “, cipherText)

plain = decrypt(cipherText)

print(“Checking our decryption: “, plain)

—–  THE END —–