lab_scatter/transform.py

118 lines
4.1 KiB
Python

# transform.py
# ------------
# By MWC Contributors
# The functions in this module transform data.
# None of them are finished; this is your job!
# Corrected on 9.12.2023 from watching video, August 2_Scatter plot lab live-coding
def maximum(data):
"Returns the largest number in data"
largest = None
for number in data:
if largest is None:
largest = number
if number > largest:
largest = number
return largest
"""Find minimum Data is a list of numbers, such as [3, 5, 7, 6, 4, 2]
number = ?
smallest = ?
example: if number = 3
example: if 3 is the only number, than smallest = 3
then you go through the numbers in the list [3, 5, 7, 6, 4, 2]
so number = 2
lowest = 2, because it's smaller than 3, now 2 becomes the smallest number
For a considerable large amount of numbers you will use the code for the smallest number"""
def minimum(data):
"Returns the smallest number in data"
smallest = None
for number in data:
if smallest is None:
smallest = number
if number < smallest:
smallest = number
return smallest
"""
The "scope of a variable" doesn't relate to the above defined functions, it's separate in it's on function.
smallest and largest changed from "None" to "minimum(data)" and "maximum(data)"
I didn't need to use "for and if else statments. Keep it simple.
I was on track with Last line need to add brackets [ ]
Also, you can define the function on 1 line.
return[minimum(data), maximum(data)]
"""
def bounds(data):
"Returns a list of the smallest and largest numbers in data"
smallest = minimum(data)
largest = maximum(data)
return [smallest, largest]
def clamp(value, low, high):
"""Clamps a value to a range from low to high.
Returns value if it is between low and high.
If value is lower than low, returns low. If value is higher than high, returns high.
"""
"""I did this correctly.
I learned the if, else if, else statement from Java classes. I like using "elif". """
if value < low:
return low
elif value > high:
return high
else:
return value
def ratio(value, start, end):
"""Returns a number from 0.0 to 1.0, representing how far along value is from start to end.
The return value is clamped to [0, 1], so even if value is lower than start, the return
value will not be lower than 0.0.
"""
"""ratio(-1, 0, 10)
-0.1"""
"""Ratio is function to find how much what percentage is finished from start to end.
Find out what's the relationship between how far you've gone from start
to the whole distance you have to go.
unclamped_ratio = (value - start) / (end - start) correct except for unclamped_ratio, I used value.
return amount_already_finished / total_distance_to_cover
I get confused with the wording such as using "clamp and unclamped_ratio." """
unclamped_ratio = (value - start) / (end - start)
return clamp(unclamped_ratio, 0, 1)
def scale(value, domain_min, domain_max, range_min, range_max):
"Given a value within a domain, returns the scaled equivalent within range."
"""This is correct, but for using return range
value = range_min + r * (range_max - range_min)
I had to define what "r" is. I understand. """
r = ratio(value, domain_min, domain_max)
return range_min + r * (range_max - range_min)
"""Points: [[1, 1], [2, 2], [4, 3], [7, 3]]
List of x and y points on a plane. You're probably going plot on a grid.
Can begin with
for x, y points in points: -It's unpacking, the x and y values
... get the x value, and add it to the x_values
the ".append" goes through each x value."""
def get_x_values(points):
"Returns the first value for each point in points."
x_values = []
for x, y in points:
x_values.append(x)
return x_values
def get_y_values(points):
"Returns the second value for each point in points."
y_values = []
for x, y in points:
y_values.append(y)
return y_values