My second for loop is not working in the below code - python-3.x

nums=[0,5,4,12]
n=len(nums)
temp=1
ans=[]
for i in range(n):
ans.append(temp)
temp*=nums[i]
temp=1
for i in range(n-1,-1):
ans[i]*=temp
temp*=nums[i]
print("yes")
print(ans)
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
This is a solution for this leetcode question but my second for loop is not executing, and i don't know why.

Using range like this will result in zero iterations because the "step" parameter is 1. So because it is 1 it will think it should go upwards, but n is already above -1, so it should be like
range(n, -1, -1)
also you most probably want
import math
n = [0, 5, 4, 12]
ans = []
for num in n:
temp = n[:] # create a copy
temp.remove(num) # remove the number you are on
ans.append(math.prod(temp)) # use math.prod to multiply the rest together
return ans

Related

Number of sub sequences of length K having total sum S, given 2d array

I wish to find Number of sub sequences of length K having total sum S, given an array.
Sample Input:
a=[1,1,1,2,2] & K=2 & S=2
Sample Output:
3 {because a[0],a[1]; a[1]a[2]; a[0]a[2] are only three possible for the case}
I have tried to write a recursive loop in Python for starter but it isn't giving output as expected.Please can you help me find a loophole I might be missing on.
def rec(k, sum1, arr, i=0):
#print('k: '+str(k)+' '+'sum1: '+str(sum1)) #(1) BaseCase:
if(sum1==0 and k!=0): # Both sum(sum1) required and
return 0 # numbers from which sum is required(k)
if(k==0 and sum1 !=0): # should be simultaneously zero
return 0 # Then required subsequences are 1
if(k==0 and sum1==0 ): #
return 1 #
base_check = sum1!=0 or k!=0 #(2) if iterator i reaches final element
if(i==len(arr) and base_check): # in array we should return 0 if both k
return 0 # and sum1 aren't zero
# func rec for getting sum1 from k elements
if(sum1<arr[0]): # takes either first element or rejects it
ans=rec(k-1,sum1,arr[i+1:len(arr)],i+1) # so 2 cases in else loop
print(ans) # i is taken in as iterator to provide array
else: # input to rec func from 2nd element of array
ans=rec(k-1, sum1-arr[0], arr[i+1:len(arr)],i+1)+rec(k, sum1, arr[i+1:len(arr)],i+1)
#print('i: '+str(i)+' ans: '+str(ans))
return(ans)
a=[1,1,1,2,2]
print(rec(2,2,a))
I am still unable to process how to make changes. Once this normal recursive code is written I might go to DP approach accordinlgy.
Using itertools.combinations
Function itertools.combinations returns all the subsequences of a given lengths. Then we filter to keep only subsequences who sum up to the desired value.
import itertools
def countsubsum(a, k, s):
return sum(1 for c in itertools.combinations(a,k) if sum(c)==s)
Fixing your code
Your code looks pretty good, but there are two things that appear wrong about it.
What is this if for?
At first I was a bit confused about if(sum1<arr[0]):. I think you can (and should) always go to the else branch. After thinking about it some more, I understand you are trying to get rid of one of the two recursive calls if arr[0] is too large to be taken, which is smart, but this makes the assumption that all elements in the array are nonnegative. If the array is allowed to contain negative numbers, then you can include a large a[0] in the subsequence, and hope for a negative element to compensate. So if the array can contain negative numbers, you should get rid of this if/else and always execute the two recursive calls from the else branch.
You are slicing wrong
You maintain a variable i to remember where to start in the array; but you also slice the array. Pretty soon your indices become wrong. You should use slices, or use an index i, but not both.
# WRONG
ans=rec(k-1, sum1-arr[0], arr[i+1:len(arr)],i+1)+rec(k, sum1, arr[i+1:len(arr)],i+1)
# CORRECT
ans = rec(k-1, sum1-arr[i], arr, i+1) + rec(k, sum1, arr, i+1)
# CORRECT
ans = rec(k-1, sum1-arr[0], arr[1:]) + rec(k, sum1, arr[1:])
To understand why using both slicing and an index gives wrong results, run the following code:
def iter_array_wrong(a, i=0):
if (a):
print(i, a)
iter_array_wrong(a[i:], i+1)
def iter_array_index(a, i=0):
if i < len(a):
print(i, a)
iter_array_index(a, i+1)
def iter_array_slice(a):
if a:
print(a)
iter_array_slice(a[1:])
print('WRONG')
iter_array_wrong(list(range(10)))
print()
print('INDEX')
iter_array_index(list(range(10)))
print()
print('SLICE')
iter_array_slice(list(range(10)))
Also note that a[i:len(a)] is exactly equivalent to a[i:] and a[0:j] is equivalent to a[:j].
Clean version of the recursion
Recursively count the subsequences who use the first element of the array, and the subsequences who don't use the first element of the array, and add the two counts. To avoid explicitly slicing the array repeatedly, which is an expensive operation, we keep a variable start to remember we are only working on subarray a[start:].
def countsubsum(a, k, s, start=0):
if k == 0:
return (1 if s == 0 else 0)
elif start == len(a):
return 0
else:
using_first_element = countsubsum(a, k-1, s-a[start], start+1)
notusing_first_elem = countsubsum(a, k, s, start+1)
return using_first_element + notusing_first_elem

