Ebeworld’s Weblog

Trying to create

JSONPath

As shown below, Json is a bit cumbersome to work with and specially when user query it, they have to go through a sort of DFS. Look at http://goessner.net/articles/JsonPath/, where Stephen Goessner developed JSONPath which is analogy of XPath for JSON. Remembering, JSON is a native for javascript and more compact, JSONPath is a good idea.

{ “book”: [
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99 }, { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    “bicycle”: {
      “color”: “red”,
      “price”: 19.95
    }
  }

JSONPath expression

 

XPath JSONPath Result
/store/book/author $.store.book[*].author the authors of all books in the store
//author $..author all authors
/store/* $.store.* all things in store, which are some books and a red bicycle.
/store//price $.store..price the price of everything in the store.
//book[3] $..book[2] the third book
//book[last()] $..book[(@.length-1)]
$..book[-1:]
the last book in order.
//book[position()<3] $..book[0,1]
$..book[:2]
the first two books
//book[isbn] $..book[?(@.isbn)] filter all books with isbn number
//book[price<10] $..book[?(@.price<10)] filter all books cheapier than 10
//* $..* all Elements in XML document. All members of JSON structure.

August 11, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Hard prob

Problem Statement

You have a rectangular piece of paper that’s divided into 1×1 cells, each of which has an integer value. The paper will be described by a vector <string> paper. The ith element of paper will be a space delimited list of integers, where the jth integer of the ith element of paper represents the value of the jth cell of the ith row of the paper.

You want to perform a sequence of folds on the paper, where you may fold anywhere along an axis that is in between two rows or columns of the paper. After performing a fold, we wish to model the folded paper as a new, flat piece of paper. We will do this by considering two overlapping cells as a single cell, with a value that is the sum of the individual cells.

You wish to perform a sequence of folds such that the value of some single cell in the resulting piece of paper is as large as possible. Return this value.

Definition

Class: FoldThePaper
Method: getValue
Parameters: vector <string>
Returns: int
Method signature: int getValue(vector <string> paper)
(be sure your method is public)

Constraints

- paper will contain between 1 and 12 elements, inclusive.
- Each element of paper will be a single-space delimited list of integers with no leading or trailing spaces.
- Each element of paper will contain between 1 and 12 integers, inclusive.
- Each element of paper will contain the same number of integers.
- Each element of paper will contain between 1 and 50 characters, inclusive.
- Each integer in paper will be between -100 and 100, inclusive.
- Each integer in paper will have no leading zeros.
- An integer in paper equal to zero will not have a preceding negative sign.

Examples

0)
{
"1 1 1",
"1 1 1"
}
Returns: 6
We can collapse every cell onto the upper-left cell.
1)
{
"1 -1",
"1 -1"
}
Returns: 2
We should perform only the fold between the two rows, and take the resulting left column.
2)
{
"1 -1 -1 1",
"-1 -1 -1 -1",
"-1 -1 -1 -1",
"1 -1 -1 1"
}
Returns: 4
Folding between the middle rows then the middle columns allows us to combine the four corner cells.
3)
{
"20 13 -2 100",
"-12 0 4 -3",
"4 1 -36 21"
}
Returns: 131
4)
{
"0"
}
Returns: 0

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

July 24, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Simple prob

Problem Statement

There is nothing more beautiful than just an integer number.
You are given an integer n. Write down n in decimal notation with no leading zeroes, and let M be the number of written digits. Perform the following operation exactly k times:
Choose two different 1-based positions, i and j, such that 1 <= i < j <= M. Swap the digits at positions i and j. This swap must not cause the resulting number to have a leading zero, i.e., if the digit at position j is zero, then i must be strictly greater than 1.
Return the maximal possible number you can get at the end of this procedure. If it’s not possible to perform k operations, return -1 instead.
Definition

Class:
TheSwap
Method:
findMax
Parameters:
int, int
Returns:
int
Method signature:
int findMax(int n, int k)
(be sure your method is public)

Constraints
-
n will be between 1 and 1,000,000, inclusive.
-
k will be between 1 and 10, inclusive.
Examples
0)

16375
1
Returns: 76315
The optimal way is to swap 1 and 7.
1)

432
1
Returns: 423
In this case the result is less than the given number.
2)

90
4
Returns: -1
We can’t make even a single operation because it would cause the resulting number to have a leading zero.
3)

5
2
Returns: -1
Here we can’t choose two different positions for an operation.
4)

436659
2
Returns: 966354

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

July 24, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Good finmath books

Theory of financial risk and derivative pricing

 Modeling Derivatives in C++

April 27, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Fast insert

Because of locking and serialization, plus logging inserting once is much more faster than inserting by loop. Here i have tested it using CTE

WITH 

 

 

 

– second CTE which returns 100 million rows by using

 

– a CROSS JOIN on the first CTE 

, dig AS (
SELECT
(millions.Number * 1000000)
+ (hThousands.Number * 100000)
+ (tThousands.Number * 10000)
+ (thousands.Number * 1000)
+ (hundreds.Number * 100)
+ (tens.Number * 10)
+ ones.Number AS Number
FROM digits AS ones
CROSS JOIN digits AS tens
CROSS JOIN digits AS  hundreds
CROSS JOIN digits AS  thousands
CROSS JOIN digits AS tThousands
CROSS JOIN digits AS hThousands
CROSS JOIN digits AS millions
)

 – Third CTE which generates a “number”. repeated after 50M

 

 , nums AS (
SELECT number
FROM dig
WHERE number <5000000
)

 

 

– the insert statement goes here

 

INSERT Test.dbo.PerformanceTester (col1,col2,col3)
SELECT number,number, number
FROM  nums

 and performance gain is more than 40 times. Cool.  but this query is bound to memory and if we try to increase 5M to 50M, will complain about revising long running transactions. My trick to this is

insert  

 

 

which basically doubles size of the table by 2 much faster.

into Test.dbo.PerformanceTester(col1,col2,col3)
select col1,col2,col3 from Test.dbo.PerformanceTester

– first CTE which returns 10 rows (0-9)
digits AS (
SELECT 0 as  Number
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
)

April 20, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

SQL Server: How to get all table columns?

select column_name 
from information_schema.columns
where table_name = ’my_table_name’

April 17, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Dependency Injection

A object holds reference to certain interface and object, therefore by setting it  is a way of injecting dependency. Really simple example at http://en.wikipedia.org/wiki/Dependency_injection.

It is somehow related with IoC, where Hollywood principle “Don’t call us, we’ll call you” works

April 4, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Preventing double click

this.bSubmit.Attributes.Add(“onclick”, ” this.disabled = true; ” + ClientScript.GetPostBackEventReference(bSubmit, null) + “;”);

March 29, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Sleaky web UI

QuestionPro

March 23, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet

Distributed query.

Add server info
EXEC sp_addlinkedserver [servername]

Add login to the server:
EXEC sp_addlinkedsrvlogin @rmtsrvname =[servername], @useself = ‘false’, @rmtuser = ’sa’, @rmtpassword= ‘Dell@123′

check it using sys.servers or directly in SSMS

March 23, 2009 Posted by ebeworld | Uncategorized | | No Comments Yet