How to fix this loop for special cases in Python - python-3.x

So I got assigned a homework where I have to do the convexHull problem by brute force (this is to then later compare the complexity vs the normal complexHull), and I have this code:
def determineSideLocation(A,B,P):
#Takes point A and B and creates a vector (A ---> B), direction sensitive
d = ((P[0] - A[0])*(B[1] - A[1])) - ((P[1] - A[1])*(B[0] - A[0]))
if d < 0:
#To the left of AB
return -1
elif d > 0:
#To the right of AB
return 1
elif d == 0:
#On AB
return 0
And now I determine if all the points I want to compare are on one side or not:
def isAllPointsOnOneSide(vector, pointList):
pointSideList = []
print("vector: " + str(vector))
print("pointList: " + str(pointList))
for i in pointList:
pointSideList.append(determineSideLocation(vector[0], vector[1], i))
print("pointSideList: " + str(pointSideList))
for j in range(0, len(pointSideList) - 1):
if pointSideList[j] == 0:
continue
elif pointSideList[j+1] == 0:
#2 options:
#1. pointSideList[j+1] is at the end of the list (j+1 = len(pointSideList) - 1)
#2. pointSideList[j+1] is not at the end of the list
if j+1 == (len(pointSideList) - 1):
continue
else:
if pointSideList[j+2] == 0:
if j+2 == (len(pointSideList) - 1):
continue
else:
if pointSideList[j] != pointSideList[j+3]:
return False
else:
continue
elif pointSideList[j] != pointSideList[j+2]:
return False
else:
continue
elif pointSideList[j] != pointSideList[j+1]:
return False
else:
continue
return True
There is the convexHull function but its not where the problem lies. The thing is: in the isAllPointsOnOneSide function, it takes the list of points and makes a list of values relative to the vector, which can only be 1 (to the right), -1 (to the left) and 0 (in the vector).
I got an ankward case where the relative values were this: [-1,0,0,0,0] and the function breaks itself and gives a False value despite being True (all points are on the vector or on one side). I know its in the exception part, where it tries to skip comparing the current value to a 0, but I know its not ideal to keep writing more exceptions to that part.
How can I fix it to avoid the cases where the function breaks? Where it has to compare with 0 at the end of the list

#Heike gave me the answer, and it was rather simple :P
def isAllPointsOnOneSide(vector, pointList):
pointSideList = []
for i in pointList:
pointSideList.append(determineSideLocation(vector[0], vector[1], i))
minValue = min(pointSideList)
maxValue = max(pointSideList)
#If max - min is higher than 1, its a False result.
if maxValue - minValue > 1:
return False
else:
return True
I'm sorry, and after all it was a rather simple problem, but I have been on this for several hours now (brute forcing the convexhull) and it frustated me.
Thanks to Heike again

Related

Recursion functions for factorial

I am writing this code to get the 10! ,however, I believe I have an infinite loop in there since it keeps repeating the same error code. I am trying to find the issue but can not seem to.
def calculatingfactor(num2cal):
"""this fuction will be calculating a mathematical factorial"""
if num2cal == 1:
returnvalue = 1
elif num2cal <= 0:
returnvalue = 0
else:
print("Calculating the facterial of {}".format(num2cal))
variable2 = calculatingfactor(num2cal - 1)
returnvalue = calculatingfactor(num2cal*variable2)
return
#main code
first_fact=calculatingfactor(10)
print (first_fact)
The recursive case of your code looks incorrect to me. You should be calling the same function with num2cal decremented by one, then returning the current value multiplied by whatever that recursive call returned.
def calculatingfactor(num2cal):
if num2cal == 1:
return 1
elif num2cal <= 0:
return 0
else:
print("Calculating the facterial of {}".format(num2cal))
variable2 = calculatingfactor(num2cal - 1)
return num2cal*variable2
# main code
first_fact=calculatingfactor(10)
print (first_fact)

Finding the logical error in my code to get first 50 primes

