CSE 1310 - Assignments - Programming Assignment 4

The assignments will be graded out of 100 points. Create a text document entitled answers.xxx (where you replace xxx with whatever extension is appropriate, depending on the file format you use). Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID at the top. Your answers that are not part of a program will be added to this file. Each task below will instruct you where to put your answers.


Task 1 (10 pts.)

The following code is supposed to ask the user for a string S and print the length of S as well as the first and last characters. It contains multiple errors. In your answers.xxx file, explain exactly what the errors are. Do not simply repeat any Python error message or just give a correction. Note that an error may be a conceptual error, not an error that causes the program to crash. Then, write a corrected version of the program and save as task1.py.

S = raw_input('Enter a string: ')
length = len(S)
first_character = S[1]
last_character = S[length]
print 'Length:', length
print 'First:', first_character
print 'Last:', last_character

Task 2 (10 pts.)

The following code is supposed to create a string called s, convert it to a list called l and print each item in the list on a separate line. It contains multiple errors. In your answers.xxx file, explain exactly what is wrong. Do not just give a corrected version, but explain each error. Then write a corrected version and save it as task2.py

s = 'This isn't a number'
l = list(s)
for char in l:
    print l[char]

Task 3 (10 pts.)

What does this function do? Do not simply give the output from running it with a sample string, but try to figure out what its purpose is.

def function1(s):
    s = s.lower()
    vowels = 'aeiou'
    for x in s:
        if x in vowels:
            return x
    return None

How does this function differ? What does it do?

def function2(s):
    s = s.lower()
    vowels = 'aeiou'
    for x in s:
        if x in vowels:
            return True
    return False

Put your answer in answers.xxx.


Task 4 (20 pts.)

Write a function named shuffle(s) that:

For example, if I pass it the string "hello!", it randomizes the characters and returns something like "!olehl" (it will likely be different each time you pass it the same string). This new string contains the same characters as the old string, but they are in a different order. Do not use the Python list shuffle function for this task. You are creating your own. To have Python choose a random integer from n to m, including both n and m (for example, to select a list index at random), you can use the following code:

# Place this line at the beginning of your file. You do not 
# need this line each time you generate a random number.
from random import randint   

# This line generates a random integer from values n, n+1, ..., m, and stores it in random_index.
random_index = randint(n, m) 

Be sure to use each character from the original string only once, so that the new string contains the exact same characters. Save the function in file task4_functions.py, and code testing your function in file task4_main.py.


Task 5 (20 pts.)

In class, we discussed a method for checking to see if a string can be a legal integer. Before we wrote any code, we created a set of rules that would guide us in writing our code. That set of rules is on slide 10 of this presentation. We now want to do the same for floats. Create a set of rules for floats that can help guide us in implementing the code. Place your rules in answers.xxx.


Task 6 (20 pts.)

Implement a function named check_float(text) similar to the check_integer example we did in class that uses your rules from task 5 to check if a string can be a float. Do not use anything we haven't learned in class like try-except blocks. It needs to function similarly to the example from class. Your solution needs to take in a string as input and return a boolean as output. Save the function definition in file task6_functions.py, and code testing your function in file task6_main.py.


Suggestions

The code needs to run on Python 2.x. Notify the instructor or TA of any problems you may have.

Pay close attention to all specifications on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for non-compliance with the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.

How to submit

The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment4.zip (no other forms of compression accepted, contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers.xxx document and all the Python code files (task5.py, task6.py, etc).

Submission checklist