I have a problem regarding the MIT OCW Python Lecture 3.
According to simple math, the code she uses shouldn't be succesful.
## EXAMPLE: approximate cube root
####################
#cube = 27
##cube = 8120601
##cube = 10000
#epsilon = 0.1
#guess = 0.0
#increment = 0.01
#num_guesses = 0
## look for close enough answer and make sure
## didn't accidentally skip the close enough bound
#while abs(guess**3 - cube) >= epsilon and guess <= cube:
# guess += increment
# num_guesses += 1
#print('num_guesses =', num_guesses)
#if abs(guess**3 - cube) >= epsilon:
# print('Failed on cube root of', cube, "with these parameters.")
#else:
# print(guess, 'is close to the cube root of', cube)
This is the code she uses, the problem I'm having is understanding this part:
while abs(guess**3 - cube) >= epsilon and guess <= cube:
# guess += increment
If guess is 0.0, cube is 27 and increment is 0.01 then the math by this term should be:
abs(0**3 - 27) = 27 #----- This is fine according to the code but the next step would be:#
abs(27.01**3 - 27) = 19677.878101
This should stop the loop from working any further. My understanding is obviously wrong somewhere but I can't see where!
Please halp...
You can try using Python visualizer to see how the value of guess changes with each iteration. Try here.
Related
I'm a python newbie and currently learning on basics with it. I've come across this task and I would really love to solve it so I can understand how to do similar things in the future. Here is how it goes : Write a function for checking the speed of drivers. This function should have one parameter: speed.
If speed is less than 70, it should print “Ok”.
Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: “Points: 2”.
If the driver gets more than 12 points, the function should print: “License suspended”
This is what I came up with currently, but can't solve bolded part of text. Would appreciate it if you could help me. Thanks !
def speed_check(speed):
warning_point = 0
max_speed = 70
if (speed <= max_speed):
print ("OK")
elif (speed >=130):
print ("Licence suspended, you total warning points is 12.")
elif ("something must go here"):
warning_point +=1
print("Current warning point is {0}".format(warning_point))
speed_check(75)
A global variable will be needed to keep track of how many warning points have been awarded. Below should do it, comment if it makes sense or if there are parts you want explaining.
def speed_check(speed):
global warning_point
max_speed = 70
if speed <= max_speed:
print ("OK")
else:
warning_point += (speed-max_speed) // 5
print("Current warning point is {0}".format(warning_point))
if warning_point >= 12:
print("Licence suspended, you total warning points is at least 12.")
warning_point = 0
speed_check(75)
speed_check(85)
speed_check(115)
You can subtract the speed limit, divide by 5 and then add 1 offset because 1 / 5 = 0
import math
def speed_check(current_speed):
max_speed = 70
if current_speed <= max_speed:
print("OK")
elif (current_speed >=130):
print ("Licence suspended, you total warning points is 12.")
else:
points = math.floor((current_speed - max_speed) / 5) + 1
print("Current warning point is {0}".format(points))
You could divide the speedlimit, 70, and the current speed, 80, by the amount per each point. Then you could just subtract those to get points.
import math
def speed_check(current_speed):
max_speed = 70
if current_speed <= max_speed:
print("OK")
elif (current_speed >=130):
print ("Licence suspended, you total warning points is 12.")
else:
points = (current_speed - max_speed) // 5
print(f"Points: {int(points)}")
Little new here and any help would be appreciated.
I have been tooling around with this code for a while now and I cant seem to wrap my head around it. Im fairly new to python so I dont quite know or remember all the tricks yet/skills.
So the question at hand:
Equation: {x_(n+1) = x_n * r * (1- x_n)}
With x_n between (0,1) and r between (0,4).
The goal here is to make a loop function that will gather a value for 'x_n' and 'r' and spit out the iteration 'n' and the current 'x_n+1'; i.e. print(n , x_n+1), at each 'n' step while checking to see if the new value is within 0.0000001 of the old value.
If it settles on a fixed point within 20,000 (0.0000001), print the final 'n' + message. If not then and goes to 20,000 then print another message.
All i have so far is:
import math
x_o=float(input("Enter a 'seed' value: "))
r=float(input("Enter an 'r' value: "))
x_a=((x_o + 0) * r * (1-(x_o + 0)))
while x_a != (0.0000001, x_o , 0.0000001):
for n in range(0,99):
x_a=((x_o + n) * r * (1-(x_o + n)))
print(n , x_a)
I'm pretty sure this is no where close so any help would be awesome; if you need any more info let me know.
Much appreciated,
Genosphere
You could write a generator function and use it directly in your for loop. If you need to keep track of the rank of intermediate values you can use enumerate on the generator.
def fnIter(fn,x,delta=0.000001):
while True:
yield x
prev,x = x,fn(x)
if abs(x-prev)<delta:break
output:
r = 2
seed = 0.1
for i,Xn in enumerate(fnIter(lambda x:x*r*(1-x),seed)):
print(i,Xn)
0 0.1
1 0.18000000000000002
2 0.2952
3 0.41611392
4 0.4859262511644672
5 0.49960385918742867
6 0.49999968614491325
7 0.49999999999980305
To implement the maximum iteration check you can either add a conditional break in the loop or use zip with a range:
maxCount = 20000
n,Xn = max(zip(range(maxCount+1),fnIter(lambda x:x*r*(1-x),seed)))
if n < maxCount:
print(n,Xn)
else:
print(Xn,"not converging")
This is an exponentially-weighted moving average. Pandas has a function for this: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html
You have a good start so far. You might be overthinking it, though.
The following approach just tries to generate this sequence for 20,000 terms. Each time, it checks whether the new value is within 0.0000001 from the previous value. If so, it breaks out of the loop and prints that. If not, it uses python's for/else construct to print a different value. Note the different levels of indentation.
x_0 = float(input("enter a 'seed' value: "))
r = float(input("enter an 'r' value: "))
x_m = x_0 # placeholder for 'previous value'
delta = 0.0000001
# Try to calculate 20 thousand terms of this sequence
# We will break out of the loop early if our x_n converges
for _ in range(20000):
x_n = x_m * r * (1 - x_m)
if abs(x_n - x_m) < delta:
print("Settled on value for x_n: ", x_n)
break
else:
x_m = x_n # move forward to the next value
else:
print("x_n did not converge in 20000 terms")
Good day,
In each iteration step, I have a p1 that describe the location of each person. p1 is a tuple, such that p1 = (x_point, y_point), p1 describes the location of a person in frame i goes.
Based on this article, https://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/ between line 95 to 109. I am trying to modify the lines in 95 to 109 to measure the distance difference of a person in terms of movement.
The problem can be reproduced as following code, suppose I am getting p1 as each i iteration goes (Originally p1 is the value supplied by SORT Tracking). Since I am dealing with a video with approximately 29 fps as well as multiple objects. Based on following code (inner for loop j), it might provide a false result as following image?
EDIT: It appears to me that inner loop fails to handle multiple objects detection as sample image provided.
Thank you for your time as well.
from collections import deque
from random import randint
import numpy as np
(direction_x, direction_y) = (0, 0)
direction = ""
points_list = deque(maxlen=32)
def sample_of_p1():
return (randint(0, 100),randint(0, 100))
for i in range(100):
p1 = sample_of_p1()
points_list.appendleft(p1)
for j in range(1, len(points_list)):
if(i >= 10):
direction_x = points_list[-10][0] - points_list[j][0]
direction_y = points_list[-10][1] - points_list[j][1]
if np.abs(direction_x) > 0:
dirx = "Right" if np.sign(direction_x) == 1 else "Left"
if np.abs(direction_y) > 0:
diry = "Top" if np.sign(direction_y) == 1 else "Bottom"
if dirx != "" and diry != "":
direction = "{} {}".format(diry, dirx)
else:
direction = dirx if dirx != "" else diry
else:
continue
the code seems to compute correctly but there are some optimizations you can make.
You can put the condition if i >= 10 outside of the loop for j, it is a little optimization but more elegant.
if i >= 10:
for j in range(1, len(points_list)):
//some code
else:
continue
Also, you don't define dirx and diry before the conditions, so you program may throw an exception if you don't move along one axis. In the article, they are initialized at line 109.
Finally, the condition np.abs(direction_x) > 0 seems a bit loose. Usually, when you want to define a movement, you set a minimum value (20 in the article, line 113) to catch a significant movement, and not just a shiver or a negligible movement.
Hope that helps.
My goal is to make a program that takes an input (Battery_Capacity) and ultimately spits out a list of the (New_Battery_Capacity) and the Number of (Cycle) it takes for it ultimately to reach maximum capacity of 80.
Cycle = range (160)
Charger_Rate = 0.5 * Cycle
Battery_Capacity = float(raw_input("Enter Current Capacity:"))
New_Battery_Capacity = Battery_Capacity + Charger_Rate
if Battery_Capacity < 0:
print 'Battery Reading Malfunction (Negative Reading)'
elif Battery_Capacity > 80:
print 'Battery Reading Malfunction (Overcharged)'
elif float(Battery_Capacity) % 0.5 !=0:
print 'Battery Malfunction (Charges Only 0.5 Interval)'
while Battery_Capacity >= 0 and Battery_Capacity < 80:
print New_Battery_Capacity
I was wondering why my Cycle = range(160) isn't working in my program?
Your first problem is that you have the first two lines in the wrong order. You need a "Cycle" variable to exist before you can use it.
You'll still get an error when you swap them, though. You can't multiply a list by a float. A list comprehension is more what you want:
Charger_Rate = [i * .5 for i in Cycle]
As far as I can tell, the range(160) part is fine.
Updated my new code at the bottom of the page as an answer.
So for my CS 170 class, we have to make a program that has user input for less than $10 and change is returned in the least amount of coins, no bills or 50 cent pieces. For the most part the program does well except when you run into a x.x0
e.g.:
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Amount due:
7.80
Amount in return
2.20.
Quaters in return 8.
Dimes in return 0.
Nickels in return 4.
>>>
the program completely skips over the dimes section and straight to nickels providing 4 as a solution when the least amount should be 8 quarters, 2 dimes and end. Also I'm not really skilled in loops too much, but I know this would be possible and a lot shorter code, and cleaning up the code advice would be nice as well. Thanks for any help!
# optional.py
# Calculating the least amount of change in return for a $10 bill.
## amount due
due = input("Amount due:\n ")
## if amount is more than 10, exit program
if due > 10.00:
print "Please enter a number lower then 10.00."
exit()
## if amount is less than 0, exit program
if due < 0:
print "Please enter a number greater than 0.00."
exit()
## subtract amount from 10
else:
change = 10.00 - due
print "Amount in return\n %0.2f." % change
## if amount is 0, no change
if change == 0:
print "No change in return."
## passes expression if previous not met
pass
elif change >= .25:
## setting q, dividing change by .25
q = change / .25
## maaking q an integer
quaters = int(q)
print "Quaters in return %r." % quaters
## subtracting quaters from chane
change = change - (quaters *.25)
if change < .10:
pass
elif change >= .10 <= .24:
d = change * .1
dimes = int(d)
print "Dimes in return %r." % dimes
change = change - (dimes * .1)
if change < .05:
pass
elif change >=.05 <=.09:
n = change / .05
nickels = int(n)
print "Nickels in return %r." % nickels
change = change - (nickels * .05)
if change == .01:
pennies = change / .01
print "Pennies in return %r." % pennies
elif change >=.01 <=.04:
p = change / .01
print "Pennies in return %0.0f." % p
There are a few changes you could make to clean up this code, and one of them might fix your problem. First, pass does absolutely nothing. It is usually used as a placeholder for a loop or function that will be filled in later. Also, the conditions of your elif statements are mutually exclusive with the if statements they follow, so
if change == 0:
print "No change in return."
## passes expression if previous not met
pass
elif change >= .25:
## setting q, dividing change by .25
q = change / .25
## maaking q an integer
quaters = int(q)
print "Quaters in return %r." % quaters
## subtracting quaters from chane
change = change - (quaters *.25)
can be rewritten as
if change >= .25:
## setting q, dividing change by .25
q = change / .25
## making q an integer
quaters = int(q)
print "Quaters in return %r." % quaters
## subtracting quaters from change
change = change - (quaters *.25)
for each if/elif statement. Also, in the statement
if change >=.01 <=.04:
you are testing whether
change >= .01 and .01 <= .04
To make it do what you want, the statement should be rewritten as
if .01 <= change <= .04
In addition, you are using floating point numbers, which often lead to rounding errors. To avoid these errors, I would suggest either representing your money as an integer number of cents and multiply all of the numbers in your problem by 100 or use something with fixed point numbers like python's decimal module.
This is not doing what you expect:
elif change >= .10 <= .24:
It looks like you intend something like:
elif change >= .10 and change <= .24:
or Python also supports:
elif .10 <= change <= .24:
However, you will next run into floating point rounding problems of various kinds. I suggest you first convert the input number to an integer number of cents, and perform all your calculations in cents. Avoid floating point numbers when dealing with money.
So I got it worked out in a better format with cleaner print code. Thanks for the help guys!
If anyone wants to know the difference between the 2 codes, it's getting it out of floating point like the others suggested and converting what is needed to integers, multiplying the integers by the specific amount, say a quarter, and then subtracting int * coin/bill from the change. Worked out well. I tried experimenting with a for statement, but that didn't turn out too well since I don't know a lot about it yet either. Until next time...
Again thanks guys!
Here's the finished code for anyone wondering about it:
import sys
due = input("Please enter the amount due on the item(s):\n ")
# if over $10, exit
if due > 10:
print "Please enter an amount lower then 10."
sys.exit(1)
## if under/equal 0, exit
if due <= 0:
print "Please enter an amount greater than 0."
sys.exit(2)
## 10 - due = change, converts change into cents by * by 100 (100 pennies per dollar)
else :
change = 1000 - (due * 100)
## if change is 0, done
if change == 0:
print "No change in return!"
## if not 0 makes change2 for amount in return
else:
change2 = change / 100
print "Amount in return:\n $%.2f." % change2
## if change > 500, subract 500 and you get 1 $5 bill
if 500.0 <= change:
bill_5 = change / 500
b5 = int(bill_5)
change = change - 500
## if change is over 100, change divided by 100 and subtracted from change for quaters
if 100.0 <= change:
dollars = change / 100
dollar = int(dollars)
change = change - (dollar * 100)
if 25 <= change < 100:
quaters = change / 25
quater = int(quaters)
change = change - (quater * 25)
if 10 <= change <= 24:
dimes = change / 10
dime = int(dimes)
change = change - (dime * 10)
if 5 <= change < 10:
nickels = change / 5
nickel = int(nickels)
change = change - (nickel * 5)
if 0 < change < 5:
pennies = change / 1
penny = int(pennies)
change = change - (penny * 1)
print "Change in return:\n $5:%i\n $1:%i\n Quaters:%i\n Dimes:%i\n Nickels:%i\n Pennies:%i " % (
b5, dollar, quater, dime, nickel, penny )
if 0 >= change:
print "Done!"