I'm trying to write my own formula to find a prime number, but it does not completely work and I cannot find the flaw in my logic. Bare in mind I have taken a look around but cannot find an algorithm that I find similar to mine.
My code:
#Challenge 7
prime = []
num = 0
found = False
while found == False:
if num == 0 or num == 1:
num+=1
else:
for value in range(2, num+1):
if len(prime) == 50:
print('Found all')
found = True
break
if num % value == 0:
num+=1
else:
if num not in prime:
prime.append(num)
else:
pass
print(prime)
This code works for first few primes (3, 5, 7...)
but it also gives incorrect values like 10, and I don't understand why. If someone could explain it to me so that I can understand where the logical mistake is, I'd appreciate it.
The error comes from this part
if num % value == 0:
num+=1
else:
if num not in prime:
prime.append(num)
else:
pass
You assume that the integer is a prime as soon as we find the first occurence of a non-divisor. But the def for primes is that every integer in the interval [2..prime] is a non-divisor. How do we check if any number does not have any divisors?
def isPrime(x):
for v in range(2, x):
if (x % v == 0):
return False;
return True;
Something like this would work to check if any given number is a prime or not. And since we now have taken the isPrime part out of the main loop, we no longer need a for loop inside the while. Something like this would do
def isPrime(x):
for v in range(2, x):
if (x % v == 0):
return False;
return True;
prime = [}
num = 2
found = False
while found == False:
if len(prime) == 50:
print("found all")
found = True
break
if(isPrime(num)):
print(num)
prime.append(num)
num+=1
else:
num+=1
If you set a breakpoint for when num == 10 you will see the problem clearly.
When you start doing you division check inside of for value in range(2, num + 1): the second number is 3, so num (10) modulo value (3) is 1, which is your test for determining a prime. What your test should be is that it not divisible by any number less than it (less than half is actually sufficient since you check with 2 anyway).
So, consider instead:
else:
is_indivisible = True
# loop through all numbers less than it not including itself
# (because x % x == 0)
for value in range(2, num - 1):
# it is only indivisible if it was previously indivisible
# And the check is same as before, modulo != 0
is_indivisible = is_indivisible and (num % value != 0)
if not is_indivisible:
break
# if it is indivisible and it doesn't exist in prime list yet
if is_indivisible and num not in prime:
prime.append(num)
# move on to the next number
num += 1

Using Recursive Functions in Python to find Factors of a Given Number

