What is the difference between the following seemingly same two python functions? - python

I'm new to data structures and algorithms, so I was kind of confused when I was writing a function to find a pair of numbers from two arrays (one from each array) that the absolute value of difference of the two numbers is the smallest. My first draft was version 2, but it did not pass all the tests. However, when I assigned arrayOne[i] to firstNum and arrayTwo[j] to secondNum, it passed all the test cases. Could anyone explain what happened exactly? Since I thought these two versions were exactly the same.
Version 1:
def smallestDifference(arrayOne, arrayTwo):
arrayOne.sort()
arrayTwo.sort()
i = j = 0
smallest = float('inf')
current = float('inf')
smallestPair = []
while i < len(arrayOne) and j < len(arrayTwo):
firstNum = arrayOne[i]
secondNum = arrayTwo[j]
if firstNum < secondNum:
current = abs(firstNum - secondNum)
i += 1
elif firstNum > secondNum:
current = abs(firstNum - secondNum)
j += 1
else:
return [firstNum, secondNum]
if current < smallest:
smallest = current
smallestPair = [firstNum, secondNum]
return smallestPair
Version 2:
def smallestDifference(arrayOne, arrayTwo):
arrayOne.sort()
arrayTwo.sort()
i = j = 0
smallest = float('inf')
current = float('inf')
smallestPair = []
while i < len(arrayOne) and j < len(arrayTwo):
if arrayOne[i] < arrayTwo[j]:
current = abs(arrayOne[i] - arrayTwo[j])
i += 1
elif arrayOne[i] > arrayTwo[j]:
current = abs(arrayOne[i] - arrayTwo[j])
j += 1
else:
return [arrayOne[i], arrayTwo[j]]
if current < smallest:
smallest = current
smallestPair = [arrayOne[i], arrayTwo[j]]
return smallestPair