Estimating value of 1/pi using Ramajunam equation, returning wrong value when comparing with (1/math.pi)

Edited for Updated code
#racraman. Love you man. You not only helped me improve my code but also to understand the Equation. Thanks for your time.
import math
# performing ramanujan's infinite series to
#generate a numerical approximation of 1/pi:
""" 1/pi = (2*sqrt(2))/9801) * (4*k)!*(1103+26390k)/(((k!)**4)*396**(4k)))"""
def factorial_1(k):
if k==0:
return 1
else:
result = k* factorial_1(k-1)
return result
def estimate_pi():
k=0
total=0
n=(2*math.sqrt(2)/9801)
limit=int(input("Enter the ending limit = ")) #k=0 until limit=infinity!!!!
while True:
m=factorial_1(4*k)*(1103+26390*k)
o=((factorial_1(k))**4)*(396**(4*k))
result=n*(m/o)
total+=result #assigning result to a new variable to keep track of changes
if k>limit:
break
k+=1 #updating value of k, to improve result & total for each loop.
return 1/total # Return's pi=3.14 only if k=0
print(estimate_pi())
The statement :
k = result
is the problem - the variable k cannot be both a loop counter and the running total.
Instead of that statement, you will need to simply decrement k, and also add result to a new running total variable that you initialise to 0 outside the loop.
You would also only want to print the result and return only after the loop has finished.
EDIT TO ADD :
Please don't use Answers in that way; that's not what they're for, and would be confusing for other readers to try to follow. The question is for containing all (ongoing) steps of defining the problem (just mark the appended updates with "EDIT TO ADD" as I have done with this comment); the answers are for solutions to that problem, to be accepted if they proved useful.
Ramanujan's formula most certainly works for increasing values of k - but you have to iterate starting at 0.
For example, let's say the user enters 5 for k.
What your code is currently doing is incrementing k - so calculating k = 5, 6, 7, ..... terminating when the step's result is 0. You're missing out k=0, 1, 2, 3, 4 - the major terms !
What you want to do instead is sum the results for k = 0, 1, 2, 3, 4, 5 so how about :
Have the user enter a variable limit instead of k
Start k at 0, and increment at each iteration
Terminate the loop when the step's result < epsilon, or k > limit
Incidentally, the n=(2*math.sqrt(2)/9801) is a constant, so can go outside the loop therefore get calculated only once.
#racraman. I'm Posting the updated code as an answer do that I could keep track of the Error's I've made for future references. Thanks for the Help.
# performing ramanujan's infinite series to
#generate a numerical approximation of 1/pi:
""" 1/pi = (2*sqrt(2))/9801) * (4*k)!*(1103+26390k)/(((k!)**4)*396**(4k)))"""
def factorial_1(k):
if k==0:
return 1
else:
result = k* factorial_1(k-1)
return result
def estimate_pi():
k=int(input("enter the value of k = "))
total=0
while True:
n=(2*math.sqrt(2)/9801)
m=factorial_1(4*k)*(1103+26390*k)
o=((factorial_1(k))**4)*(396**(4*k))
result=n*(m/o)
total+=result #assigning result to a new variable to keep track of changes
epsilon=1e-15
if abs(result)<epsilon:
break
k+=1 #updating value of k, to improve result & total for each loop.
return 1/total # Return's pi=3.14 only if k=0
print(estimate_pi())

