I implemented a binary search in Python:
def bisect(seq, goal, lo=0, hi=None):
if hi == None:
hi = len(seq)
while True:
middle = (lo+hi)//2
if seq[middle] == goal:
return middle
elif goal < seq[middle]: hi = middle
elif goal > seq[middle]: lo = middle+1
if lo >= hi:
return -1
It should return the index of the item that is first met. However, when I apply it on a list like this:
seq = [-81, -81, 1, 2, 9, 10, 63, 79]
bisect(seq, -81)
it doesn't return 0 but 1. How can I fix this?
For such a seemingly simple problem, nonetheless, getting the boundary conditions exactly right can be a challenge. So what I normally do in these cases is just use, or more usually, copy and adjust the code in the bisect module. The function you want is bisect_left, since you want the index of the leftmost value if there are more than one, or the index of the insertion point if there is no match.
Here is a copy of the bisect_left function from the Python 3.3 Std Lib:
def bisect_left(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
return lo
if seq[middle] == goal: return middle bails out without considering whether the same value might occur at a lower index. In your example, lo stays 0, hi becomes 7, then 3. When hi is 3, middle is 1, and that meets your condition so 1 is returned. Since any multiple occurrences of goal have to be consecutive to meet the condition that seq is nondecreasing (required for binary search), the easiest thing to do might be:
if seq[middle] == goal:
while middle > lo and seq[middle - 1] == goal:
middle = middle - 1
return middle
Related
Wrote this code in comp sci class and I cant get it to work, it always returns as false every time I run it. Its supposed to be a recursive binary search method... Any idea why it only returns false?
arr = [1,10,12,15,16,122,132,143,155]
def binarysearch(arr,num):
arr2 = []
if (len(arr) == 1):
if (arr[0] == num):
return 1
else:
return 0
for i in range (len(arr)/2):
arr2.append(0)
if (arr[len(arr)/2]>num):
for x in range (len(arr)/2,len(arr)):
arr2[x-(len(arr)/2)]=arr[x]
return binarysearch(arr2,num)
if(arr[len(arr)/2]<num):
for x in range(0, len(arr) / 2 ):
arr2[x] = arr[x]
return binarysearch(arr2, num)
num = raw_input("put number to check here please: ")
if(binarysearch(arr,num)==1):
print "true"
else:
print "false"
You're doing vastly more work than you need to on things that Python can handle for you, and the complexity is masking your problems.
After your base case, you have two if statements, that don't cover the full range—you've overlooked the possibility of equality. Use if/else and adjust the ranges being copied accordingly.
Don't create a new array and copy stuff, use Python's subranges.
Don't keep repeating the same division operation throughout your program, do it once and store the result.
If you want to print True/False, why not just return that rather than encoding the outcome as 0/1 and then decoding it to do the print?
Recall that raw_input returns a string, you'll need to convert it to int.
The end result of all those revisions would be:
def binary_search(arr,num):
if (len(arr) == 1):
return (arr[0] == num)
mid = len(arr) / 2
if (arr[mid] > num):
return binary_search(arr[:mid], num)
else:
return binary_search(arr[mid:], num)
num = int(raw_input("put number to check here please: "))
print binary_search(arr,num)
I'm trying to understand what is wrong with my current solution.
The problem is as follows:
using python 2.7.6"
You have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. if it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.
input: (int list) l = [3, 1, 4, 1]
output: (int) 4311
input (int list) l = [3 ,1 ,4 ,1 ,5, 9]
output: (int) = 94311
This is my code to tackle the problem:
import itertools
def answer(l):
'#remove the zeros to speed combinatorial analysis:'
zero_count = l.count(0)
for i in range(l.count(0)):
l.pop(l.index(0))
' # to check if a number is divisible by three, check if the sum '
' # of the individual integers that make up the number is divisible '
' # by three. (e.g. 431: 4+3+1 = 8, 8 % 3 != 0, thus 431 % 3 != 0)'
b = len(l)
while b > 0:
combo = itertools.combinations(l, b)
for thing in combo:
'# if number is divisible by 3, reverse sort it and tack on zeros left behind'
if sum(thing) % 3 == 0:
thing = sorted(thing, reverse = True)
max_div_3 = ''
for digit in thing:
max_div_3 += str(digit)
max_div_3 += '0'* zero_count
return int(max_div_3)
b -= 1
return int(0)
I have tested this assignment many times in my own sandbox and it always works.
However when I have submitted it against my instructor, I end up always failing 1 case.. with no explanation of why. I cannot interrogate the instructor's tests, they are blindly pitched against the code.
Does anyone have an idea of a condition under which my code fails to either return the largest integer divisible by 3 or, if none exists, 0?
The list always has at least one number in it.
It turns out that the problem was with the order of itertools.combinations(l, b)
and sorted(thing, reverse = True). The original code was finding the first match of n%3 == 0 but not necessarily the largest match. Performing sort BEFORE itertools.combinations allowed itertools to find the largest n%3 == 0.
import math
def is_prime(num):
if num < 2:
return False
for i in range(2, int(math.sqrt(num))+ 1):
if num % i == 0:
return False
return True
Primes seems to be a popular topic but in the book in which I am learning Python, I am on chpt 6 out of 21 and in the iteration chapter which it teaches while loops. I have not learned for loops yet although I understand what they do. So, let's say I have not learned for loops yet and am given only if/elif/else statements and the while loops as my tools. How can I change the for line of code into something more simple using the above tools? While asking this question I quickly came up with this code:
def formula(num):
i = 2
while i >= 2:
return int(math.sqrt(num)+ 1)
def is_primetwo(num):
i = 2
if num < 2:
return False
formula(num)
if num % i == 0:
return False
return True
It works but would this be a simple version of the for loop or is there something even more simple where I do not have to wrap a function within a function?
Absolutely, you do not need a function to replace a for loop.
So you've got this
for i in range(2, int(math.sqrt(num))+ 1):
which is your for loop. Take a second to think what it's doing.
1.) It's taking the variable i, and it's starting it at a value of 2.
2.) It's checking whether to do the loop every time by checking if i is less than the (square root of num) plus 1
3.) Every time through the loop, it adds one to i.
We can do all of these things using a while loop.
Here's the original
for i in range(2, int(math.sqrt(num))+ 1):
if num % i == 0:
return False
let's rename the second and third lines loop contents just so we're focusing on the looping part, not what logic we're doing with the variables i and num.
for i in range(2, int(math.sqrt(num))+ 1):
loop contents
ok, now let's just rearrange it to be a while loop. We need to set i to 2 first.
i = 2
Now we want to check that i is in the range we want
i = 2
while i <= int(math.sqrt(num) + 1):
loop contents
Now we're almost set, we just need to make i actually change, instead of staying at a value of 2 forever.
i = 2
while i <= int(math.sqrt(num) + 1):
loop contents
i = i + 1
Your example seemed to do some of these elements, but this way is a simple way to do it, and no extra function is necessary. It could be the range() function that is confusing. Just remember, the for loop is doing three things; setting a variable to an initial value, checking a condition (one variable is less than another), and incrementing your variable to be one large than previously to run the loop again.
How about something like:
from math import sqrt
def is_prime(num):
if (num < 2):
return False
i = 2
limit = int(sqrt(num) + 1)
while (i <= limit):
if num % i == 0:
return False
i = i + 1
return True
Not sure if this is what you want, but the for loop:
for i in range(2, int(math.sqrt(num))+ 1):
if num % i == 0:
return False
return True
can be expressed as:
i = 2
while i < int(math.sqrt(num))+ 1):
if num % i == 0:
return False
i += 1
return True
Probably a good idea to determine int(math.sqrt(num))+ 1) once:
i = 2
n = int(math.sqrt(num))+ 1)
while i < n:
if num % i == 0:
return False
i += 1
return True
I'm going back and tooling around with Project Euler questions to see if I can speed up my code, this is 003: finding the max prime factor of a really big number.
def is_prime(n):
'''check if n is prime'''
if n == 1: return 0
elif n == 2: return 1
elif n % 2 == 0: return 0
for i in range(3, int(n**0.5) +1, 2):
if n % i == 0:
return 0
else:
return 1
factor_list = []
the_number = 600851475143
for i in range(3, int(the_number**0.5) +1, 2):
if the_number % i == 0: factor_list.append(i)
print factor_list
for i in factor_list:
if is_prime(i) == False: factor_list.remove(i)
print factor_list
print max(factor_list)
The first print call prints: [71, 839, 1471, 6857, 59569, 104441, 486847]
So far, so good, printing the pre-n^0.5 factors of n.
The second print call prints: [71, 839, 1471, 6857, 104441]
Wait, how did 104441 slip through the is_prime function?
The third print call prints the incorrect answer, namely 104441. My question is how is 104441 slipping through?
I believe you have an issue with your for loop. When you use a for-each loop, you usually don't want to remove values because it ends up skipping over some. So I think what is happening is that 59569 gets removed, and then because you remove it, your next i value is 486847.
If you want a working solution, refer to steveha's code.
It's always tricky to try to modify a list while looping over it. It's better and safer to just build the list you need, rather than trying to pull out values you don't need.
This code works perfectly:
factor_list = [n for n in xrange(3, int(the_number**0.5) +1, 2) if the_number % n == 0]
print(factor_list)
prime_factors = [n for n in factor_list if is_prime(n)]
print(prime_factors)
answer = max(prime_factors)
print(answer)
Also, you should be returning False and True from is_prime(), not 0 and 1.
I keep getting this error:
line 4, in timesTwo
IndexError: list index out of range
for this program:
def timesTwo(myList):
counter = 0
while (counter <= len(myList)):
if myList[counter] > 0:
myList[counter] = myList[counter]*2
counter = counter + 1
elif (myList[counter] < 0):
myList[counter] = myList[counter]*2
counter = counter + 1
else:
myList[counter] = "zero"
return myList
I'm not exactly sure how to fix the error. Any ideas?
You are setting the upper-bound of the while loop to the length of myList, which means that the final value of counter will be the length. Since lists are indexed starting at 0, this will cause an error. You can fix it by removing the = sign:
while (counter < len(myList)):
Alternatively, you could do this in a for loop that may be a bit easier to handle (not sure if this fits your use case, so the above should work if not):
def timesTwo(myList):
for index, value in enumerate(myList):
if value is not 0:
myList[index] *= 2
else:
myList[index] = 'zero'
return myList