This is my first Stack Overflow post and I hope it will be good.
This is a problem that I came up with myself, and now I'm a little ashamed to say, but it beats out the daylight from me. Please note that this is not homework, the honor of a scout.
In principle, the program accepts (as an input) a string consisting of integers from 0 to 9.
strInput = '2415043'
Then you need to break this string of numbers into smaller groups of numbers, until, in the end, the sum of these groups gives you a predetermined amount. In the case of the above line, the target is 289.
iTarget = 289
In this example, there are two correct answers (but, most likely, only one will be displayed, since the program stops after reaching the goal):
Answer 1 = 241, 5, 043 (241 + 5 + 043 = 289)
Answer 2 = 241, 5, 0, 43 (241 + 5 + 0 + 43 = 289)
Note that integers do not change position. They are still in the same order as in the original line.
Now I know how to solve this problem using recursion. But the disappointing part is that I DON'T ALLOW to use recursion.
This needs to be solved using only while and for loops. . And obviously, lists and functions are fine.
Below is the code that I still have:
My code is:
#Pre-defined input values, for the sake of simplicity lstInput = ['2','4','1','5','0','4','3']
-
def SumAll(_lst):
-
def AddComma(_lst):
So basically, the pseudocode will look something like this:
Pseudo Code:
while SumAll(lstWorkingList) != iTarget:
Phew! That was pretty much.
I worked out the process that the program will execute on a piece of paper, so the expected result is presented below:
OUTPUT:
[2415043] #Element 0 has reached maximum size, so add another group #AddComma() #Reset() [2, 415043] #ShiftGroups() [24, 15043] #ShiftGroups() [241, 5043] #ShiftGroups() #...etc...etc... [241504, 3] #Element 0 has reached maximum size, so add another group #AddComma() #Reset() [2, 4, 15043] #ShiftGroups() [2, 41, 5043] #ShiftGroups() #etc...etc... [2, 41504, 3] #Tricky part
Now here is the hard part. In the next step, the first element should be 24, and the other two should reset.
#Increase Element 0 #All other elements Reset() [24, 1, 5043] #ShiftGroups() [24, 15, 043] #ShiftGroups() #...etc...etc [24, 1504, 3] #Increase Element 0 #All other elements Reset() [241, 5, 043] #BINGO!!!!
Good. This is the main stream of program logic. Now the only thing I need to find out is how to make it work without recursion.
For those of you who have read up to this point, I sincerely thank you and hope that you still have the energy to help me solve this problem. If something is unclear, ask, and I will clarify (perhaps in the painful detail of XD).
Thanks again!
Edit: September 1, 2011
Thanks to everyone for the answer and for your answers. All of them are very good and, of course, more elegant than the route that I followed. However, my students have never worked with βimportβ or any data structures more advanced than lists. However, they know many functions of the list. I should also note that students are quite talented mathematically, many of them competed and placed in international mathematics competitions. Thus, this assignment does not go beyond their intellect; perhaps it goes beyond their knowledge of python.
I had Eureka last night! moment. I have not implemented it yet, but I will do it over the weekend, and then I will publish my results here. It may be a little rude, but I think he will do his job.
Sorry, it took me a long time to answer, my internet cap was reached, and I had to wait until the 1st for her to reset. Which reminds me, happy Spring of all (for those of you who are in the Southern Hemisphere).
Thanks again for your contributions. I will choose the top answer after the weekend. Hello!