Sum of numbers in array - dynamic

I am new to programming and I try to solve problems in online-judging system. There is a problem, which looks very interesting and important, but unfortunately I have no idea how to solve that. I would appreciate any hints.
Here is the problem: Given an array as an input. Print 1 if there are two subarrays, which have the same sum of numbers, otherwise print 0.
Input:
2
1 1
Output:
1
Input:
3
2 5 3
Output:
1
Input:
3
1 4 7
Output:
0
Thanks

This is known as the partition problem (or at least a variant of the partition problem). It's a problem that requires what is called a dynamic programming solution. This is a pretty advanced problem for someone who is just beginning to learn how to program. I advise starting on some easier challenges.
If, however, you are interested in tackling this beast, check out this link: http://people.csail.mit.edu/bdean/6.046/dp/. The link on this page to the 'partition problem' shows a video explanation of a working solution.

Related

Pandas dataframe [] issue

I am new to python. Could someone help me why the first code statement generates no coding error but the second one will generate a keyerror(I do not even know what is a keyerror).
p.s.: data is a DataFrame,'StrategyCumulativePct' and 'BuyHold' are two columns in the DataFrame respectively.
data[['StrategyCumulativePct', 'BuyHold']].plot()
data['StrategyCumulativePct', 'BuyHold'].plot()
On the other hand, may I ask why sometimes when I had only written 20 lines of codes but there could be errors pointing an arrow at lines 2000 / 3000.... which I have not created before? Thanks.

Can you help me with this simple RobotC exercise?

I mean 2. Second Length found here
I think I found all mistakes but still, I can't seem to get the right answer.
On line 8 it should be wait1Msec(3000); instead of wait1Msec(4000);
Swing turn to the right should be written as 100 power for C and 0 power for B. I also tried -100 for B and even exchanged letters just in case I misunderstood which of the motors is left and which is right, but I still got the wrong answer (according to the website).
There are mistakes in the comments as well, but that shouldn't be a problem since they're just comments! Still, I corrected them just in case. Replaced 4 with 3 and replaced right with left, still not correct.
Do they expect me to right some specific text in the comments? Or am I missing something?
It seems that you should write the code for your second length after the comment
//END OF FIRST LENGTH
So, your code should be a code for the first length and the second length. I hope that helps.

Is it possible to perform this SumIf without knowing the target range? [duplicate]

This question already has answers here:
How do I sum data based on a PART of the headers name?
(2 answers)
Closed 6 years ago.
This is my code so far, refer to this question as to what I am trying to do.
=SUM(IF(LEFT(C$2,4)="/111",SUM(C3,G3)))
As you can see, I am defining C2, the point of my header, and in the Sum, I am defining the sum range. I now have this question:
How do I proceed if I do not know where my header is exactly, but I
know that I'm looking for a 'part' of the header. How do I search for
this? With an HLookUp? This is because these headers may be placed in just about any order, because of this, I will need to search for the text rather than define its place.
For example, I have this table:
+-------------+-------------+-------------+-------------+-------------+
| A | B | C | D | E |
+---+-------------+-------------+-------------+-------------+-------------+
| 1 |/650 - Black |/670 - White |/800 - White |/680 - Red |/650 - Black |
+---+-------------+-------------+-------------+-------------+-------------+
| 2 | 250 | 400 | 100 | 300 | 125 |
+---+-------------+-------------+-------------+-------------+-------------+
I want to sum up the row of data where I specify the 4 character LEFT limited header, as it is in the code I posted. The problem here is that the code is more of a, "This.Cell, if it is your desired header, add stuff" when I wanted something more like "Where.Cell matches LEFT limited header, add their stuff (horizontal by other matches, not vertical) I think an Index Match is what I'm looking for, but from what I gathered, the data would have to be defined on a cell, which, as per the table above, does not contain it.
I created a separate question as I believe the requirements were not stated properly the first time, resulting in a different direction and a different answer.
Pure formula is preferred, but if VBA is what you've thought up, I'd still like to read it.
TLDR: In excel, I define several left limited headers, left limited because past the / and four digits, the rest of the text may be different. I search the sheet for these headers, and when found, I sum them up and place them at the point of this formula.
I hope I was able to clarify my requirements clearer this time!
EDIT:
So I just wanted to reiterate that the requirements of this formula is that I have no idea where my headers are located despite the table I wrote above. The idea is that I will always have a /650, a /670, or a /680. The number arrangement doesn't even matter at this point. I don't know if this is possible at all, because, since I don't know where the headers are, only what they are, I don't know what letter the cell will be on, only that it is the "cell where the header was found".
Say I want /670 and a /680. Formula finds the header /670 first, and gets its cellspace like "/670 is on C1! Therefore the number that has to be added is C2!". Proceed to the next, /680 and do the same thing. In the end, I hit enter on the formula and out comes the sum of these two headers = 700.
I'm thinking in terms of C# code where I could probably state that the range to be added is based on the letter of the header I found, no idea if excel can do this much less as a draggable formula, which is why I posted this question.
According to your sample table and your description this is what SUMPRODUCT is for. Sum products of arrays. Here the first array is an array of 0 or 1 where 1 is if the header matches one of the desired headers 0 if not. The second array is the array of values to sum.
So:
=SUMPRODUCT((LEFT($A$1:$E$1,4)={"/670";"/650";"/680"})*$A$2:$E$2)
will result in 1075 = 250 + 400 + 300 + 125.
The desired headers could also be given in a cell range. But this must be a column range (1 column, multiple rows). For example given the folowing
G
1 /650
2 /670
3 /680
then:
=SUMPRODUCT((LEFT($A$1:$E$1,4)=$G$1:$G$3)*$A$2:$E$2)
I assume, what you're looking for is an array formula which is almost the same as the one you already used:
{=SUM(IF(LEFT(A1:A3,4)="/670",B1:B3),IF(LEFT(A1:A3,4)="/680",B1:B3))}
Hit CTRL + SHIFT + ENTER

