Have to display coins used in makeChange function? - python

I am creating a makeChange function using a useIt or loseIt recursive call. I was able to determine the least amount of coins that's needed to create the amount; however, I am unsure as to how to display the actual coins used.
def change(amount,coins):
'''takes the amount and makes it out of the given coins in the list in order to return the least number of coins that's needed to create the value and the coins used'''
if amount==0:
return 0
if coins== [] or amount < 0:
return float('inf')
else:
useIt= change(amount,coins[1:])
loseIt= change(amount-coins[0],coins) + 1
return min(useIt,loseIt)

First of all the semantics of your names for the use-it-or-lose-it strategy are backwards. "Use it" means the element in question is being factored into your total result. "Lose it" means you're simply recursing on the rest of the list without considering the current element. So I'd start by switching those names around. And also you should be discarding the current element in the recursive call, whether or not you use it:
loseIt = change(amount,coins[1:])
useIt = change(amount-coins[0], coins[1:]) + 1
Now that we've fixed that, on to your question about passing the result down. You can take the min of tuples or lists, and it will start by comparing the first elements of each, and continue on successive elements if no definite min is yet found. For example if you pass around a list with an integer and a list:
>>> min([2, [1,2]], [3, [0,2]])
[2, [1, 2]]
You can also combine any number of elements in this form with an expression like this:
>>> [x+y for x,y in zip([2, [1,2]], [3, [0,2]])]
[5, [1, 2, 0, 2]]
but your case is easier since you always are just combining 2 of these, so it can be written more like:
[useIt[0] + 1, useIt[1] + [coins[0]]]
Note that the first element is an integer and the second a list.
in your case the implementation of this idea would look something like:
def change(amount,coins):
if amount==0:
return [0, []]
if coins== [] or amount < 0:
return [float('inf'), []]
else:
loseIt = change(amount, coins[1:])
useIt = change(amount - coins[0], coins[1:])
# Here we add the current stuff indicating that we "use" it
useIt = [useIt[0] + 1, useIt[1] + [coins[0]]]
return min(useIt,loseIt)

You want something like:
def change(amount, coins):
'''
>>> change(10, [1, 5, 25])
[5, 5]
'''
if not coins or amount <= 0:
return []
else:
return min((change(amount - coin, coins) + [coin]
for coin in coins if amount >= coin), key=len)
As an aside, this is not a dynamic programming solution. This is a simple recursive algorithm which will not scale for very large inputs.

Related

How do I solve a two-sum problem with multiple solutions?

