CSE 1310 - Assignments - Programming Assignment 6

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.)

What does this function do? What might it be used for? Note: s1 and s2 are strings. Your answer should be understandable by someone who does not know programming. Put your answer in answers.xxx.

def function1(s1, s2):
    s1_list = list(s1.lower())
    s2_list = list(s2.lower())
    s1_list.sort()
    s2_list.sort()
    if s1_list == s2_list:
        return True
    else:
        return False

Task 2 (30 pts.)

Describe the execution of this program: what lines are executed; what namespaces come into existence and when; what variables are in each namespace and what are their values? Your answer must explicitly show the state of each namespace after every execution of a line. Put your answer in answers.xxx.

def function1(z):       # line 1
    values = [1, 2, 3]  # line 2
    return values[z]    # line 3
                        # line 4
def function2(z, y):    # line 5
    if y < 0:           # line 6
        val = 0         # line 7
    elif y == 10:       # line 8
        val = 1         # line 9
    else:               # line 10
        val = 2         # line 11
    x = function1(val)  # line 12
    return x*z          # line 13
                        # line 14
x = 10                  # line 15
y = 2                   # line 16
print(function2(y, x))   # line 17

Task 3 (10 pts.)

Write a function named repeat_number(s) that:

For example:

IMPORTANT: make sure that, when the input argument is an integer, the output is also an integer. In other words, make sure that, if the input argument is of type int, the return value is also of type int.

Save the function in file task3_functions.py, and code testing your function in file task3_main.py.


Task 4 (10 pts.)

Write a function named divisors(num, limit) that:

For example:

Save the function in file task4_functions.py, and code testing your function in file task4_main.py.


Task 5 (10 pts.)

Write a function named is_prime(s) that:

For example:

Save the function in file task5_functions.py, and code testing your function in file task5_main.py.


Task 6 (10 pts.)

Write a function named squares(s) that:

For example:

Save the function in file task6_functions.py, and code testing your function in file task6_main.py.


Task 7 (10 pts.)

Write a function named random_element(s) that:

For example, if I pass it the string "hello!", it may return "h", or "e", or any other of the letters in "hello", but it should not return "m" or "n", since those letters are not in "hello!". Your function will return different things each time you pass it the same string, but (by chance) it may return the same answer multiple times in a row. To have Python choose a random integer from n to m, including both n and m, 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) 

Save the function in file task7_functions.py, and code testing your function in file task7_main.py.


Task 8 (10 pts.)

Write a function named reverse_vowels(s) that:

For example:

Save the function in file task8_functions.py, and code testing your function in file task8_main.py.


Suggestions

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 assignment5.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