Friday, 3 March 2017

Intro to Computer Science Python Coding Assignment

Description:
Problem 1 (List Slicing Function)
Python provides slicing functionality for lists, but for this question, you will implement your own function capable of producing list slices (note: you cannot use the slicing operator in your solution). The function should be called slice and take the following three inputs in this specific order:
A list, source, which the slice will be created from. This list cannot be modified by your function.
A positive integer, start, representing the starting index of the slice you will create. If this value is not in the range [0, len(list)-1], your function should return an empty list.
A positive integer, end, representing the ending index of the slice you will create. If this value is not in the range [start, len(list)-1], your function should return an empty list.
If the parameter values are acceptable, your function will return a list that contains the items from source beginning at the index start and ending at the index end (inclusive). This is different from the Python slice operator, as the item at the index end is also included in the new list. Examples:
mylist = [“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”]
slice(mylist, 0, 9) should be [“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”] slice(mylist, 3, 4) should be [“D”, “E”]
slice(mylist, 4, 3) should be [ ]
slice(mylist, 3, 8) should be [“D”, “E”, “F”, “G”, “H”, “I”]
slice(mylist, 4, 4) should be [“E” ]
Save your code is a file called slice.py and add it to your submission zip.

Problem 2 (Queue)
A queue is a data structure use to store items. The next item removed from a queue is always the item that has been in the queue the longest (i.e., was added first - think of a line of people waiting to buy something). In this question, you will use a list to implement a queue and write several functions to perform operations on the queue. Your program will need the following components:

A variable representing the maximum size of the queue (how many items can be stored in the queue at one time). This value should have a default value of 10.
A list type variable, which is used to store all of the data in the queue.
An enqueue function, which takes a single input value. If there is room in the queue (i.e.,
the current size is less than the maximum size), the value will be added to the end of the queue and the function will return True. If there is no room in the queue, the function will return False.
A dequeue function, which has no inputs. If the queue has any items in it, this function will remove and return the first item (i.e., from the front of the queue). If the queue does not have any items in it, the function should return None. In this case, None is the specific Python value representing nothing, not the string value “None”.
A peek function, which has no inputs. If the queue has any items in it, this function will return the value of the first item in the queue but leave that item in the queue (different from the dequeue function). If the queue does not have any items in it, the function should return None. In this case, None is the specific Python value representing nothing, not the string value “None”.
An isempty function, which takes no inputs. This function should return True if the queue is empty (no items) and False otherwise.
A getlist function, which has no inputs. This function will return the list that stores the queue data. This function can be used to print the queue contents using print(getlist()).
A multienqueue function, which takes a single list type input argument. This function should add as many of the items from the input list to the queue (i.e., keep adding until the queue is full or all items have been added) and return the number of items that were added successfully.
A multidequeue function, which takes a single integer input value N. This function should attempt to dequeue up to N items from the queue and return a new list containing all items removed (note: this may be less than N if the queue has become empty).
Save your queue function code into a file called queue.py that

No comments:

Post a Comment

Note: only a member of this blog may post a comment.