Have tried searching for this, but can't find exactly what I'm looking for.
I want to make a function that will recursively find the factors of a number; for example, the factors of 12 are 1, 2, 3, 4, 6 & 12.
I can write this fairly simply using a for loop with an if statement:
#a function to find the factors of a given number
def print_factors(x):
print ("The factors of %s are:" % number)
for i in range(1, x + 1):
if number % i == 0: #if the number divided by i is zero, then i is a factor of that number
print (i)
number = int(input("Enter a number: "))
print (print_factors(number))
However, when I try to change it to a recursive function, I am getting just a loop of the "The factors of x are:" statement. This is what I currently have:
#uses recursive function to print all the letters of an integer
def print_factors(x): #function to print factors of the number with the argument n
print ("The factors of %s are:" % number)
while print_factors(x) != 0: #to break the recursion loop
for i in range(1,x + 1):
if x % i == 0:
print (i)
number = int(input("Enter a number: "))
print_factors(number)
The error must be coming in either when I am calling the function again, or to do with the while loop (as far as I understand, you need a while loop in a recursive function, in order to break it?)
There are quite many problems with your recursive approach. In fact its not recursive at all.
1) Your function doesn't return anything but your while loop has a comparision while print_factors(x) != 0:
2) Even if your function was returning a value, it would never get to the point of evaluating it and comparing due to the way you have coded.
You are constantly calling your function with the same parameter over and over which is why you are getting a loop of print statements.
In a recursive approach, you define a problem in terms of a simpler version of itself.
And you need a base case to break out of recursive function, not a while loop.
Here is a very naive recursive approach.
def factors(x,i):
if i==0:
return
if x%i == 0:
print(i)
return factors (x,i-1) #simpler version of the problem
factors(12,12)
I think we do using below method:
def findfactor(n):
factorizeDict
def factorize(acc, x):
if(n%x == 0 and n/x >= x):
if(n/x > x):
acc += [x, n//x]
return factorize(acc, x+1)
else:
acc += [x]
return acc
elif(n%x != 0):
return factorize(acc, x+1)
else:
return acc
return factorize(list(), 1)
def factors(x,i=None) :
if i is None :
print('the factors of %s are : ' %x)
print(x,end=' ')
i = int(x/2)
if i == 0 :
return
if x % i == 0 :
print(i,end=' ')
return factors(x,i-1)
num1 = int(input('enter number : '))
print(factors(num1))
Recursion is a functional heritage and so using it with functional style yields the best results. This means avoiding things like mutations, variable reassignments, and other side effects. That said, here's how I'd write factors -
def factors(n, m = 2):
if m >= n:
return
if n % m == 0:
yield m
yield from factors(n, m + 1)
print(list(factors(10))) # [2,5]
print(list(factors(24))) # [2,3,4,6,8,12]
print(list(factors(99))) # [3,9,11,33]
And here's prime_factors -
def prime_factors(n, m = 2):
if m > n:
return
elif n % m == 0:
yield m
yield from prime_factors(n // m, m)
else:
yield from prime_factors(n, m + 1)
print(list(prime_factors(10))) # [2,5]
print(list(prime_factors(24))) # [2,2,2,3]
print(list(prime_factors(99))) # [3,3,11]
def fact (n , a = 2):
if n <= a :
return n
elif n % a != 0 :
return fact(n , a + 1 )
elif n % a == 0:
return str(a) + f" * {str(fact(n / a , a ))}"
Here is another way. The 'x' is the number you want to find the factors of. The 'c = 1' is used as a counter, using it we'll divide your number by 1, then by 2, all the way up to and including your nubmer, and if the modular returns a 0, then we know that number is a factor, so we print it out.
def factors (x,c=1):
if c == x: return x
else:
if x%c == 0: print(c)
return factors(x,c+1)

What is wrong with my function? Giving me a blank output

def get_nearest_multiple(minnum, factor):
"""
function get_nearest_multiple will calculate the nearest multiple that is greater than the min. value,
Parameters are the minimum value and factor,
Will return the ans - the nearest multiple
"""
ans = 0
x = 1
while ans < minnum:
if minnum == 0:
ans = 0
else:
ans = x * factor
x += 1
return ans
get_nearest_multiple(0, 1)
if __name__ == '__main__':
get_nearest_multiple(0, 1)
Can't seem to figure out why my function doesn't print out anything. The output doesn't even show up as an error. Just blank.
Nowhere in your code do you have a print() statement which is required to produce an output in the console

How to fix "maximum recursion depth exceeded" error in python code?

While I was running the code, I got a "maximum recursion depth exceeded in comparison" error. I'm not exactly sure which part of the code to look at to fix this problem. This numToBinary function is basically supposed to convert a number n to a binary number with bit size k. I would greatly appreciate any input on how I can resolve this issue!
def numToBinary(k, n):
''' converts number to binary number bit size k'''
def binary(n):
if n == 0:
return ''
elif n%2 == 1:
return binary(n/2)+'1'
else:
return binary(n/2)+ '0'
temp = binary(n)
if len(temp) <= k:
answer = '0' * (k - len(temp)) + temp
elif len(temp) > k:
answer = temp[-k:]
return answer
print (numToBinary(6, 10))
You need floor division, double /, in python3 / does truediv so you are getting floats from n/2:
def binary(n):
if n == 0:
return ''
elif n%2 == 1:
return binary(n//2) + '1' # // floor
else:
return binary(n//2)+ '0' # // floor
Once you make the change, it will work fine:
In [50]: numToBinary(6, 10)
Out[50]: '001010'
You can also use else in place of the elif, if the len of temp is not <= then it has to be greater than:
def numToBinary(k, n):
''' converts number to binary number bit size k'''
def binary(n):
if n == 0:
return ''
elif n % 2 == 1:
return binary(n//2)+'1'
else:
return binary(n//2) + '0'
temp = binary(n)
if len(temp) <= k:
answer = '0' * (k - len(temp)) + temp
else:
answer = temp[-k:]
return answer
If you wanted to see exactly what was happening you should put a print in you own code, if you added a print(n) in binary you would see a lot of output like:
5.125332723668738e-143
2.562666361834369e-143
1.2813331809171846e-143
6.406665904585923e-144
3.2033329522929615e-144
Which meant you eventually hit the recursion limit.

Resources