I think there is something I am not understanding with an algorithm I'm working with. I have a binary tree where "*" is the root, "C" is on the left, "+" is on the right and + has "a" to the left and "b" to the right.
This is the algorithm:
Module Find_Value ( List, Root, Value ) {
If ( Root is NULL ){
Return NULL;
}Else{
If ( List [ Root ] .data equals Value )
Return Root;
Temp = Find_Value ( List, List[ Root ].left, Value )
If ( Temp is NULL )
Return Find_Value ( List, List[ Root ].right, Value );
Else
Return Temp;
}
}
And I'm supposed to carry out Find_Value(List, 0, b)
I could be just reading it wrong, but I do not see any instruction to go back up the tree. I get find_value(List,1,C) the first run-through and then I see temp as looking to the left of C (which is null) and the the right of C (also null). Can anyone tell me I am just interpreting the instructions incorrectly?
This is what is happening here:
(I have replaced * with 0,C with 2 for better understanding and also used them as identifiers for each temp at each step)
1) Temp(*) = Find_Value ( List, *.left, b );
2) Temp(C) = Find_value ( List,C.left,b); //returns null ,
therefore Temp(C) = NULL;
3) If ( Temp(C) is NULL )
Return Find_Value ( List, C.right, Value ); //returns null;
4) therefore the 1st step returns null;
Temp(*) = null;
5) If ( Temp(*) is NULL )
Return Find_Value ( List, *.right, Value );
6) Temp(+) = Find_Value ( List, +.left, Value );
7) Temp(a) = Find_Value ( List, a.left, Value ); // returns null
8) If ( Temp(a) is NULL )
Return Find_Value ( List, a.right, Value ); //returns null
therefore Temp(+) = NULL;
9) If ( Temp(+) is NULL )
Return Find_Value ( List, +.right, Value );
10) If ( List [ Root ].data equals Value )
Return Root;
where List[Root].data = b;
Step 9 returns b,followed by Step 5 returning b;
and overall the function return b;
Related
This is a simple request.
I have to create a column in query editor, or in table view. Whichever is easy.
Column looks like this -->
A,B,C,D,D,E,D
B,C,D,B,D,A
C,C,D,F,E,G
D,D,E,E,E,F,B
Result should be based on count of characters present, with 'A' character always taking the priority.
For instance result of the above column next to it will be
A ( A will take priority even if D has most count)
A (Even though B has most count, A will take Priority)
C ( as C has most count)
E ( as E has most count)
Result =
VAR String = COALESCE ( 'Table'[Column], "XX" )
VAR Items = SUBSTITUTE ( String, ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "#Item", PATHITEM ( Items, [Value] ) )
VAR T3 = GROUPBY ( T2, [#Item], "#Count", COUNTX ( CURRENTGROUP(), 1 ) )
VAR T4 = TOPN ( 1, T3, [#Count] )
VAR Result = MAXX ( T4, [#Item] )
RETURN
IF ( PATHCONTAINS ( Items, "A" ), "A", Result )
Blockquote
I’ve got a really large file, circa 10m rows, in which I’m trying to populate a column based on conditions on another column via a jsee macro. While it is quite quick for small files, it does take some time for the large file.
//pseudocode
//No sorting on Col1, which can have empty cells too
For all lines in file
IF (cell in Col2 IS empty) AND (cell in Col1 IS NOT empty) AND (cell in Col1 = previous cell in Col1)
THEN cell in Col2 = previous cell in Col2
//jsee code
document.CellMode = true; // Must be cell selection mode
totalLines = document.GetLines();
for( i = 1; i < totalLines; i++ ) {
nref = document.GetCell( i, 1, eeCellIncludeNone );
gsize = document.GetCell( i, 2, eeCellIncludeNone );
if (gsize == "" && nref != "" && nref == document.GetCell( i-1, 1, eeCellIncludeNone ) ) {
document.SetCell( i, 2, document.GetCell( i-1, 2, eeCellIncludeNone ) , eeAutoQuote);
}
}
Input File:
Reference
Group Size
14/12/01819
1
14/12/01820
1
15/01/00191
4
15/01/00191
15/01/00191
15/01/00198
15/01/00292
3
15/01/00292
15/01/00292
15/01/00401
5
15/01/00401
15/01/00402
1
15/01/00403
2
15/01/00403
15/01/00403
15/01/00403
15/01/00404
20/01/01400
1
Output File:
Reference
Group Size
14/12/01819
1
14/12/01820
1
15/01/00191
4
15/01/00191
4
15/01/00191
4
15/01/00198
15/01/00292
3
15/01/00292
3
15/01/00292
3
15/01/00401
5
15/01/00401
5
15/01/00402
1
15/01/00403
2
15/01/00403
2
15/01/00403
2
15/01/00403
2
15/01/00404
20/01/01400
1
Any ideas on how to optimise this and make it run even faster?
I wrote a JavaScript for EmEditor macro for you. You might need to set the correct numbers in the first 2 lines for iColReference and iColGroupSize.
iColReference = 1; // the column index of "Reference"
iColGroupSize = 2; // the column index of "Group Size"
document.CellMode = true; // Must be cell selection mode
sDelimiter = document.Csv.Delimiter; // retrieve the delimiter
nOldHeadingLines = document.HeadingLines; // retrieve old headings
document.HeadingLines = 0; // set No Headings
yBottom = document.GetLines(); // retrieve the number of lines
if( document.GetLine( yBottom ).length == 0 ) { // -1 if the last line is empty
--yBottom;
}
str = document.GetColumn( iColReference, sDelimiter, eeCellIncludeQuotes, 1, yBottom ); // get whole 1st column from top to bottom, separated by TAB
sCol1 = str.split( sDelimiter );
str = document.GetColumn( iColGroupSize, sDelimiter, eeCellIncludeQuotes, 1, yBottom ); // get whole 2nd column from top to bottom, separated by TAB
sCol2 = str.split( sDelimiter );
s1 = "";
s2 = "";
for( i = 0; i < yBottom; ++i ) { // loop through all lines
if( sCol2[i].length != 0 ) {
s1 = sCol1[i];
s2 = sCol2[i];
}
else {
if( s1.length != 0 && sCol1[i] == s1 ) { // same value as previous line, copy s2
if( s2.length != 0 ) {
sCol2[i] = s2;
}
}
else { // different value, empty s1 and s2
s1 = "";
s2 = "";
}
}
}
str = sCol2.join( sDelimiter );
document.SetColumn( iColGroupSize, str, sDelimiter, eeDontQuote ); // set whole 2nd column from top to bottom with the new values
document.HeadingLines = nOldHeadingLines; // restore the original number of headings
To run this, save this code as, for instance, Macro.jsee, and then select this file from Select... in the Macros menu. Finally, select Run Macro.jsee in the Macros menu.
I need to separate a number with commas
for example number 142531 to show as 14:25:31
Googled for a while and found something like this:
Column =
VAR right =
RIGHT ( [Column1], 3 )
VAR left =
SUBSTITUTE ( [Column1], right, "" )
RETURN
COMBINEVALUES ( ":", left, right )
This kinda shows me how to do it, I can separate the number in the middle with 1 comma but not sure how to do this with the middle part.
Time =
VAR left = LEFT ( [RCP_TIME], 2 )
VAR mid = MID ( [RCP_TIME], 3, 2 )
VAR right = RIGHT ( [RCP_TIME], 2 )
RETURN
COMBINEVALUES(":",left, mid, right)
i have made a spell checker code using Ternary search tree. Can anybody tell me how to find the next possible word in TST.
for example if i want to search if i search a word "Manly" in spell checker and the word is not present in TST, so the output it gives like
DO YOU MEAN:
"Man"
"Mango"
.
.means possible near words
I have implemented my own spell checker but instead of a simple ternary trie I am using a ternary dag as Peter Kankowski suggests here. You can look at my blog for some details and how I did it. Its in Greek, but you can get an idea.
Edit:
Ok, you are right.
The basic idea is to use a pre-created list of candidates for a given edit distance ( a value of 2 for me is ok ). To reduce the size of the list one can use wildcard characters.
Such a list, of course, can be constructed in different ways. I prefer for / while loops like this ( e.g. for candidates of two substitutions )
void Substitute2( vector<wchar_t*>& v, const wstring& w )
{
size_t len = w.size();
if ( len < 2 )
return;
size_t p1 = 0, p2 = 1;
while ( p1 < len ) {
p2 = p1 + 1;
while ( p2 < len ) {
wchar_t* chars = new wchar_t[ len + 1 ];
for ( size_t i = 0; i < len; ++i ) {
if ( i != p1 && i != p2 ) {
chars[ i ] = w[ i ];
}
}
chars[ p1 ] = '?';
chars[ p2 ] = '?';
chars[ len ] = '\0';
v.push_back( chars );
p2++;
}
p1++;
}
}
After having prepared the candidates list, a simple search in a ternary dag for each item in the list will give us the suggestions for that misspelled word.
void Search( FileNode* pDict, FileNode* pNode, const wchar_t* Word, wstring Sug, set<wstring>& List )
{
if ( IsNullLink( pNode, pDict ) )
return;
if ( *Word == '?' ) {
Search( pDict, GetLo( pNode, pDict ), Word, Sug, List );
Search( pDict, GetEq( pNode, pDict ), Word + 1, Sug + pNode->Char, List );
Search( pDict, GetHi( pNode, pDict ), Word, Sug, List );
} else {
if ( *Word < pNode->Char ) {
Search( pDict, GetLo( pNode, pDict ), Word, Sug, List );
} else if ( *Word > pNode->Char ) {
Search( pDict, GetHi( pNode, pDict ), Word, Sug, List );
} else {
if ( pNode->Char == '\0' )
{
List.insert( Sug );
}
if ( *Word != '\0' ) {
Search( pDict, GetEq( pNode, pDict ), Word + 1, Sug + pNode->Char, List );
}
}
}
}
Note: The dictionary is a compiled (file based) ternary dag
The search for the word in your TST will terminate at a specific location in the tree. From this point, you can simply go up one level in the tree until you hit a level where there is more than just the child you came from.
On that level, you can simply choose the other possible paths and return those words.
I'm supposed to use recursion to output the total number of unique north-east paths ne(x, y) to get from point A to point B, where B is x rows north and y columns east of A. In addition, I am required to print the possible unique NE paths.
I know how to use recursion to get the total number of unique paths. However, I am stuck with using recursion to print all the NE paths correctly.
This is the given output of some test cases:
image of output
Anyway, here's a screenshot of my faulty recursive code.
Please do give me advice where I went wrong. I have been burning a lot of time on this, but still I can't reach a solution.
I think you should print if( rows == 0 && cols == 0 ), because it's the case when you've reached point B.
Why are you using path+="N" in the first ne call in return? this will add "N" to original path and then you'll get path+"N"+"E" in the second call.
Try following:
public static int ne( int rows, int cols, String path )
{
if( rows == 0 && cols == 0 )
{
System.out.println(path);
return 1;
}
int npats = 0, wpaths = 0;
if( rows != 0 )
npaths = ne( rows-1, cols, path+"N" );
if( cols != 0 )
wpaths = ne( rows, cols-1, path+"E" );
return npaths + wpaths;
}