Algorithm for calculating the duplicates in a list

The code below prints out the numbers which are duplicated in A.
From my understanding, the for loop goes through each element in the list and turns it into a negative number, though i can not figure out why it does not turn the numbers it prints (which are at at position 0,4,5) negative.
A = [1,2,3,1,3,6,6]
def printRepeating(arr, size):
print("The repeating elements are: ")
for i,x in enumerate(arr):
if arr[abs(arr[i])] >= 0:
arr[abs(arr[i])] = -arr[abs(arr[i])]
print(arr)
else:
print (abs(arr[i]), end = " ")
printRepeating(A,len(A))
The algorithm assumes:
all the elements of the array start as positive numbers, and
all the elements of the array are less than the length of the array.
In your example, since the length of the array is 7, all the elements in the array must be between 1 and 6.
What the algorithm does is change array[k] to negative to indicate that k has been seen. For example, since 1 is the first number seen, array[1] is changed to a negative number. The next time 1 is seen, array[1] is already negative, so 1 must be a duplicate.
If you just want to print the repeated values in the list then why not try this:
A = [1, 2, 3, 1, 3, 6, 6]
def get_repeated_elements(lst):
return list(set((i for i in lst if lst.count(i) > 1)))
print(get_repeated_elements(A))
This function converts the passed array into a generator of duplicated values
and then converts this into a set to filter out duplicates in the generator and then converts this into a list for returning to the caller. This is a far shorter function than the one given.
The algorithm assumes that all entries are strictly positive and smaller than the length of the list. Then, what it does is essentially using the sign of the i-th element to store if it already saw number i. In your example:
A=[1,2,3,1,3,6,6] Take 1
A[1] is positive, i.e. we have not seen it. Make it negative to mark it
A=[1,-2,3,1,3,6,6] Take -2 (stands for 2)
A[2] is positive, i.e. we have not seen it. Make it negative to mark it
A=[1,-2,-3,1,3,6,6] Take -3
A[3] is positive, i.e. we have not seen it. Make it negative to mark it
A=[1,-2,-3,-1,3,6,6] Take -1
A[1] is negative, i.e. we have already seen it. Report it.
A=[1,-2,-3,-1,3,6,6] Take 3
A[3] is negative, i.e. we have already seen it. Report it.
...
The below code can be used to find repeated elements in the list and also unique elements in the list.
from collections import Counter
A = [1,2,3,1,3,6,6]
B = Counter(A)
The below line prints repeated elements.
[k for k, v in B.items() if v > 1]
Output : [1, 3, 6]
The below line prints unique elements.
[k for k, v in B.items() if v == 1]
Output : [2]

