Binary Search algorithm - binary-search

I'm using binary search to locate a record. My question is not about whether the data is present or not. I'll list my questions below.
if data are in following order
1 2 3 4 4 5 5 5 6 7 8 8 9 10 11
If now i want to search 5 where it is locate first. By using binary search algorithm I can get the 5 is present or not. in the above case i can get the 6th location of 5( totally 13 data) . But I need to get 5th location. How can i get this using binary search ? Once again in some cases i need to get last position of the given data. How can i get this binary search algorithm.
Is there any fastest method than binary search ? Not but hashing method ?

You could use a binary search again.
To get the lowest value of 5, start the search again but with its upper bounds 1 less than the current found value of 5.
To get the higest value of 5, start the search again but with its lower bounds 1 greater than the current found value of 5.
You must repeat the process above until you can no longer find the item, when you can no longer find the item, the last item found was the one you were looking for.

Related

Freemarker: Removing Items from One Sequence from Another Sequence

This may be something really simple, but I couldn't figure it out and been trying to find an example online to no avail. I'm basically trying to remove items found in one sequence from another sequence.
Example #1
Items added to the cart is in one sequence; items removed from cart is in another sequence:
<#assign Added_Items_to_Cart = "AAAA,BBBB,CCCC,DDDD,EEEE,FFFF">
<#assign Deleted_Items_from_Cart = "BBBB,DDDD">
The result I'm looking for is: AAAA,CCCC,EEEE,FFFF
Example #2
What if the all items added to and deleted from cart are in the same sequence?
<#assign Cart_Activity = "AAAA,BBBB,BBBB,CCCC,DDDD,EEEE,DDDD,FFFF,Add,Add,Delete,Add,Add,Add,Delete,Add">
The result I'm looking for is the same: AAAA,CCCC,EEEE,FFFF
First things first: You ask about sequence but the data you are dealing with are strings.
I know you are using the string to work as a sequence (and it works), but sequences are sequences and strings are strings, and they have diferente ways of dealing with. I just felt this was important to clarify if someone who is starting to learn how to program get to this answer.
Some assumptions since you're providing strings with data separated by comma:
You want a string with data separated by comma as a result.
You know how to properly create strings with data separated by comma.
You dont have commas in your items names.
Observations:
I'll give you the logic but not the code donne, as this can be a great chance for you to learn/practice freemarker (stackoverflow spirit, you know...)
You question is not about something specific of freemaker (it just happens to be the language you want to work with). Think about adding the logic tag to you question. :-)
Now to the answer on how to do what you want on a "string that is working as a sequence":
Example #1
Change your string to a real sequence :-)
1 - Use a built-in to split your string on commas. Do it for both Added_Items_to_Cart and Deleted_Items_from_Cart. Now you have two real sequences to work with.
2 - Create a new string tha twill be your result .
3 - Iterate over the sequence of added itens.
4 - For each item of the added list, you will check if the deleted list also contains this item.
4.1 - If the deleted list contains the item you do nothing.
4.2 - If the deleted list do not contains the item, you add that item to your string result
At the end of this nested iteration (thats another hint) you should get the result you're looking for.
Example #2
There are many ways of doing it and i'll just share the one that pops out of my mind right now.
I think it's noteworthy that in this approach you will always have an even sized list, as you always insert 2 infos each time: item and action.
So always the first half will be the 'item list' and the second half will be the 'action list'.
1 - Change that string to a sequence (yes, like on the other example).
2 - Get half of its size (in your example size = 16 so half of it is 8)
3 - Iterate over a range from 0 to half-1 (in your example 0 to 7)
4 - At each iteration you'll have a number. Lets call it num (yes I'm very creative):
4.1 - If at the position num + half you have the word "Add" you add the item of position num in your result string
4.2 - If at the position num + half you have the word "Delete" you remove the item of position num from your result string
And for the grand finale, some really usefull links that will help you in your freemarker life forever!!!
All built-ins from freemarker:
https://freemarker.apache.org/docs/ref_builtins.html
All directives from freemarker:
https://freemarker.apache.org/docs/ref_directive_alphaidx.html
Freemarekr cheatsheet :
https://freemarker.apache.org/docs/dgui_template_exp.html#exp_cheatsheet

Wiki API - Parsing sentences from JSON extracts in JavaScript?

Is there a way to have wiki display extracts in an array of sentences?
Or does anyone have any ideas other than using string.split(".") to parse? There are cases where the sentence may include a . and I don't want to split if it occurs mid-sentence.
For example, "The Eagles were No. 1 in the U.S. in 1970" would be split into 4 sentences using str.split(), and that's not what I want.
Wiki must have some sort of determination of what defines a sentence as it works when you limit the number of existence in a call (they don't break a sentence on an in-line period). Is there a way to get them individually?
Looking for a solution in JavaScript to parse a JSON excerpt string.
I ended up figuring out a work-around. Using exsentences, I made 10 calls, each with one more sentence than the previous call. I stored the results of each call in an array. So when the 10 calls were complete, I had 10 strings, ranging from one sentence in position 0, up to 10 sentences in the 9th position. Then I just iterated through the array, from 0 to length - 2, subtracting the string in the current position from the string at position [i + 1] (with string[i + 1].slice(string[i].length)), to get the nth string.

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 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

Sum of numbers in array

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.