So essentially it is a simple two sum problem but there are multiple solutions. At the end I would like to return all pairs that sum up to the target within a given list and then tally the total number of pairs at the end and return that as well. Currently can only seem to return 1 pair of numbers.
So far my solution has to been to try and implement a function that counts the amount of additions done, and while that number is less than the total length of the list the code would continue to iterate. This did not prove effective as it would still not take into account other solutions. Any help would be greatly appreciated
I took your code and did a couple of tweaks to where summations were being tested and how the data was being stored. Following is your tweaked code.
def suminlist(mylist,target):
sumlist = []
count = 0
for i in range(len(mylist)):
for x in range(i+1,len(mylist)):
sum = mylist[i] + mylist[x]
if sum == target:
count += 1
worklist = []
worklist.append(mylist[i])
worklist.append(mylist[x])
sumlist.append(worklist)
return count, sumlist
list = [0, 5, 4, -6, 2, 7, 13, 3, 1]
print(suminlist(list,4))
Things to point out.
The sumlist variable is defined as a list with no initial values.
When a summation of two values in the passed list equate to the test value, they are placed into a new interim list and then that list is appended to the sumlist list along with incrementing the count value.
Once all list combinations are identified, the count value and sumlist are returned to the calling statement.
Following was the test output at the terminal for your list.
#Dev:~/Python_Programs/SumList$ python3 SumList.py
(2, [[0, 4], [3, 1]])
To split the count value out from the list, you might consider splitting the returned data as noted in the following reference Returning Multiple Values.
Give that a try to see if it meets the spirit of your project.
You can use the itertools module for this job.
my_list = [1, 2, 3, 4]
target = 3
out = [x for x in itertools.combinations(my_list, r=2) if sum(x) == target]
print(out)
>>> [(0, 3), (1, 2)]
If you feel like using a python standard library import is cheating, the the official documentation linked above showcases example code for a "low level" python implementation.
Issue:
The issue for returning one set of possible several sets remains in the first return line (return sumlist). Based on the code, the function will automatically ends the function as the first set of value that their sum is the same as the target value. Therefore, we need to adjust it.
Adjustment:
I add a list(finallist[]) at the begining of the function for collecting all the applicable sets that can sum up to the target value. Then, I add a list(list[]) right after the if statement (*since I create an empty list(list[]) right after the if statement, when any sum of two values fulfills the target value, the function will empty the list again to store the new set of two values and append to the finallist[] again). Hence, as long as a set of two numbers can sum up to the target value, we can append them to the list(list[]). Accordingly, I add two more lines of code to append two values into the list(list[]). At the end, I append this list(list[]) to finallist[]. Also, I move the return statement to the final line and adjust the spacing. After this adjustment, the function will not end right after discovering the first possible set of values. Instead, the function will iterate repeatedly until getting all sets of the values and storing in the finalist[].
Originally, the function puts the return statement (return -1) at the end of the function for the situation that none of the sets can sum up to the target value. However, after the previous adjustment, the original return statement (return -1) will not have the opportunity to function as everything will end in the previous return line (return finallist). Therefore, I change it to the else part in the if statement (*meaning: when none of the sum of two values adds up to the target value, we will return 'No two values in the list can add up to the target value.')
Changes in Function:
def suminlist(mylist,target):
# count = 0 # delete
# while count < len(mylist): # delete
finallist=[] # add
for i in range(len(mylist)):
for x in range(i+1,len(mylist)):
sum = mylist[i]+mylist[x]
# count = count + 1 # delete
if sum == target:
# sumlist = mylist[i],mylist[x] # delete
# return sumlist # delete
list=[] # add
list.append(mylist[i]) # add
list.append(mylist[x]) # add
finallist.append(list) # add
else: # add
return 'No two values in the list can add up to the target value.' # add
return finallist # add
# return -1 # delete
Final Version:
def suminlist(mylist,target):
finallist=[]
for i in range(len(mylist)):
for x in range(i+1,len(mylist)):
sum = mylist[i]+mylist[x]
if sum == target:
list=[]
list.append(mylist[i])
list.append(mylist[x])
finallist.append(list)
else:
return 'No two values in the list can add up to the target value.'
return finallist
Test Code and Output:
list = [0, 5, 4, -6, 2, 7, 13, 3, 1]
print(suminlist(list,100))
# Output: No two values in the list can add up to the target value.
print(suminlist(list,4))
# Output: [[0, 4], [3, 1]]

How to append to a list two numbers from within the list that add up to a number in the list?

