-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
35 lines (32 loc) · 1.3 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Authors: Ben Huenemann and Michael Keyser
Date: 10/17/20
This script contains functions that generate sequences to be searched for the longest subsequence.
It also contains a function used for graphing the sequences.
"""
import random as r
import math
"""
Generates a list of n random floating point numbers within lower_bound and upper_bound
"""
def generate_random_sequence(lower, upper, n):
sequence = []
for i in range(n):
sequence.append(r.uniform(lower,upper))
return sequence
"""
Generates a sequence provided the definition and number of elements passed by the user
"""
def generate_specific_sequence(sequence_definition, num_elements):
sequence = []
for n in range(num_elements):
n = n + 1 # sequences start at 1 instead of 0 and end at num_elements instead of num_elements - 1
val = float(eval(sequence_definition)) # be aware that eval can be a potentially dangerous function to use
sequence.append(val)
return sequence
def graph(indices, longest_seq, seq):
import matplotlib.pyplot as plt # import inline is done here in case user does not have matplotlib installed, can choose to not graph
fig, ax = plt.subplots()
ax.scatter([i + 1 for i in range(len(seq))], seq, color = 'b')
ax.plot(indices, longest_seq, color = 'r')
plt.show()