Florida Southern Seal
CSC 4640: Programming Languages
(Spring 2025)

Syllabus

LMS

Teachers


Assignments

Assignments
Other Pages

Final 0:
All Together Now! (In Python)


Assigned: Wed Apr 23 2025
Due: 5:15:00 PM on Tue Apr 29 2025
Team Size: 1
Language: Various
Out of: 100 points


This project is the final exam for this course. In this project, you will write code in languages that are not well-suited for that code's goals. Keep in mind that this is a FINAL EXAM. As such, you must work alone and you may not communicate with anyone else about this project. You may only use the following resources:
  • Me (Kyle Burke). Seriously, come ask me questions!
  • Any of the course texts listed on the syllabus,
  • Your notes from class,
  • Code from your prior projects, and
  • Pages linked to directly on this assignment.
You may not use any other physical or electronic sources (e.g. Stack Overflow or generative AI). If your IDE includes an AI assistant, turn it off now. You may not use any non-built in Python libraries (e.g. numpy). Ignoring these restrictions will cause you to earn a zero on the project and may result in additional administrative honor code penalties. If you think there's another resource that you should be able to use, ask me first! If you proceed without asking, you are running the risk of getting violating the above rules!

Part 0, 0 points:

Create a python file, finalExam4640.py. (If you want, you can start with the template file that I created with stubs for the different parts of this project.) Put it in a folder as described in the submission instructions below. Put your name in the file's docstring to indicate that you're the author, e.g. by including: author: YourName.

Part 1, 30 points:

Python Lambdas Add to your file a function, monkey_sort, which takes a list of integers. This should sort the list following all three of these conditions:
  • All non-squares should come before squares. (A square is an integer which has an integer square root.)
  • All non-squares should be sorted highest to lowest.
  • All squares should be sorted lowest to highest.
E.g.:
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> monkey_sort(a)
>>> print(a)
[10, 8, 7, 6, 5, 3, 2, 0, 1, 4, 9]
>>> a = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> monkey_sort(a)
>>> print(a)
[10, 8, 7, 6, 5, 3, 2, -1, 0, 1, 4, 9]
I want you to use Python lambda functions, so you must write your function in the following way:
  • Your function can only manipulate the list by calling the sort method. You can supply that with a function parameter as the key. Python then sorts the array based on the output of that key on the elements, from lowest to highest. (Unlike Java, the key function takes a single element instead of comparing two elements.) If you modify the list in any other way, you won't get points for this part.
  • You should include a lambda function for your key.
  • You are allowed to create any other variables you might need, but you can't modify the list except through the sort method!
You'll get partial points for this if your function only works for non-negative numbers, like in the first example above.

Part 2, 30 points:

Python INOUT-Mode Parameters INOUT mode parameters in Prolog are quite powerful! Let's try implementing them in Python using classes. In this you will create a class that acts as a wrapper for parameters, which may be instantiated (they have a value) or not (they don't have a value). Your class will support this kind of code:
x = InoutParameter()
...
x.setValue('cheese')
...
print(x.getValue())
...
y = InoutParameter(42)
...
print(y.getValue())
A skeleton to implement this is on CodingRooms. You have to finish it by completing these parts:
  • Implement the constructor (__init__). You need to handle the cases where there is a parameter and no parameter. You might need to review default argument values.
  • Implement hasValue, which returns whether there is a value set in this parameter.
  • Implement setValue, which sets the value of this parameter. INOUT-mode parameters shouldn't be able to do this if they already have a value, however, so in that case you should throw a RuntimeError. You may want to review error-handling in Python
  • Implement getValue, which gets the value stored in this parameter. If none has been set, then, you guessed it, you should throw a RuntimeError.
  • Implement a separate function, toSet, which takes two InoutParameters. The first represents a list and the second a set. If the set contains exactly the same elements as the list, then the function should return True, otherwise False. (Capitalization matters.) If only one of the two parameters is initialized, then your code should set the other one so that the function returns True. I will not test the case where both are uninstantiated. You might want to review Python documentation for lists and sets. You can iterate over a set, so you can do something like this:
    for x in mySet:
        print(x)

Part 3, 30 points:

Python Parallel Processing Python has something called the Global Interpreter Lock which prevents multiple Python threads from executing simultaneously. Nevertheless, we can still create threads and dispatch them using the threading package. Although we won't get any speedup, add to your final exam file a new function, parallel_xor, which takes two parameters, a list of boolean values and an integer number of threads to use. Your code should then use that many threads to calculate the XOR of all elements in the list. (I highly recommend creating some of the other helper functions in the Go project where you wrote a similar code.) Here are some hints for using the threading package:
  • Create threads using the Thread constructor like this: (Yes, you need to use keyword parameters.)
    t = threading.Thread(target=function_you_want_to_call, args=[param_a, param_b, param_c])
  • Run a created thread concurrently by calling t.start()
  • Wait for that thread to complete by calling t.join()
  • You can't have the thread return a value--the target function should be void--so you have to be creative with getting the result value back to the main thread. I wrote my function to take a list parameter, and I put the value in there when it was done executing. Then the parent thread just pulled that element out of that list. (You have to create the list in the parent thread so they both have a pointer to the same thing.)

Part 4, 10 points:

Add the following statement to your file's docstring, replacing <YourName> with your full name:
"I, <YourName>, am the sole author of this file. I did not use any unacceptable sources to help me write this code. I understand that any violation of this will result in a zero on this exam and potential administrative penalties."
If you do not include this statement exactly as I have written here, then you will earn zero points on this exam.

Submitting your Project: Put your finalExam4640.py file in a folder named <YourLastName>FinalExam, where <YourLastName> is your last name. Your folder name should not include any special characters. Zip that folder into <YourLastName>FinalExam.zip and then upload it to the Final Exam assignment on Canvas. If I have to make any adjustments to your file/folder structure or your code in order for me to grade it smoothly, I will deduct 10 points.