First, I want to find the highest number in the list which is the second number in the list, then split it in two parts. The first part contains the 2nd highest number, while the second part contains the number from the list that sums to the highest number. Then, return the list
eg: input: [4,9,6,3,2], expected output:[4,6,3,6,3,2] 6+3 sums to 9 which is the highest number in the list
Please code it without itertools.
python
def length(s):
val=max(s)
s.remove(val)
for j in s:
if j + j == val:
s.append(j)
s.append(j)
return s
Here's what I have but it doesn't return what the description states.
Any help would be appreciated as I spent DAYS on this.
Thanks,
The main issue in your code seems to be that you are editing the list s whilst iterating through it, which can cause issues with the compiler and is generally just something you want to avoid doing in programming. A solution to this could be iterating through a copy of the original list.
The second problem is that your program doesn't actually find the second biggest value in the list, just a value which doubles to give you the biggest value.
The final problem (which I unfortunately only noticed after uploading what I thought was a solution) is that the split values are appended to the end of the list rather than to the position where originally the largest value was.
Hopefully this helps:
def length(array):
val = max(array)
idx = array.index(val) # gets the position of the highest value in the array (val)
array.remove(val)
for i in array.copy(): # creates a copy of the original list which we can iterate through without causing buggy behaviour
if max(array) + i == val:
array = array[:idx] + [max(array), i] + array[idx:]
# Redefines the list by placing inside of it: all values in the list upto the previous highest values, the 2 values we got from splitting the highest value, and all values which previously went after the highest value.
return array
This will return None if there is no value which can be added to the second highest value to get the highest value in the given array.
Input:
print(length([1,2,3,4,5]))
print(length([4,8,4,3,2]))
print(length([11,17,3,2,20]))
print(length([11,17,3,2,21]))
Output:
[1, 2, 3, 4, 4, 1]
[4, 4, 4, 4, 3, 2]
[11, 17, 3, 2, 17, 3]
None
Here are the docs on list slicing (which are impossible to understand) and a handy tutorial.
when you say "The first part contains the 2nd highest number" does that mean second highest number from the list or the larger of the two numbers that add up the largest number from list?
Here I assume you just wanted the larger of the two numbers that add up to the largest number to come first.
def length(s:list):
#start by finding the largest value and it's position in the list:
largest_pos = 0
for i in range(len(s)):
if s[i] > s[largest_pos]:
largest_pos = i
# find two numbers that add up to the largest number in the s
for trail in range(len(s)):
for lead in range(trail, len(s)):
if (s[trail] + s[lead]) == s[largest_pos]:
if s[trail] > s[lead]:
s[largest_pos] = s[trail]
s.insert(largest_pos +1, s[lead])
else:
s[largest_pos] = s[lead]
s.insert(largest_pos + 1, s[trail])
return s
# if no two numbers add up to the largest number. return s
return s
Since you are limited to 2 numbers, a simple nested loop works.
def length(s):
val = max(s)
idx = s.index(val)
s.remove(val)
for i in range(len(s) - 1):
for j in range(i + 1, len(s)):
if s[i] + s[j] == val:
s = s[:idx] + [s[i], s[j]] + s[idx:]
return s
print(length([4,9,6,3,2]))
Output:
[4, 6, 3, 6, 3, 2]
I used deque library
first to find the highest element or elements then remove all of them and replace them with second high value and rest like : 9 replace with 6 and 3 in example:
from collections import deque
l = [4, 9, 6, 3, 2]
a = deque(l)
e = a.copy()
s = max(a)
while s in a:
a.remove(s) # remove all highest elements
s2 = max(a) # find second high value
c = s - s2
for i in l:
if i == s:
w = e.index(i) # find index of high values
e.remove(max(e))
e.insert(w, s2)
e.insert(w+1, c)
print(list(e))

How can I write a code to obtain the lowest values of each list within a list in python?

