Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:- Divide the problem into some subproblem. Conquer:- Conquer the subproblem by calling recursively until subproblem solved. Combine:- (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution. When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case. Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and ...
Extracting all states data like population,economy from a wikipedia link of India using Java Language
package urldatafetch;
/**
*
* @author Rishabh
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class UrlDataFetch {
public static void main(String[] args) throws IOException {
org.jsoup.nodes.Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/India").get();
org.jsoup.select.Elements tables =doc.select("table");
int i=1;
String state[]=new String[29];
ArrayList<String> ar=new ArrayList<>();
for(org.jsoup.nodes.Element tableo :tables)
{ if(i==1 ||i==2 ||i==4){i++;}
else if(i==3)
{ org.jsoup.select.Elements rows=tableo.select("tr");
for(org.jsoup.nodes.Element row :rows)
{
org.jsoup.select.Elements columns = row.select("td");
for (org.jsoup.nodes.Element column:columns)
{
if(column.text().substring(0, 2).matches("[A-Z][.]+"))
{}
else if(column.text().substring(0,2).matches("[0-9][.]+")){
// System.out.println(column.text().substring(3));
ar.add(column.text().substring(3));}
else if(column.text().substring(0,3).matches("[0-9][0-9][.]+")){
// System.out.println(column.text().substring(4));
ar.add(column.text().substring(4));}
}
}
// System.out.println("");
i++;
}}
Iterator it=ar.iterator();
int m=0;
while(it.hasNext())
{
state[m]=(String)it.next();
m++;
} int j=1;
int k=1;
int h=1;
for(int s=0;s<=(state.length)-1;s++)
{ j=1;
System.out.println(state[s]);
System.out.println("");
org.jsoup.nodes.Document doc1 = Jsoup.connect("https://en.wikipedia.org/wiki/"+state[s]).get();
org.jsoup.select.Elements tablestate =doc1.select("table");
for(org.jsoup.nodes.Element tablem :tablestate)
{ if(j==1){
org.jsoup.select.Elements rowstate=tablem.select("tr");
for(org.jsoup.nodes.Element rowm:rowstate)
{
if(rowm.text().contains("Literacy")||rowm.text().contains("Density")||rowm.text().contains("Area rank")||rowm.text().contains("Rank")||(rowm.text().contains("Total")))
{
org.jsoup.select.Elements rowheadstate=rowm.select("th");
for(org.jsoup.nodes.Element header:rowheadstate)
{
System.out.println(header.text());}
// org.jsoup.select.Elements rowinner=rowm.select("tr");
// for(org.jsoup.nodes.Element header1:rowinner)
// {
// System.out.println(header1.text());}
org.jsoup.select.Elements columnstate=rowm.select("td");
for(org.jsoup.nodes.Element columnm:columnstate)
{
System.out.println(columnm.text());}
}
j++;} }
}
}
}
}
Comments
Post a Comment