On video 3 of the course, "if ans**3 == abs(x): break" why even insert this line of code and "break" afterword? Question 2 "for ans in range(0, abs(x)+1):" The professor explain the code like this, "(x,y) x, x+1 ........... ,y-1". How is the y involved? There is no "Y". It just pops from thin air.
where can i watch the course?
Q1: This line of code is used to improve the efficiency of program. Because the while loop would be stopped as soon as the ans meets the restriction (ans**3 == abs(x)). Without these part, the program would search the whole searching space (ans**3 <abs(x)) , even though we have already found the answer. Q2: The x is the first int of range(x, y), and the y is the second. For range (0, abs(x)+1), x is 0, and y is abs(x)+1
q1 I still don't understand why break is needed or the line directly above break is needed. q2 If x = 0 and y = x+1 isn't x always going to be greater so it should be 0, 1, 2 ,3 ,4 ,5 I don't get it. I was just using x = 0 for an example of x
In order to watch the course here is a link https://www.youtube.com/watch?v=ggxY20cXql8&feature=youtu.be&t=1345
Like Gakun said, it's to improve efficiency. If you write two programs that get the same answer, but one terminates faster, you might as well use this one. That line of code and the break statement aren't *needed*, but they're basically saying "if you found the right answer, stop searching". As for range, you can use it in multiple ways, and one of them is to use it with two arguments, two numbers basically. For instance, range(1, 5). This would give you the numbers 1, 2, 3, 4. (and not 5, this is important) range(2, 8) would "produce" 2, 3, 4, 5, 6, 7. If you want to be more general, you can represent what I've just said with range(x, y), where both x and y stand for numbers, with y being greater than x, and it returns the list of numbers x, x+1, x+2 etc etc up until y-1. So if x is 1 and y is 5, it would give you all the number there are between 1 and 5-1=4, hence: range(1, 5) returns 1, 2, 3, 4
Its so when that is true, it immediately stops as to make it run smoother
Join our real-time social learning platform and learn together with your friends!