The programs should be saved in Python script file (.py extension).
1) Random race: Imagine two runners are competing in a race. They both begin at the starting
line (step 0). The finish line is at step 100. Use a while loop to give each runner a turn in the race.
Each turn they take, they advance a random integer number of steps from 1 to 5 ahead. The while loop should continue as long as neither runner has reached the finish line. In the while loop, print the new position for each runner. For example, “Runner 1 is now at step” and the actual step value. Once the while loop ends, use an if statement to print which runner won the race.
To use the random functions,
import random # then use random.randint()
o
from random import randint # then use randint().
2) Create a long string of text. Let the user input a search term (could be a single
character, word or phrase).
If the search term is not found, print a message that indicates it was not found.
If the search term is found,
• print the number of times it is found in the full text
• print the position of the first occu
ence of the search term
2 b) Let the user input a long string of text. Print the full string entered then check each
character individually using a loop.
If the character is numeric, print the character and the phrase “is numeric”.
If the charcter is lowercase, print the character and the phrase “is lower”.
If the character is uppercase, print the character and the phrase “is uppercase”.
3) Write a function that takes in a string and returns a dictionary with the lette
frequencies in the string. For example, an input string of "Hello" would generate a dictionary
with the following entries { "H": 1, "e": 1, "l": 2, "o": 1 }
3 b) Write a function that takes in a file and returns a dictionary with the word frequencies
in the file.
4) Implement solutions 1, 2 and 4 in Section 2.4 – An Anagram Detection Example from the
Problem Solving with Algorithms and Data Structures online text.
(http:
interactivepython.org
unestone/static/pythonds/AlgorithmAnalysis/AnAnagramDetectionExample.html)
• Import the time module and use its time() function to get the running time of each of
these three algorithms with these parameters:
· Run with a string of length 10.
· Run with a string of length 100.
· Run with a string of length 1000.
You can modify this code from the Car class designed to generate a random VIN of length 16 to generate a random string for these tests.
characterSet = ascii_uppercase + digits
length = 16
VIN = "".join([choice(characterSet) for x in range(length)])
5) Implement a DigitalEvidence class in Python. The class will model a single piece of digital
evidence in either a criminal or civil case. The exact data members included are up to you, but
could be similar to the information captured on a chain of custody form. Include __init__
and __str__ methods, as well as methods to retrieve, set and/or process the data in the class.
Test your class in a main.py file by creating a digital evidence object and testing all the methods
you’ve defined in the class.
References for digital evidence and chain of custody documentation.
• https:
www.nij.gov/topics/forensics/evidence/digital/Pages/welcome.aspx
• https:
esources.infosecinstitute.com/category/computerforensics/introduction/areas-ofstudy
legal-and-ethical-principles/chain-of-custody-in-computer-forensics
• https:
www.oreilly.com/li
ary/view/computer-evidencecollection
XXXXXXXXXX/9781584506997_app01.html
6) Instructions: Add the following functions into the unsorted Linked List class started in class.
I sent the code for the main.py and linkedlist.py in two other word documents since the Linked List is called from the main program.
pop()
Remove and return the value of the last item in the list. You could consider adding a tail data
member to the linked list that will point to the last node, just as the head data member points to the first node in the list. If the list is empty, return None.
index(item)
Find and return the position in the list of the value sent in to the function. The position of the
value in the first node would be 0. If the value is not in the list, return None.
Test the functions in the main program by first creating a list and populating it with different
values, then printing out the return values of the functions, e.g. print(pop()).
Visualgo has visualizations for Linked Lists here: https:
visualgo.net/en/list
7) Instructions: Read in a text file (at least a paragraph of text) and split the text into a list of individual words.Usetwo stacks,one for the whole words, and one for the characters in a word. Reverse the order of wordsin a text, as well as the order of the letters in each word. Print out the reversed text.For example, if the text were “Read in a text file.”, the output would be “.elif txet a ni daeR”.
Visualgo has visualizations for Stackshere: https:
visualgo.net/en/list
Linked List
class Node:
def __init__(self, data):
self.data = data
self.nextnode = None
def set_data(self, newdata):
self.data = newdata
def set_nextnode(self, newnext):
self.nextnode = newnext
def get_data(self):
return self.data
def get_nextnode(self):
return self.nextnode
class LinkedList:
def __init__(self):
self.head = None
# check if empty
def is_empty(self):
return self.head == None
# add a Node to front
def add_front(self, data):
temp = Node(data)
temp.set_nextnode(self.head)
self.head = temp
# size of list
def get_size(self):
cu
ent = self.head
count = 0
while cu
ent != None:
count += 1
cu
ent = cu
ent.get_nextnode()
return count
# search
def search(self, value):
cu
ent = self.head
found = False
while cu
ent != None and not found:
if cu
ent.get_data() == value:
XXXXXXXXXXfound = True
else:
XXXXXXXXXXcu
ent = cu
ent.get_nextnode()
return found
# remove
def remove(self, value):
cu
ent = self.head
found = False
previous = None
while not found:
if cu
ent.get_data() == value:
XXXXXXXXXXfound = True
else:
XXXXXXXXXXprevious = cu
ent
XXXXXXXXXXcu
ent = cu
ent.get_nextnode()
if previous == None:
self.head = cu
ent.get_nextnode()
else:
previous.set_nextnode(cu
ent.get_nextnode())
def print_list(self):
cu
ent = self.head
while cu
ent != None:
print(cu
ent.get_data())
cu
ent = cu
ent.get_nextnode()
class SortedLinkedList:
def __init__(self):
self.head = None
def add(self, value):
cu
ent = self.head
previous = None
stop = False
while cu
ent != None and not stop:
if cu
ent.get_data() > value:
XXXXXXXXXXstop = True
else:
XXXXXXXXXXprevious = cu
ent
XXXXXXXXXXcu
ent = cu
ent.get_nextnode()
temp = Node(value)
if previous == None:
temp.set_nextnode(self.head)
self.head = temp
else:
temp.set_nextnode(cu
ent)
previous.set_nextnode(temp)
def search(self, value):
cu
ent = self.head
found = False
stop = False
while cu
ent != None and not found and not stop:
if cu
ent.get_data() == value:
XXXXXXXXXXfound = True
else:
XXXXXXXXXXif cu
ent.get_data() > value:
XXXXXXXXXXstop = True
XXXXXXXXXXelse:
XXXXXXXXXXcu
ent = cu
ent.get_nextnode()
return found
def print_list(self):
cu
ent = self.head
while cu
ent != None:
print(cu
ent.get_data())
cu
ent = cu
ent.get_nextnode()
Main.py code
from LinkedList import Node, LinkedList, SortedLinkedList
nodeA = Node(50)
print(nodeA.get_data())
print(nodeA.get_nextnode())
print('\n')
myList = LinkedList()
print(myList.is_empty())
print(myList.get_size())
print('\n')
myList.add_front(100)
myList.add_front(40)
myList.add_front(75)
myList.add_front(33)
myList.print_list()
print('\n')
mySortedList = SortedLinkedList()
mySortedList.add(100)
mySortedList.add(40)
mySortedList.add(75)
mySortedList.add(33)
mySortedList.print_list()