When I call the function it shows nothing?

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
This is my script in python 3.5:
def plin1():
for i in range(1000,1):
for j in range(1000,1):
if str(i*j)==str(i*j)[::-1]:
break
return i*j
print(i,'*',j,'=',i*j)
But when I call this function, nothing is printed. What's wrong?
range(start, stop[, step]) -> range object
range() requires the first argument to be less than the second when step is positive, and step defaults to 1 when range() is called with 2 arguments. Hence the range object yields an empty sequence and the body of the for loop is never entered:
>>> list(range(1000, 1))
[]
However, by specifying a negative value for step, you can get the sequence that you require e.g.:
>>> list(range(10, 1))
[]
>>> list(range(10, 1, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]
So in your case you would do this:
for i in range(999, 99, -1):
for j in range(999, 99, -1):
# etc...
which would then iterate over all 3 digit numbers starting at the largest.
Also the use of return is incorrect and it will cause the function to terminate without printing anything. Here is a corrected version:
def plin1():
for i in range(999, 99, -1):
for j in range(999, 99, -1):
if str(i*j) == str(i*j)[::-1]:
return i*j
print(i,'*',j,'=',i*j)
Now the function returns the value only when it finds a palindrome. If there were no palindromes, the function would return None.
Notice, however, that this does not produce the largest palindrome that is the product of two 3 digit numbers. This function returns 580085 which is 995 * 583. But the largest one is 993 * 913 = 906609. You need to figure out which is the largest palindrome across all products, not just the first palindrome found. Here is a generator expression that will produce the required result:
max((i*j,i,j) for i in range(100, 1000) for j in range(100, 1000)
if str(i*j) == str(i*j)[::-1])
Which produces the tuple (906609, 993, 913).
Probably there are optimisations that could be made to reduce the number of calculations.
range with two arguments i, j generates the range from i to j. If i > j then there is no range to generate, and when iterating over that range nothing happens.
Range also takes a "step" parameter, allowing you to set the increment of the range. Perhaps you meant to use that. It would look like this:
for i in range(1, 1000, 1):
# loop body
Note that specifying a step of 1 is redundant, as 1 is the default value for that parameter.
If you want to generate a "do the right thing" range to generate the range from the lesser of (i, j) to the greater, you might write a function like the following:
def dwim_range(i,j):
return range(*sorted((i,j)))
Here you're sorting the tuple (i, j) and then unpacking it with the * operator so that range gets the two items of the tuple, in sorted order.

Carry numbers in list

I am making a code guessing program that asks for an input of an example key code, then the program tries combinations of characters and finds how long it took.
What I need to do is have a function that takes in a list of numbers, and returns a list of numbers, the hard thing being that the length could be anything. I currently have this:
def increment(nums):
nums[len(nums) - 1] += 1
for i in range(len(nums)):
if nums[-i + 1] == 62:
nums[-i + 1] = 0
nums[-i] += 1
if nums[0] == 62:
return [0 for n in nums]
return nums
I am trying to make the last index increase, when that reaches 62, increment the second to last by 1, then set the last to 0, continuing on and on. Then, when the first number reaches 62, it returns a list of zeros.
The expected output is as follows:
[0,0,0,1]
[0,0,0,2]
[0,0,0,3]
[0,0,0,4]
...
[0,0,0,61]
[0,0,1,0]
...
The current output is as follows:
[0,0,0,1]
[0,0,0,2]
[0,0,0,3]
[0,0,0,4]
...
[0,62,0,0]
At this point I am confused. Is there anything I am doing wrong?
Your indices in the carry loop are off, you're adding 1 and nothing, when you want to subtract 1 and 2:
def increment(nums):
nums[-1] += 1 # len(nums) - 1 is a slow verbose way to index at -1 in Python
for i in range(len(nums)):
if nums[-i - 1] == 62: # Change + 1 to - 1
nums[-i - 1] = 0 # Change + 1 to - 1
nums[-i - 2] += 1 # Change no adjustment to -2
if nums[0] == 62:
nums[:] = [0] * len(nums) # Always mutate argument to match return
return nums
This will still fail when you hit the wraparound case (due to an index out of bounds issue on the increment), and it involves more small math operations than needed, so we can improve it a bit and fix the bug by adjusting the range to run one fewer times, and remove two of the index fixups:
# Run one fewer times, iterate as negative numbers directly, and start
# from -1, not 0 so no adjustment needed for two of three lookups
for i in range(-1, -len(nums), -1):
if nums[i] == 62:
nums[i] = 0
nums[i - 1] += 1
If speed was important, you could get a bit more clever with enumerate so you're usually not even doing indexing, but this is close to what you already had, and premature optimization is the root of all evil. :-)
Note that if the goal is just to make an iterator that produces lists of this form sequentially, as I mention in the comments, the itertools module provides simpler ways to do this. For memory reasons, we can't use itertools.cycle (it would end up eventually storing the entire run of values, about 15 million of them for the four element case), but we can simulate it using chain:
from itertools import chain, product, repeat
carrygen = chain.from_iterable(product(range(62), repeat=4) for _ in repeat(None))
next(carrygen) # To skip all zero entry the first time around
carrygen = map(list, carrygen) # Returns lists instead of tuples
for nums in carrygen:
print(nums)
which would output indefinitely starting with until you break the loop or kill the process:
[0,0,0,1]
[0,0,0,2]
[0,0,0,3]
[0,0,0,4]
...
[0,0,0,61]
[0,0,1,0]
...
I don't think your list indices are working like you expect in your for loop, but rather than try to unravel that, I'd suggest that this would be a good use for a recursive function:
def increment(nums):
# Condition for ending the recursion when nums is empty
if not nums:
return []
# Check if we're overflowing the last item...
if nums[-1] < 61:
# ...if not, just increment it and return
nums[-1] += 1
return nums
else:
# ...if so, strip it off, increment the rest of the list, and
# tack on a 0 to the end
return increment(nums[:-1]) + [0]

Resources