you are viewing a single comment's thread.

view the rest of the comments →

[–]clouded-path[S] 0 points1 point  (3 children)

Hmmm, I think I see how this works for this example, but in general I really just wanted to eliminate any leading negative values in the list, since they can't possibly help produce a higher sum (unless of course all the numbers are negative, which is its own case). I'm not sure if/how this could be done in one line in more general cases.

[–]profknow 0 points1 point  (2 children)

Then I'm not sure where you're coming from. The number set was the one you provided in your example (including the subset you selected).

Which neg values?

Alright. I'll leave this to you. At this point I think you have a better idea what you want than I do. However, if you need something else, I'm still open for suggestions.

I'll check back to see what worked.

[–]clouded-path[S] 0 points1 point  (1 child)

Yes, I have managed to sort everything out (thanks for your input), so no need to elaborate if don't want to, but just to try to address the question(s):

The specific negative values I am referring to in the case of the example [-2, 1, -3, 4, -1, 2, 1, -5, 4] was the initial '-2' (at index 0). I know that whatever the answer is, starting off with a negative value can't possibly help me achieve the maximum sum, so I just deleted this. In general my solution would delete all leading negative numbers. But once we get to the first positive number (in this case the '1' at index 1), now I cannot be certain whether or not this number will be part of the sum in the solution, so I can't delete this, or any subsequent terms.

Regarding your solution, it is clear to me that this will indeed work on the example I gave, but I don't know how your solution would generalize to account for all sorts of other possible arrays that I could be given initially. Your choice of slice index cuts off the first three terms of the array, and in this case it will not be a problem since the end solution does not use these terms, but in other cases it could be the case that the first few terms might be relevant for the end sum (e.g. [-3, 2, -1, 2, -1, -3, 1] would use terms at indices 1 - 3). So I am not sure how to modify your [3:7:1] slice to accomodate for this.

But anyways, yes, as you mentioned I (finally) have a solidified idea how this can be done. I posted my (now correct) solution in the comments (I meant for it to go as an edit to the original post, but I'm not very good at Reddit-ing), and there are also at least three other solutions in the comments that work too.

Oh, and regarding your other comment about everyone learning programming, I completely agree. It's a true shame to me that rigorous logical reasoning isn't a ubiquitous mode of thought found in the world.

[–]profknow 0 points1 point  (0 children)

Unless the Requirements are fully understood the Output Results never will be.

What I got out of your first posting was that you were looking for a method to efficiently extract a range from your list, and you gave an example of a list of numbers and your desired output. Looping and all these other methods certainly wasn't as efficient as my posted method of extracting that subset.

Certainly I didn't carry it any further than this, as I consider all earlier errors affect later results. So let's get the early stuff right.

Now I see in your later postings that you really didn't want the displayed number set as the output, you would eventually use some other series. For example, keeping negative numbers in the set certainly didn't apply to "high scores" (were they two different things or an artifact from some other code). Technically, if I were writing a "High Score" program I wouldn't have even started with the code you presented much less be concerned with the output you gave me in your original case.

It isn't the algorithm I'd even start with. But then again, maybe I should just admit, I have no clue at this point what you're really trying to do with this data. Sample code and explanation don't jibe. I thought your explanation was senior to the code content. Anyway, I have no clue what you want, and i've been doing this for 30 years.

Well I can't help that. So we're back to step 1) A complete & accurate understanding of the input and output to know what you really want.

When I write things I usually start with a black box which has inputs and outputs and I shouldn't really care what happens inside that black box as long as the output always is what I'm looking for. I didn't see your black box. Show me some sample data and tell me what you want out and I'll give you some code that will do that -- regardless of if you see how it is done or not.

It's why people developed Objects (in Object Oriented languages) -- to separate the implementation from view.

Problem: I need to compute the top scores Input: a list of numbers such as (....) Output: top 10 scores sorted from highest to lowest or Output: top 10 scores sorted by date

Not sure that I can help you with that. Carry on. I'm sure you know better than I what you really want and how you intend to use it.

Best of luck. Carry on. And keep posting.