I need help writing a code that will help me obtain the lowest number of the each list within a list in python. And then out of the lowest values obtained I must somehow find the highest number of the lowest numbers. I am not allowed to call the built-in functions min or max, or use any other functions from pre-written modules. How can I go about doing this? I have already tried using the following code:
for list in ells:
sort.list(ells)
Since you are not allowed to use built-in functions, you can instead use a variable to keep track of the lowest number you find so far while you iterate through the sublists, and another to keep track of the highest of the lowest numbers you find so far you iterate through the list of lists:
l = [
[2, 5, 7],
[1, 3, 8],
[4, 6, 9]
]
highest_of_lowest = None
for sublist in l:
lowest = None
for item in sublist:
if lowest is None or lowest > item:
lowest = item
if highest_of_lowest is None or highest_of_lowest < lowest:
highest_of_lowest = lowest
print(highest_of_lowest)
This outputs: 4
You can iterate through your lists and compare to a variable, here using lo, if the item is less than the current amount then assign that as the new lo. After repeat the process but with hi and opposite logic.
lst = [[1, 2, 3], [4, 5, 6,], [7, 8, 9]]
lows = []
for i in lst:
lo = None
for j in i:
if lo == None: # to get initial lo
lo = j
elif j < lo:
lo = j
lows.append(lo)
hi = 0
for i in lows:
if i > hi:
hi = i
print(hi)
a=[6,4,5]
b=[8,3,9]
listab=[a,b]
sorted([sorted(x)[0] for x in listab])[-1]
Output > 4
You can do this for an arbitrary number of lists. This also avoids having to write your own min and max functions.
In my opinion the cleanest solution would be to write:
max([min(inner_list) for inner_list in list_of_lists])
But you said that you were not allowed to use built-in max and min. Well... Why don't we just implement them ourselfs? Here you go:
def min(iterable):
result = iterable[0]
for element in iterable:
if element < result:
result = element
return result
def max(iterable):
result = iterable[0]
for element in iterable:
if element > result:
result = element
return result
Now, that might not be the most robust min and max in the world (and they could sure use more code reuse), but they are short, clear, and will do just fine. Also this would keep the main line of code clear of loops, which make it more difficult to read your code.
And, as an added bonus (which you probably won't need here, but it is a good practice in software design)---your code would have some semblance of SRP, requiring you to only replace the max and min without ever touching the actual logic of your program.
Here is the full snippet.

Generating Padovan Sequence

The Padovan sequence is governed by the relationship P(n+1) = P(n-1) + P(n-2), for n is a
non-negative integer, where P(0) = P(1) = P(2) = 1. So, for instance, P(3) = 2, P(4) = 2, and
P (5) = 3, and so on.
I want to write a Python program Pad(n) that generates the sequence P(0), P(1), ..., P(n - 1).
This is what I have this far, but it only produces a list with the ith number replicated up to the largest number in the sequence:
def ith(n):
first, sec, third, fourth = 1, 1, 1, 1
for i in range(3, n+1):
fourth = first + sec
first = sec
sec = third
third = fourth
return fourth
def pad(n):
pad = ith(n)
lst = []
for i in range(n):
lst.append(pad)
return lst
I want it to produce this as an output:
>>> Pad(6)
>>>[1,1,1,2,2,3]
Currently my code only produces:
>>>[4,4,4,4,4,4]
I now that I append the ith value ith number of times to the list, but I dont know how to append each number in series up to and including the value for the last number. Pad(6) yields 4 since this is all the previous relationships put together.
Sorry for my bad description and formulating of the problem.
You have a two minor errors in your pad() function.
First you should be calling the ith() function inside the loop (also don't name the variable pad as that's the name of the function and it can cause problems).
Secondly, you are calling ith(n) inside the loop when you should be calling ith(i). This is the reason that you were always getting the same number- the argument to ith() was not changing inside the loop.
A fixed version of your pad() function would be:
def pad(n):
lst = []
for i in range(n):
val = ith(i)
lst.append(val)
return lst
You can verify that this indeed produces the correct output of [1, 1, 1, 2, 2, 3] for pad(6).
More efficient non-recursive method
Now, while your method works, it's also very inefficient. You are calling the ith() function for every value in range(n) which recomputes the entire sequence from the beginning each time.
A better way would be to store the intermediate results in a list, rather than by calling a function to get the ith() value.
Here is an example of a better way:
def pad2(n):
lst = [1, 1, 1] # initialize the list to the first three values in the sequence
for i in range(3,n):
lst.append(lst[i-2] + lst[i-3]) # use already computed values!
# slice the list to only return first n values (to handle case of n <= 3)
return lst[:n]
Recursive method
As seen on Wikipedia, the recurrence relation shows us that pad(n) = pad(n-2) + pad(n-3).
Use this as the starting point for a recursive function: return pad(n-2) + pad(n-3)
This is almost everything you need, except we have to define the starting values for the sequence. So just return 1 if n < 3, otherwise use the recurrence relation:
def pad_recursive(n):
if n < 3:
return 1
else:
return pad_recursive(n-2) + pad_recursive(n-3)
Then you can get the first n values in the sequence via list comprehension:
print([pad_recursive(n) for n in range(6)])
#[1, 1, 1, 2, 2, 3]
But this suffers from the same drawback as your original function as it computes the entire sequence from scratch in each iteration.

Traversing a Python list and making in-place changes

My task is to remove all instances of one particular element ('6' in this example) and move those to the end of the list. The requirement is to traverse a list making in-line changes (creating no supplemental lists).
Input example: [6,4,6,2,3,6,9,6,1,6,5]
Output example: [4,2,3,9,1,5,6,6,6,6,6]
So far, I have been able to do this only by making supplemental lists (breaking the task's requirements), so this working code is not allowed:
def shift_sixes(nums):
b = []
c = 0
d = []
for i in nums:
if i == 6:
b.insert(len(nums),i)
elif i != 6:
c = c +1
d.insert(c,i)
ans = d + b
return ans
I've also tried list.remove() and list.insert() but have gotten into trouble with the indexing (which moves when I insert() then move the element to the end): For example -
a = [6,4,6,2,3,6,9,6,1,6,5]
def shift_sixes(nums):
for i in nums:
if i == 6:
nums.remove(i)
nums.insert(nums[len(nums)-1], 0)
elif i != 0:
i
shift_sixes(a)
Additionally, I have tried to use the enumerate() function as follows, but run into problems on the right hand side of the b[idx] assigment line:
for idx, b in enumerate(a):
a[idx] = ???
Have read other stackoverflow entries here, here and here, but they do not tackle the movment of the element to one end.
Would appreciate any help on this list traversal / inplace switching issue. Many thanks.
EDIT
#eph - thank you. this is indeed an elegant response. I am sure it will pass my 'no new list' requirement? I surely intend to learn more about lambda and its uses
#falsetru - thank you for the reminder of the append/pop combination (which I tried to do in my original query via list.remove() and list.insert()
#tdelaney - thank you as well. somehow your response is closest to what I was attempting, but it seems not to pass the test for [0, 0, 5].
It is a bad idea to modify list while traverse. You can either make a copy to traverse, or generate a new list during traverse.
In fact, the question can be done in many ways, such as:
>>> a.sort(key = lambda i: i == 6)
>>> a
[4, 2, 3, 9, 1, 5, 6, 6, 6, 6, 6]
Iterating the list reverse way, pop the element if it's 6, then append it.
xs = [6,4,6,2,3,6,9,6,1,6,5]
for i in range(len(xs)-1, -1, -1): # 10 to 0
if xs[i] == 6:
xs.append(xs.pop(i))
Why not try something like this?
Basically, the approach is to first count the number of values.
If 0, then returns (since Python produces a ValueError if the list.index method is called for an element not in the list).
We can then set the first acceptable index for the value to be the length of the list minus the number of occurrences it exists in the list.
We can then combine list.pop/list.append to then traverse the list until all the values desired occur at the end of the list.
def shift_value(lst, value):
counts = lst.count(value) # 5
if not counts:
return lst
value_index = len(lst) - counts
index = lst.index(value)
while index != value_index:
lst.append(lst.pop(index))
index = lst.index(value)
return lst
lst = [6,4,6,2,3,6,9,6,1,6,5]
print(shift_value(lst, 6))
EDIT: This is horribly inefficient, better answer suggested above.
This requires O(n^2) time, rather than O(n) time.
The key term here is "In Line". The way you do that is move num[i] = num[i+1] for each i to the end of the list.
def shift_sixes(num):
for i, val in enumerate(num):
if val == 6:
# shift remaining items down
for j in range(i,len(num)-1):
num[j] = num[j+1]
# add 6 at the end
num[-1] = 6
return num
print(shift_sixes([1,9,4,6,2,7,8,6,2,2,6]))
print(shift_sixes([1,2,3]))
print(shift_sixes([6]))
print(shift_sixes([3]))
Use two runners. First from front to end checking for 6s, second from end to front pointing to last item that's not a 6. Keep swapping (a[i+1], a[i] = a[i], a[i+1]) until they meet.
Catch: this is not stable like in a stable sort. But I don't see that as a requirement.
Will try to write working code when in front of a python interpreter with a keyboard.
In case you need a stable sort (i.e. order of elements that are not 6 should remain the same), then the solution is:
def move_to_end(data, value):
current = 0 # Instead of iterating with for, we iterate with index
processed = 0 # How many elements we already found and moved to end of list
length = len(data) # How many elements we must process
while current + processed < length: # While there's still data to process
if data[current] == value: # If current element matches condition
data.append(data.pop(current)) # We remove it from list and append to end
processed += 1 # Our index remains the same since list shifted, but we increase number of processed elements
else: # If current element is not a match
current += 1 # We increase our index and proceed to next element
if __name__ == '__main__':
print
print 'Some testing:'
print
for test_case in (
[1, 9, 4, 6, 2, 7, 8, 6, 2, 2, 6], # Generic case
[6, 6, 6, 6], # All items are 6
[1, 7], # No items are 6
[], # No items at all
):
print 'Raw:', test_case
move_to_end(test_case, 6)
print 'Becomes:', test_case
print
Note that this solution retains the order of not only non-matching elements, but of matching elements as well. So for example, if you change the check condition from "equal to 6" to "is an even number", all elements matching the condition will be moved to the end of list while retaining their order among themselves.
Why not keep it simple?
a = [6,4,6,2,3,6,9,6,1,6,5]
def shift_sixes(nums):
for i in range(0,len(nums)):
if nums[i] == 6:
nums.append(nums.pop(i))
>>> shift_sixes(a)
>>> a
[3, 9, 1, 5, 2, 4, 6, 6, 6, 6]

Categories

Resources