Why does this search for [help/dont-ask] return irrelevant results in DSE?

Why does this ridiculously simple query on data.stackexchange.com return results that don't have [help/dont-ask] in the comment text? I feel like I'm missing something mind-numbingly obvious here.
select top 10 Id, PostId, Text
from comments
where text like '%[help/dont-ask]%'
Results I currently get:
Id PostId Text
-- ------- -----------------------------------
1 35314 not sure why this is getting downvoted -- it is correct! Double check it in your compiler if you don't believe him!
2 35314 Yeah, I didn't believe it until I created a console app - but good lord! Why would they give you the rope to hang yourself! I hated that about VB.NET - the OrElse and AndAlso keywords!
4 35195 I don't see an accepted answer now, I wonder how that got unaccepted. Incidentally, I would have marked an accepted answer based on the answers available at the time. Also, accepted doesn't mean Best :)
9 47239 Jonathan: Wow! Thank you for all of that, you did an amazing amount of work!
10 45651 It will help if you give some details of which database you are using as techniques vary.
12 47428 One of the things that make a url user-friendly is 'discover-ability', meaning you can take a guess at url's simply from the address bar. http://i.love.pets.com/search/cats+dogs could easily lead to http://i.love.pets.com/search/pug+puppies etc
14 47481 I agree, both CodeRush and RefactorPro are visually impressive (most of which can be turned off BTW), but for navigating and refactoring Resharper is much better in my opinion of using both products.
15 47373 Just wanted to mention that this is an excellent solution if you consider the problem to be linear (i.e. treating `A1B2` as a single number). I still think the problem is multi-dimensional, but I guess we'll just have to wait for the author to clarify :)
16 47497 Indeed, the only way to do this is get the server to generate your CSS file which can be done in many ways depending on which language you are using. HttpHandlers are common in C#. You could use jQuery or the likes to add styling to every element with the class 'ourColur' and parametrise your JS
18 47513 This advice goes against the spirit of CSS, which is separation of content and presentation. You way requires changing HTML for presentation sake, and stating in content which elements have same color.
...none of which contains the magic link (or even the text dont-ask).
Because [] delimits a set of characters to find.
You need to escape them.
Or just use CHARINDEX as the search is unsargable anyway.
WHERE CHARINDEX('[help/dont-ask]', text) > 0

Why does the rebol interpreter return different results?

Consider:
>> print max 5 6 7 8
6
== 8
The documentation states that max only takes two arguments, so I understand the first line. But from the second line it looks like the interpreter is still able to find the max of an arbitrary number of args.
What's going on here? What is the difference between the two results returned? Is there a way to capture the second one?
I don't really know Rebol but what I do notice is that you're using print inside of th REPL. The first output is from print, which is outputting the result of max 5 6. The second output is from the REPL, which is outputting the value of your whole expression — which is maybe just the last item in the list? If you changed the order of your inputs, I bet you would see a different result.
max is an abbreviation for maximum. As #hobbs correctly guessed, it takes two arguments, and what you're seeing is just the evaluator's logic of turning the crank...and becoming equal to the last value in the expression. In this case you're not using that result, so the interpreter shows it to you with "==". But you could have assigned that whole expression to a variable (for instance).
What you were intending is something that gets the maximum value out of a series. In the DO dialect all functions have fixed arity, and the right way to design such a beast would be to make it take one argument...the series.
Such a thing does exist, though there isn't an abbreviation...
>> print maximum-of [5 6 7 8]
8