whihathac's Den

code is poetry

Format XML in C#

Found an interesting link - http://www.shrinkrays.net/code-snippets/csharp/format-xml-in-csharp.aspx

This is to format XML in C#. I have found it quite useful to view and analyze the XML returned from the Web Services.

/// <summary> 
/// Formats the provided XML so it's indented and humanly-readable. 
/// </summary> 
/// <param name="inputXml">The input XML to format.</param> 
/// <returns></returns> 
public static string FormatXml(string inputXml) 
{ 
    XmlDocument document = new XmlDocument(); 
    document.Load(new StringReader(inputXml)); 
 
    StringBuilder builder = new StringBuilder(); 
    using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(builder))) 
    { 
        writer.Formatting = Formatting.Indented; 
        document.Save(writer); 
    } 
 
    return builder.ToString(); 
}

Regards,
Bhavik

IPL 3 – Mathematics for Semis

I and Sandy (Sandeep Kumar) got to the inside of the mathematics behind the top 4 slots for IPL 3 Semis after considering all possible outcomes (2*2*2) of the remaing 3 matches and the most likely impact on the Net Run Rate. This does brings out interesting facts.

Following is the current standings (as on 4/17)–>

Team

Matches Points NRR
Mumbai Indians (MI) 13 20 1.251
Royal Challengers Bangalore (RCB) 14 14 0.219
Delhi Daredevils (DD) 13 14 0.066
Deccan Chargers (DC) 13 14 -0.363
Chennai Super Kings (CSK) 13 12 0.27
Kolkata Knight Riders (KKR) 13 12 -0.456
Rajasthan Royals (RR) 14 12 -0.514
Kings XI Punjab (KXIP) 13 8 -0.49

On Following are the analysis for each teams:

1. Mumbai Indians: They have already qualified for Semis and will pick up the 1st slot. The only unanswered question is whom do they play for Semis. They will see RCB if CSK wins against KXIP else they’ll see the losing team of DC and DD and with a very slight chance of seeing KKR.

2. Royal Challengers Bangalore: They are surely in Semis. The position now depends on CSK vs KXIP. If CSK win, RCB will take the 4th berth and play MI in Semis else they remain on 2nd berth and avoid MI in Semis.

3. Delhi Daredevils: If they win against DC, they take the 2nd berth. They can still qualify for the Semifinals provided CSK loses against KXIP. KKR shouldn’t pose problem since their runrate is quite high and they should try to maintain the run-rate while losing to DC.

4. Deccan Chargers: Same case as DD. If they win against DD, they take the 2nd berth. They can still qualify for the Semifinals provided CSK loses against KXIP but they should control the net run rate (while losing to DD) so that they don’t fall behind KKR and lose the semi-finals spot to them.

5. Chennai Super Kings: Couldn’t get more simple than this. CSK needs to win against KXIP to be in Semis. If they win, they’ll take up the 3rd Spot.

6. Kolkata Knight Riders: Chances are quite low, but still statistically possible. They do still need to beat MI (quite convincingly) and then hope that the winner of DC vs DD wins with a huge margin. Though looking at the present net run rate KKR should hope that DD wins against DC.

7. Rajasthan Royals: Well its THE-END for the team. No chances to play in Semis this year. :(

8. Kings XI Punjab: KXIP wont play the Semis, but they can still stop CSK from qualifying if they win against them.

To Reference follow the below table to check who’ll be the Top –4 teams and which are the teams playing Semis 1 and Semis 2.

KXIP vs CSK DD vs DC MI vs KKR Points table (top 4) Semi Final 1 Semi Final 2
KKIP DD MI MI,DD,RCB,DC MI vs DC DD vs RCB
KKIP DD KKR MI,DD,RCB,(DC, KKR) MI vs (DC or KKR) DD vs RCB
KKIP DC MI MI,DC,RCB,DD MI vs DD DC vs RCB
KKIP DC KKR MI,DC,RCB,(DD, KKR*) MI vs (DD or KKR) DC vs RCB
CSK DD MI MI,DD,CSK,RCB MI vs RCB DD vs CSK
CSK DD KKR MI,DD,CSK,RCB MI vs RCB DD vs CSK
CSK DC MI MI,DC,CSK,RCB MI vs RCB DC vs CSK
CSK DC KKR MI,DC,CSK,RCB MI vs RCB DC vs CSK

Regards,

Bhavik and Sandeep.

P.S.: Thanks to Ranjit Salvi (die hard fan of KKR) : this analysis was also done in a bid to prove that chances of KKR qualifying for Semis are even less than 1/8 (considering net run rate is a major factor). ;)

For Swayams query - Swayam Khialani :  bangalore can still (statistically) get knocked out if kolkata beat mumbai by a huge huge huge margin and if chennai beat kings 11... then it would be MI, DD/DC, Chennai, Kolkata in semis... btw, by how much does kolkata need to beat mumbai to stand a possible chance???

Our joint answer:

Considering your scenario: MI(1st spot), DD/DC Winner (2nd spot), Chennai (3rd spot) - Fixed!!

NRRs: CSK (considering a win) > 0.270; DD < 0.066 (if they lose), DC < -0.363 (if they lose); RCB = 0.219; KKR (current) -0.456

For KKR to win in this scenario, they have to beat the NRR of RCB to claim the 4th spot... The first 2 would be taken on the basis of points while the 3rd spot CSK would enjoy a NRR of > 0.270. So, KKR would require to make their NRR surpass 0.219 (of RCB). We have 2 cases.

Present NRR equation for KKR: (2009/255.3) - (2059/247.3) = -0.456

Case 1: KKR bats first and scores x runs (in 20 overs or gets all out before), Mumbai is restricted to y runs(in 20 overs or gets all out before)

New NRR value for KKR = ((2009+x)/275.3) - ((2059+y)/267.3) which needs to be > 0.219

Solving the inequality, (x-y) should be roughly > 160 runs (Whoa!!)

Case 2: MI bats first and scores x runs(in 20 overs or gets all out before); KKR scores (x+1) in y overs

New NRR value for KKR = ((2010+x)/(255.3+y)) - ((2059+y)/267.3) which needs to be > 0.219

This inequality is not solvable for positive values of x and y. This implies KKR cannot achieve the reqd. NRR.

Conclusion: KKR will need to bat first !!! Hope Ganguly realises this maths before he walks in for the toss!!! ;)