In version 1, you are indexing arrayOne and arrayTwo for your first if,elif,else statement which is fine, however you change the value of i/j so if the program hits the if current < smallest part of the algorithm, the value of i/j has changed.
For example lets say i and j where both 0 and arrayOne[0] < arraytwo[0], you would increment i and continue on. Once you get to the bottom and you want to delclare smallestPair, you are now setting the value to [arrayOne[1], arrayTwo[0] rather than [arrayOne[0], arrayTwo[0] because you incremented i.
Version one declares the two variables and uses those declarations for the WHOLE function.

Related

Eliminating nearest elements from a list or range

So what I have to do is find the smallest number among those that are in the list, and remove from the list both that number and whichever of its current immediate neighbors is larger. This function should repeat until the largest number in the original list gets eliminated and I have to return the number of times it took to achieve this goal. For example, [1,6,4,2,5,3] would remove element 1 and its current largest neighbor element which is 6, thus reaching the goal in 1 step. My code worked for a list but it did not work when I inputted a range in items. So could anybody help?
def elements(items):
max_value = max(items)
count = 0
while max_value in items:
n = len(items)
right_of_items = 0
left_of_items = 0
min_value = min(items)
min_value_index = items.index(min_value)
if (min_value_index -1 >= 0):
left_of_items = items[min_value_index-1]
if (min_value_index+1 < n):
right_of_items = items[min_value_index+1]
if left_of_items > right_of_items:
items.remove(left_of_items)
items.remove(min_value)
count += 1
else:
items.remove(right_of_items)
items.remove(min_value)
count += 1
return count
The problem with range is that it doesn't support access to a specific item.
So that, you either need to build a list from that range, or use more advanced algorithm which will be able to find the solution in a single pass.
As we won't know the smallest and largest items unless we're analysed the whole list, I guess, there's no such algorithm, that could solve in less than 2 passes, so consider just doing
items = list(items)
at the beginning of your function.
Also I would suggest to not to store the values of a smallest and largest items themselves. Better store their positions. So that you'll be able to access (and remove) neighbours much more effeciently. Like:
def find_largest_neighbour(lst, pos):
if pos <= 0:
return pos + 1
if pos + 1 >= len(lst):
return pos - 1
if lst[pos - 1] <= lst[pos + 1]:
return pos - 1
else:
return pos + 1
def find_min_and_max(lst):
min_pos = max_pos = 0
for i, v in enumerate(lst):
if v < lst[min_pos]:
min_pos = i
if v > lst[max_pos]:
max_pos = i
return min_pos, max_pos
def solve(lst):
number_of_iterations = 0
while True:
number_of_iterations += 1
min_pos, max_pos = find_min_and_max(lst)
max_pos_about = find_largest_neighbour(lst, min_pos)
if max_pos_about == max_pos:
# The largest item found about the smallest, we're done
break
else:
# Getting rid of the smallest item and its largest bro
del lst[min_pos]
del lst[max_pos_about]
# as we used index lookups above, we
# won't search through the list twice
return number_of_iterations

Finding sum of a fibonacci consistent subarray

We have an input integer let's say 13. We can find consistent subarray of fibonacci numbers that sums to 10 - [2,3,5]. I need to find next number that is not a sum of consistent subarray. In this case this number will be 14. I have this code, but the catch is, it can be optimized to not iterate through all of the N's from starting Left Pointer = 1 and Right Pointer = 1 but somehow "import" from previous N and i have no clue how to do it so maybe someone smarter might help.
def fib(n):
if n == 1: return 1
if n == 2: return 1
return fib(n-1) + fib(n-2)
def fibosubarr(n):
L_pointer = 1
R_pointer = 2
sumfibs = 1
while sumfibs != n:
if sumfibs > n and L_pointer < R_pointer:
sumfibs -= fib(L_pointer)
L_pointer += 1
elif sumfibs < n and L_poiner < R_pointer:
sumfibs += fib(R_pointer)
R_pointer += 1
else: return False
return True
n = int(input())
while fibosubarr(n):
n += 1
print(n)
Here's a technique called "memoizing". The fib function here keeps track of the current list and only extends it as necessary. Once it has generated a number, it doesn't need to do it again.
_fib = [1,1]
def fib(n):
while len(_fib) <= n:
_fib.append( _fib[-2]+_fib[-1] )
return _fib[n]
With your scheme, 200000 caused a noticeable delay. With this scheme, even 2 billion runs instantaneously.
To get the next subarray sum, you only need one call of the function -- provided you keep track of the least sum value that was exceeding n.
I would also use a generator for the Fibonacci numbers:
def genfib():
a = 1
b = 1
while True:
yield b
a, b = b, a + b
def fibosubarr(n):
left = genfib()
right = genfib()
sumfibs = next(right)
closest = float("inf")
while sumfibs:
if sumfibs > n:
closest = min(sumfibs, closest)
sumfibs -= next(left)
elif sumfibs < n:
sumfibs += next(right)
else:
return n
return closest
Now you can do as you did -- produce the next valid sum that is at least the input value:
n = int(input())
print(fibosubarr(n))
You could also loop to go from one such sum to the next:
n = 0
for _ in range(10): # first 10 such sums
n = fibosubarr(n+1)
print(n)

Grab 'n' numbers from a given list of numbers with minimum difference between them

I put up a similar question a few hours ago, albeit with a few mistakes, and my poor understanding, admittedly
So the question is, from a given list of indefinite numbers, I'm supposed to take an input from the user, say 3, and grab 3 numbers wherein the numbers have the least difference between them.
def findMinDiff(arr):
# Initialize difference as infinite
diff = 10**20
n = len(arr)
# Find the min diff by comparing difference
# of all possible pairs in given array
for i in range(n-1):
for j in range(i+1,n):
if abs(arr[i]-arr[j]) < diff:
diff = abs(arr[i] - arr[j])
# Return min diff
return diff
def findDiffArray(arr):
diff = 10**20
arr_diff = []
n = len(arr)
for i in range(n-1):
arr_diff.append(abs(arr[i]-arr[i+1]))
return arr_diff
def choosingElements(arr, arr_diff):
arr_choose = []
least_index = 0
least = arr_diff[0]
least_index_array = []
flag = 0
flag2 = 0
for z in range(0,3):
for i in range(0,len(arr_diff)-1):
if arr_diff[i] < least:
if flag > 0:
if i == least_index:
continue
least = arr_diff[i]
least_index = i
least_index_array.append(i)
arr_choose.append(arr[i])
flag += 1
arr_choose.append(arr[i+1])
flag += 1
print("least index is", least_index)
return arr_choose
# Driver code
arr = [1, 5, 3, 19, 18, 25]
arr_diff = findDiffArray(arr)
arr_diff2 = arr_diff.copy()
item_number = int(input("Enter the number of gifts"))
arr_choose = choosingElements(arr, arr_diff2)
print("Minimum difference is " + str(findMinDiff(arr)))
print("Difference array")
print(*arr_diff, sep = "\n")
print("Numbers with least difference for specified items are", arr_choose)
This is how much I've tried, and I've thought to find the difference between numbers, and keep picking ones with the least difference between them, and I realised that my approach is probably wrong.
Can anybody kindly help me out? Thanks!
Now, I'm sure the time complexity on this isn't great, and it might be hard to understand, but how about this:
arr = [1, 18, 5, 19, 25, 3]
# calculates length of the overall path
def calc_path_difference(arr, i1, i2, i3):
return abs(arr[i1] - arr[i2]) + abs(arr[i2] - arr[i3])
# returns dictionary with differences to other numbers in arr from each number
def differences_dict(arr):
return {
current: [
abs(number - current) if abs(number - current) != 0 else float("inf")
for number in arr
]
for current in arr
}
differences = differences_dict(arr)
# Just to give some starting point, take the first three elements of arr
current_path = [calc_path_difference(arr, 0, 1, 2), 0, 1, 2]
# Loop 1
for i, num in enumerate(arr):
# Save some time by skippin numbers who's path
# already exceeds the min path we currently have
if not min(differences[num]) < current_path[0]:
continue
# Loop 2
for j, num2 in enumerate(arr):
# So you can't get 2 of the same index
if j == i:
continue
# some code for making indices i and j of differences
# infinite so they can't be the smallest, but not sure if
# this is needed without more tests
# diff_arr_copy = differences[num2].copy()
# diff_arr_copy[i], diff_arr_copy[j] = float("inf"), float("inf")
# Get index of number in arr with smallest difference to num2
min_index = differences[num2].index(min(differences[num2]))
# So you can't get 2 of the same index again
if min_index == i or min_index == j:
continue
# Total of current path
path_total = calc_path_difference(arr, i, j, min_index)
# Change current path if this one is shorter
if path_total < current_path[0]:
current_path = [path_total, i, j, min_index]
Does this work for you? I played around with the order of the elements in the array and it seemed to give the correct output each time but I would have liked to have another example to test it on.

How to determine the complexity of python code?

I need to understand the complexity of the following code. I am familiar with the concepts of all the Big O() notation and also have read a lot of blogs but I cant figure how to apply to big programs.
Following is the code for largest pallindrome number from 100 to 999:
def isPaindrome(number):
stringNum = str(number)
firstDigit_index,lastDigit_index=0,len(stringNum)-1
isPalindrome = False
while(lastDigit_index > 0 and firstDigit_index < lastDigit_index):
#print(stringNum[f],"==",stringNum[l],"......")
if(stringNum[firstDigit_index]==stringNum[lastDigit_index]):
isPalindrome = True
else:
isPalindrome = False
break
firstDigit_index = firstDigit_index + 1
lastDigit_index = lastDigit_index - 1
if(isPalindrome):
return number
else:
return 0
max = 0
startRange = 100
endRange = 999
for i in range(endRange*endRange,startRange*startRange,-1):
factors = []
result = isPaindrome(i)
if(result!=0):
for i in range(startRange,endRange+1):
if(result%i==0):
factors.append(i)
if(len(factors)>1):
sumFactor = factors[(len(factors))-1] + factors[(len(factors))-2]
mul = factors[(len(factors))-1] * factors[(len(factors))-2]
if(sumFactor>max and mul==result):
max = sumFactor
print("Largest Palindrome made from product of two 3 digit numbers(",factors[(len(factors))-1],",",factors[(len(factors))-2] ,") is", result,".")
If anyone could just make me understant step by step how to calculate I'd be grateful.
As I mentioned, your isPalindrome function is literally incorrect, as you're not changing the indexes. I changed a bit of your also, so this is the version I'm analysing. (I'm only analysing the isPalindrome function, since I actually couldn't understand what the main function is doing), sorry!
def isPalindrome(n):
num = str(n)
head, tail = 0, len(num) - 1
while tail > head:
if num[head] != num[tail]: # Not symmetrical!! WARNING!
return False
head += 1 # move position
tail -= 1 # move position
return True
This code on average is O(|n|) i.e. O(log N) where N is the number. This is because on average, the if comparison has 50% chance of breaking (returning False) and 50% of continuing. Therefore the expected number of comparisons would be |n|/4 which is O(|n|).

Build a max heap and Iteration

I have two questions:
1) Recently I'm try to build a max heap. Even though I read CLRS I can't find the bug as I run it. The following is my code...
def maxHeapify(list, index):
int = index
left = (int+1) * 2 - 1
right = (int+1) * 2
largest = 0
if left < len(list):
if (left <= len(list)) & (list[left] >= list[int]):
largest = left
else:
largest = int
if right < len(list):
if (right <= len(list)) & (list[right] >= list[largest]):
largest = right
else:
pass
if largest != int:
listNew = swapWithinList(list, int, largest)
listNew = maxHeapify(listNew, largest)
else:
return listNew
def swapWithinList(list, id1, id2):
num1 = list[id1]
num2 = list[id2]
listNew = list[:id1]
listNew.append(num2)
listNew = listNew + list[(id1+1):id2]
listNew.append(num1)
listNew = listNew + list[(id2+1):]
return listNew
I give input but the console just says:
UnboundLocalError: local variable 'listNew' referenced before assignment
does it mean that I put the return statement on the wrong line or there's something I haven't mentioned?
2) What is a iteration?
I am a bit embarrassed when I ask the question. But what is a iteration? Wiki says each repetition of the process means it, so is it a result the loop gives each round?
And iterator seems a basic element in Python, what's difference between iterator and iteration?
1:
Without further comments, here's the code adapted from Wikipedia:
def max_heapify(A, i):
left = 2 * i + 1
right = 2 * i + 2
largest = i
if left < len(A) and A[left] > A[largest]:
largest = left
if right < len(A) and A[right] > A[largest]:
largest = right
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
def build_max_heap(A):
for i in range(len(A) // 2, -1, -1):
max_heapify(A, i)
Example:
def ptree(A, i=0, indent=0):
if i < len(A):
print ' ' * indent, A[i]
ptree(A, i * 2 + 1, indent + 1)
ptree(A, i * 2 + 2, indent + 1)
A = range(9)
build_max_heap(A)
ptree(A)
Result:
8
7
3
1
0
4
6
5
2
Let us know if you have any questions.
2:
Iteration in python is technically a process of repeatedly calling some object's next() method until it raises the StopIteration exception. Object that possesses this next method is called Iterator. An object that is able to provide an Iterator for the calling code is called Iterable.
The problem is here:
if largest != int:
listNew = swapWithinList(list, int, largest)
listNew = maxHeapify(listNew, largest)
else:
return listNew
If the if statement is false, you try to return listNew, but listNew might not exist at this point (it's only assigned to if the if statement is true).
An iteration is just a pass through a loop or in general, a single pass through some larger thing. Iterators are objects are iterate through data structures, or go through the elements one at a time.

Categories

Resources