My assignment is as follows- Write a function all_sums which takes a non-negative integer n as its input and returns a list L with the following properties: The length of L is n 1. For each integer k from 0 up to n, the value L[k] is a list which contains precisely those pairs [a, b] of non-negative integers which satisfy a b == k. (The order of the elements of L[k] does not matter, but these lists should not contain any duplicates, nor any elements other than those indicated before.)
My current code is as follows, however why does my error message say that "too many values (expected 1)?" Do I have too many lists or something?
def all_sums(n):
a = 0
b = 0
for k in range(0, n):
if a b == k:
L = [a, b]
return all_sums
CodePudding user response:
The first step in solving this problem is to figure out how to generate all the a and b values for a given k. It doesn't seem like your code has made any steps in this direction -- the reason you're getting a NameError on a >= 0 is that no value has been defined for a yet but you're trying to do a comparison on it.
One way to think about generating all the a b = k solutions for a given k is to think iteratively -- the possible values for a range from 0 to k, and algebraically we know that b = k - a, so (a, b) is really just (a, k - a). Hence for k = 4 we could do:
>>> k = 4
>>> [(a, k - a) for a in range(k 1)]
[(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)]
Now we just need to iterate over k in range(n 1) to return L:
>>> def all_sums(n):
... return [[(a, k - a) for a in range(k 1)] for k in range(n 1)]
...
>>> all_sums(3)
[[(0, 0)], [(0, 1), (1, 0)], [(0, 2), (1, 1), (2, 0)], [(0, 3), (1, 2), (2, 1), (3, 0)]]
CodePudding user response:
To produce a working solution, you need to thing logically. When you bake a cake you first need to grab the flour, the milk and the eggs before you start. To create your software, you need to grab other things:
- A function which is given a value k, and returns a list of pairs of non-negative integers which sum to k
- A function which uses the first function for values of k from 0 to n.
You then create the software by combining these ingredients.
A list comprehension over variables a and b does the first item. Please note that you can do list comprehensions on two variables at the same time.
def find_pairs(k):
return [(a,b) for a in range(k 1) for b in range(k 1) if (a b)==k]
# test it
find_pairs(3) # => [(0, 3), (1, 2), (2, 1), (3, 0)]
Now, complete for all values of n.
This, of course, assumes that the class has done list comprehensions. If you haven't then you may be best off using two explicit loops. Otherwise, the lecturer may give you some very funny looks, and ask a lot of searching questions (this is one of the drawbacks of copying stuff for homework from Stack Overflow).
def find_pairs(k):
lst=[]
for a in range(k 1):
for b in range(k 1):
if (a b)==k:
lst.append((a,b))
return lst
# test it
find_pairs(3) # => [(0, 3), (1, 2), (2, 1), (3, 0)]
It should be clear why most Python programmers would use the list comprehension.
Please ask enough questions until you are sure that you understand how the code works. Please also ensure that you understand the importance of testing the software so as to find all of the problems with it.
CodePudding user response:
A little bit verbose, but the logic should be clear
def permutations(n) :
res = []
i, lower_threshold = n, n / 2
while i >= lower_threshold:
res.append([i, n - i])
i -= 1
return res
def permutations_up_to(n):
return [permutations(x) for x in range(0, n 1)]
# This is just to output the result of permutation_up_to nicely
for perm in permutations_up_to(5):
print(perm)
OUTPUT
[[0, 0]]
[[1, 0]]
[[2, 0], [1, 1]]
[[3, 0], [2, 1]]
[[4, 0], [3, 1], [2, 2]]
[[5, 0], [4, 1], [3, 2]]
In order to avoid duplicate pairs, you need to stop the iteration half way - let's put this way - and you don't want to proceed beyond that.
FOLLOW UP
Looking at the comments to this post, I noticed there can be disagreement if permutations of the same set of numbers (i.e. (2, 0) and (0, 2)) should be both included or not (the latter being my interpretation earlier). In any case, a simpler solution which could leave all the options open is:
def permutations(n, all_included = True):
func = (lambda x, y: True) if all_included else (lambda x, y: x >= y)
return [(x, y) for x, y in zip(range(n, -1, -1), range(0, n 1)) if func(x, y)]
def permutations_up_to(n, all_included = True):
return [permutations(x, all_included) for x in range(0, n 1)]
So, if we need to include only 1 set summing up to k:
for perm in permutations_up_to(5, False):
print(perm)
OUTPUT
[(0, 0)]
[(1, 0)]
[(2, 0), (1, 1)]
[(3, 0), (2, 1)]
[(4, 0), (3, 1), (2, 2)]
[(5, 0), (4, 1), (3, 2)]
Otherwise:
for perm in permutations_up_to(5):
print(perm)
OUTPUT
[(0, 0)]
[(1, 0), (0, 1)]
[(2, 0), (1, 1), (0, 2)]
[(3, 0), (2, 1), (1, 2), (0, 3)]
[(4, 0), (3, 1), (2, 2), (1, 3), (0, 4)]
[(5, 0), (4, 1), (3, 2), (2, 3), (1, 4), (0, 5)]
