I will be happy to get some help.
I have the following problem:
I'm given a list of numbers seq and a target number and I need to write 2 things:
A recursive solution that returns True if there is a sum of a subsequence that equals the target number and False otherwise.
example:
subset_sum([-1,1,5,4],0) # True
subset_sum([-1,1,5,4],-3) # False
Secondly, I need to write a solution using what I wrote in the previous solution
but now with memoization that uses a dictionary in which the keys are tuples:
(len(seq),target)
For number 1 this is what I got to so far:
def subset_sum(seq, target):
if target == 0:
return True
if seq[0] == target:
return True
if len(seq) > 1:
return subset_sum(seq[1:],target-seq[0]) or subset_sum(seq[1:],target)
return False
Not sure I got it right so if I could get some input I will be grateful.
For number 2:
def subset_sum_mem(seq, target, mem=None ):
if not mem:
mem = {}
key=(len(seq),target)
if key not in mem:
if target == 0 or seq[0]==target:
mem[key] = True
if len(seq)>1:
mem[key] = subset_sum_mem(seq[1:],target-seq[0],mem) or subset_sum_mem(seq[1:],target,mem)
mem[key] = False
return mem[key]
I can't get the memoization to give me the correct answer so I'd be glad for some guidance here.
Thanks for anyone willing to help!
Just for reference, here's a solution using dynamic programming:
def positive_negative_sums(seq):
P, N = 0, 0
for e in seq:
if e >= 0:
P += e
else:
N += e
return P, N
def subset_sum(seq, s=0):
P, N = positive_negative_sums(seq)
if not seq or s < N or s > P:
return False
n, m = len(seq), P - N + 1
table = [[False] * m for x in xrange(n)]
table[0][seq[0]] = True
for i in xrange(1, n):
for j in xrange(N, P+1):
table[i][j] = seq[i] == j or table[i-1][j] or table[i-1][j-seq[i]]
return table[n-1][s]
This is how I'd write the subset_sum:
def subset_sum(seq, target):
if target == 0:
return True
for i in range(len(seq)):
if subset_sum(seq[:i] + seq[i+1:], target - seq[i]):
return True
return False
It worked on a couple of examples:
>>> subset_sum([-1,1,5,4], 0))
True
>>> subset_sum([-1,1,5,4], 10)
True
>>> subset_sum([-1,1,5,4], 4)
True
>>> subset_sum([-1,1,5,4], -3)
False
>>> subset_sum([-1,1,5,4], -4)
False
To be honest I wouldn't know how to memoize it.
Old Edit: I removed the solution with any() because after some tests I found out that to be slower!
Update: Just out of curiosity you could also use itertools.combinations:
from itertools import combinations
def com_subset_sum(seq, target):
if target == 0 or target in seq:
return True
for r in range(2, len(seq)):
for subset in combinations(seq, r):
if sum(subset) == target:
return True
return False
This can do better that the dynamic programming approach in some cases but in others it will hang (it's anyway better then the recursive approach).
I have this modified code:
def subset_sum(seq, target):
left, right = seq[0], seq[1:]
return target in (0, left) or \
(bool(right) and (subset_sum(right, target - left) or subset_sum(right, target)))
def subset_sum_mem(seq, target, mem=None):
mem = mem or {}
key = (len(seq), target)
if key not in mem:
left, right = seq[0], seq[1:]
mem[key] = target in (0, left) or \
(bool(right) and (subset_sum_mem(right, target - left, mem) or subset_sum_mem(right, target, mem)))
return mem[key]
Can you provide some test cases this does not work for?
Related
I would like to change a function from iteration to recursion.
Here is the function:
def differences(values):
result = []
for i in range(len(values) - 1):
result.append(values [i+1] - values[i])
return result
def palindrome(word):
if len(word) <=1:
return True
if word[0] != word[-1]:
return False
return palindrome(word[1:-1])
In pseudocode:
differences list =
if list has < 2 elements result is empty list
otherwise,
list = first, second, *rest
result is [second - first] + differences([second, *rest])
Implementing this in python, you can elegantly use the new match statement.
Iteration:
palindrome str =
first = 0, last = len(str) - 1
while first < last
if str[first] != str[last] return false
first++, last--
return true
The first function could be made recursive as follows:
def differences2(values, i = 1, res = []):
if i < len(values):
res.append(values[i] - values[i-1])
return differences2(values, i + 1, res)
else:
return res
The second could be made iterative like this:
def palindrome2(word):
pointer = 0
while 2 * pointer + 1 <= len(word):
if word[pointer] != word[-pointer-1]:
return False
pointer += 1
return True
The recursive implementation of this can be very compact:
def palindrome(word, i = 0):
if 2 * i + 1 <= len(word):
return False if word[i] != word[-i-1] else palindrome(word, i = i + 1)
return True
As a side note: in general, while very elegant, I am always a little bit worried in implementing recursive solutions in Python - iterative one are more readable and robust (again, those are my 2 cents).
I am just getting started in Python Programming. I had a problem on checking if a given list contains alternating sequence of primes and perfect squares. The list can start with either a prime or a perfect square. I came up with a solution but it's not efficient as it generates unwanted lists. Is this possible with more efficient Python code?
First I'm creating functions to generate a list of primes as well as perfect squares up to the max value of testing list. Functions squaretest() and primecheck():
def squaretest(num):
sqlist=[]
i=1
while i**2 <= num:
sqlist.append(i**2)
i+=1
return sqlist
def primecheck(num):
primelist=[]
for i in range(2,num + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
return primelist
Then I am dividing the given list into lists of even index and odd index elements and checking all elements of them against the primelist and the squarelist:
def primesquare(l):
if len(l)==1:
primelist = primecheck(l[0])
sqlist = squaretest(l[0])
return (l[0] in primelist) or (l[0] in sqlist)
else:
ol=[]
el=[]
for i in range(0,len(l),2):
ol.append(l[i])
for p in range (1, len(l),2):
el.append(l[p])
primelist = primecheck(max(l))
sqlist = squaretest (max(l))
return((all(x in primelist for x in el)) == True and (all(y in sqlist for y in ol)) == True) or ((all(x in primelist for x in ol)) == True and (all(y in sqlist for y in el)) == True)
It works.
Any suggestions will be really helpful.
You can use sets to check if all members of a list are in another list.
def primesquare(l):
if len(l) == 0:
return True
primelist = set(primecheck(max(l)))
sqlist = set(squaretest(max(l)))
ol = set(l[::2])
el = set(l[1::2])
odds_are_primes = ol.issubset(primelist)
odds_are_squares = ol.issubset(sqlist)
evens_are_primes = el.issubset(primelist)
evens_are_squares = el.issubset(sqlist)
return (odds_are_primes and evens_are_squares) or (odds_are_squares and evens_are_primes)
I came up with a solution but it's not efficient as it generates
unwanted lists.
Assuming the unwanted lists are the two lists representing the even and odd elements, then we can fix that. (Eliminating the primes and squares list is a whole 'nother problem.) Below is my rework of your code -- we don't create addtional lists but rather with a couple of reusable ranges which are objects that produce integer sequences as needed, but not stored in memory.
Your any() design is efficient in that the arguments are generator expressions, not lists, which are computed as needed. As soon as a flaw is found in the array, the whole thing stops and returns False--it doesn't need to process the rest:
def squares(number):
return {x * x for x in range(int(number ** 0.5) + 1)}
def primes(number):
prime_set = set()
for i in range(2, number + 1):
for p in range(2, int(i ** 0.5) + 1):
if (i % p) == 0:
break
else: # no break
prime_set.add(i)
return prime_set
def primesquare(array):
if not array:
return True # define as the problem demands
length, maximum = len(array), max(array)
odd, even = range(0, length, 2), range(1, length, 2)
prime_set, square_set = primes(maximum), squares(maximum)
return all(array[i] in prime_set for i in even) and all(array[i] in square_set for i in odd) or all(array[i] in prime_set for i in odd) and all(array[i] in square_set for i in even)
I admire #AndreySemakin's set-based solution (+1), and use sets above, but his solution generates the same lists you want to eliminate (just in the form of sets).
I came up with this solution:
def primesquare(lst):
# checking if the first element is either perfect square or a prime
if not lst or (not checksquare(lst[0]) and not checkprime(lst[0])):
return False
length = len(lst)
if length == 1:
return True
if checksquare(lst[0]):
# if first element is square then make s(quare)=2 and p(rime)=1
s, p = 2, 1
else:
# if first element is prime then make s=1 and p=2
s, p = 1, 2
# running perfect square loop from s to len-1 with gap of 2 and checking condition
for i in range(s, length, 2):
if not checksquare(lst[i]):
return False
# running prime loop from p to len-1 with gap of 2
for i in range(p, length, 2):
if not checkprime(lst[i]):
return False
return True
def checksquare(n): # function to check perfect square
if n < 0:
return False
if 0 <= n <= 1:
return True
for i in range(int(n ** 0.5) + 1):
if i * i == n:
return True
return False
def checkprime(n): # function to check prime
if n < 2:
return False
if n % 2 == 0:
return n == 2
for i in range(3, int(n ** 0.5) + 1, 2):
if n % i == 0:
return False
return True
I am currently working on this code and the only thing that seems to work is the "no solution." Also it seems that the code has an infinite loop and I can't seem to figure out how to solve it. If someone could point out my mistake it would be appreciated.
def greedySum(L, s):
""" input: s, positive integer, what the sum should add up to
L, list of unique positive integers sorted in descending order
Use the greedy approach where you find the largest multiplier for
the largest value in L then for the second largest, and so on to
solve the equation s = L[0]*m_0 + L[1]*m_1 + ... + L[n-1]*m_(n-1)
return: the sum of the multipliers or "no solution" if greedy approach does
not yield a set of multipliers such that the equation sums to 's'
"""
if len(L) == 0:
return "no solution"
sum_total = (0, ())
elif L[0] > k:
sum_total = greed(L[1:], k)
else:
no_number = L[0]
value_included, take = greed(L, k - L[0])
value_included += 1
no_value, no_take = greed(L[1:], k)
if k >= 0:
sum_total = (value_included, take + (no_number,))
else:
sum_total = (value_included, take + (no_number,))
return sum_total
sum_multiplier = greed(L, s)
return "no solution" if sum(sum_multiplier[1]) != s else sum_multiplier[0]
Second method:
def greedySum(L, s):
answer = []
try:
while (s >= L[0]):
total = s// L[0]
s -= (total * L[0])
answer.append(total)
L = L[1:]
return(str(answer)[1:-1])
except:
return("no solution")
Here is something that works:
def greedySum(L, s):
multiplier_sum = 0
for l in L:
(quot,rem) = divmod(s,l) # see how many 'l's you can fit in 's'
multiplier_sum += quot # add that number to the multiplier_sum
s = rem # update the remaining amount
# If at the end and s is 0, return the multiplier_sum
# Otherwise, signal that there is no solution
return multiplier_sum if s == 0 else "no solution"
I would offer more help on what is wrong with your code, but that is for the moment a moving target - you keep changing it!
>>> greedySum([4,2],8)
2
>>> greedySum([4,2],9)
'no solution'
>>> greedySum([4,2,1],9)
3
I'm trying to make a slightly more advanced palindrome as according to the docstring. However I can't get it to work. Am I going it about the right way? This is what I have so far:
def pal_length(s: str, n: int) -> bool:
'''Return True iff s has a palindrome of length exactly n.
>>> pal_length('abclevel', 5)
True
>>> pal_length('level', 2)
False
'''
if not s:
return True
else:
index = 0
while index < len(s):
if s[index] == s[index+n]:
return pal_length(s[index+1:index+n-1],n-1)
index += 1
return False
I'm trying to not use any import modules etc. Just straight recursion.
Any help is appreciated. Thanks.
I think your indexing is a bit off. Shouldn't it be
index = 0
while index < len(s) - n + 1:
if s[index] == s[index+n-1]:
return pal_length(s[index+1:index+n-1], n-2)
index += 1
return False
I'm trying to do a problem in my book but I have no idea how. The question is, Write function geometric() that takes a list of integers as input and returns True if the integers in the list form a geometric sequence. A sequence a0,a1,a2,a3,a4,...,an-2,an-1 is a geometric sequence if the ratios a1/a0,a2/a1,a3/a2,a4/a3,...,an-1/an-2 are all equal.
def geometric(l):
for i in l:
if i*1==i*0:
return True
else:
return False
I honestly have no idea how to start this and I'm completely drawing a blank. Any help would be appreciated.
Thanks!
For example:
geometric([2,4,8,16,32,64,128,256])
>>> True
geometric([2,4,6,8])`
>>> False
This should efficiently handle all iterable objects.
from itertools import izip, islice, tee
def geometric(obj):
obj1, obj2 = tee(obj)
it1, it2 = tee(float(x) / y for x, y in izip(obj1, islice(obj2, 1, None)))
return all(x == y for x, y in izip(it1, islice(it2, 1, None)))
assert geometric([2,4,8,16,32,64,128,256])
assert not geometric([2,4,6,8])
Check out itertools - http://docs.python.org/2/library/itertools.html
One easy method would be like this:
def is_geometric(a):
r = a[1]/float(a[0])
return all(a[i]/float(a[i-1]) == r for i in xrange(2,len(a)))
Basically, it calculates the ratio between the first two, and uses all to determine if all members of the generator are true. Each member of the generator is a boolean value representing whether the ratio between two numbers is equal to the ratio between the first two numbers.
Here's my solution. It's essentially the same as pyrospade's itertools code, but with the generators disassembled. As a bonus, I can stick to purely integer math by avoid doing any division (which might, in theory, lead to floating point rounding issues):
def geometric(iterable):
it = iter(iterable)
try:
a = next(it)
b = next(it)
if a == 0 or b == 0:
return False
c = next(it)
while True:
if a*c != b*b: # <=> a/b != b/c, but uses only int values
return False
a, b, c = b, c, next(it)
except StopIteration:
return True
Some test results:
>>> geometric([2,4,8,16,32])
True
>>> geometric([2,4,6,8,10])
False
>>> geometric([3,6,12,24])
True
>>> geometric(range(1, 1000000000)) # only iterates up to 3 before exiting
False
>>> geometric(1000**n for n in range(1000)) # very big numbers are supported
True
>>> geometric([0,0,0]) # this one will probably break every other code
False
Like this
def is_geometric(l):
if len(l) <= 1: # Edge case for small lists
return True
ratio = l[1]/float(l[0]) # Calculate ratio
for i in range(1, len(l)): # Check that all remaining have the same ratio
if l[i]/float(l[i-1]) != ratio: # Return False if not
return False
return True # Return True if all did
And for the more adventurous
def is_geometric(l):
if len(l) <= 1:
return True
r = l[1]/float(l[0])
# Check if all the following ratios for each
# element divided the previous are equal to r
# Note that i is 0 to n-1 while e is l[1] to l[n-1]
return all(True if e/float(l[i]) == r else False for (i, e) in enumerate(l[1:]))