id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
a60c6cc9-22dc-496b-93cd-10ea4e0cba89
|
public double getTotalWeightItems()
{
double peso = 0D;
for(Item item : mochila){
peso += item.getWeight();
}
return peso;
}
|
beac1c07-ac9c-4ab0-8e5b-e8aca5b319b6
|
public void take(Command command){
if (!command.hasSecondWord()){
System.out.println("take what?");
return;
}
String id = command.getSecondWord();
Item item = currentRoom.getItem(id);
if(item != null)
{
if(item.canBeTaken()){
if(item.getWeight() + getTotalWeightItems() <= cargaMaxima) {
System.out.println("You add a new item to your bag");
mochila.add(item);
currentRoom.removeItem(id);
}
else {
System.out.println("No hay espacio para este objeto");
}
}else{
System.out.println("El Objeto no se puede coger");
}
}
else
{
System.out.println("You don't select a item");
}
}
|
98bd427d-195d-4421-8229-53c32d61b501
|
public void drop(Command command)
{
if (!command.hasSecondWord()){
System.out.println("drop what?");
return;
}
String id = command.getSecondWord();
int index = 0;
boolean searching = true;
while( searching && index < mochila.size()){
Item item = mochila.get(index);
if(item.getId().equals(id)){
currentRoom.addItem(item);
mochila.remove(index);
searching = false;
System.out.println("El objeto se ha dejado en la habitacion");
}
index++;
}
if (searching)
{
System.out.println("No estas llevando el objeto que has indicado");
}
}
|
032796c9-4c7f-441e-9222-9a92e3a27c94
|
public Command(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}
|
7e466ffe-df77-4470-b0a9-edfa34970949
|
public String getCommandWord()
{
return commandWord;
}
|
9573fb22-681e-411a-918e-93b5e3140688
|
public String getSecondWord()
{
return secondWord;
}
|
c68aa79c-ba64-427a-8e0b-4844f118efcc
|
public boolean isUnknown()
{
return (commandWord == null);
}
|
dee52cc5-46a2-47b3-ab8f-14e13625902f
|
public boolean hasSecondWord()
{
return (secondWord != null);
}
|
1570473d-71e9-4f11-baa5-683b04a6ea00
|
public static void main(String[] args) {
int m=0;
int p=0;
for(int i=999;i>m;i--)
for(int j=999;j>i;j--)
if(isp(j*i)) {
p=Math.max(i*j,p);
m=p/1000;
}
System.out.println(p);
}
|
6498d7f2-c123-419d-ac34-316620e4f925
|
private static boolean isp(int n) {
if(n%10!=n/100000)
return false;
if((n/10)%10!=(n/10000)%10)
return false;
if((n/100)%10!=(n/1000)%10)
return false;
return true;
}
|
224a4c7c-1ada-4add-a9e4-560306bef329
|
public static void main(String[] args) {
for(int i=0;i<300;i++)
for(int j=i;j<500;j++)
if(Math.sqrt(i*i+j*j)==1000-i-j) {
System.out.println(i+" "+j+" "+(1000-i-j)+" "+i*j*(1000-i-j));
System.exit(0);
}
}
|
3c8ed466-91ad-417e-aa68-3bf2bc508ca2
|
public static void main(String[] args) {
int[][] a= {{8,2,22,97,38,15,0,40,0,75,4,5,7,78,52,12,50,77,91,8},
{49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,4,56,62,0},
{81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,3,49,13,36,65},
{52,70,95,23,4,60,11,42,69,24,68,56,1,32,56,71,37,2,36,91},
{22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
{24,47,32,60,99,3,45,2,44,75,33,53,78,36,84,20,35,17,12,50},
{32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
{67,26,20,68,2,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21},
{24,55,58,5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
{21,36,23,9,75,0,76,44,20,45,35,14,0,61,33,97,34,31,33,95},
{78,17,53,28,22,75,31,67,15,94,3,80,4,62,16,14,9,53,56,92},
{16,39,5,42,96,35,31,47,55,58,88,24,0,17,54,24,36,29,85,57},
{86,56,0,48,35,71,89,7,5,44,44,37,44,60,21,58,51,54,17,58},
{19,80,81,68,5,94,47,69,28,73,92,13,86,52,17,77,4,89,55,40},
{4,52,8,83,97,35,99,16,7,97,57,32,16,26,26,79,33,27,98,66},
{88,36,68,87,57,62,20,72,3,46,33,67,46,55,12,32,63,93,53,69},
{4,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36},
{20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,4,36,16},
{20,73,35,29,78,31,90,1,74,31,49,71,48,86,81,16,23,57,5,54},
{1,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48}};
int m=0;
for(int i=0;i<16;i++)
for(int j=0;j<20;j++) {
int l=1;
int r=1;
for(int k=0;k<4;k++) {
l*=a[i+k][j];
r*=a[j][i+k];
}
m=Math.max(Math.max(l, m), r);
}
for(int i=0;i<16;i++)
for(int j=0;j<16;j++) {
int u=1;
int d=1;
for(int k=0;k<4;k++) {
d*=a[i+k][j+k];
u*=a[i-k+3][j+k];
}
m=Math.max(Math.max(u, m), d);
}
System.out.println(m);
}
|
c48c8e5a-ddd9-48af-a22e-202dc66ec904
|
public static void main(String[] args) {
String s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
int m=0;
for(int i=0;i<995;i++) {
int u=1;
for(int j=0;j<5;j++)
u*=Integer.parseInt(s.substring(i+j, i+j+1));
m=Math.max(m,u);
}
System.out.println(m);
}
|
70203ec6-00cb-47c9-a3ba-13e793a6c7f2
|
public static void main(String[] args) {
int n=28123;
boolean[] a=new boolean[n];
boolean[] b=new boolean[n];
for(int i=1;i<n;i++) {
int s=1;
int l=(int)Math.sqrt(i);
for(int j=2;j<=l;j++) {
int d=i/j;
if(d==i/(double)j) {
s+=j;
s+=(d==j?0:d);
}
}
a[i-1]=i<s;
}
// for(int i=0;i<n;i++)
// if(a[i])
// System.out.print(i+1+" ");
for(int i=0;i<n;i++)
b[i]=false;
for(int i=1;i<n;i++)
for(int j=1;j<=i;j++)
if(i+j<n && a[i-1] && a[j-1])
b[i+j]=true;
int s=0;
for(int i=0;i<n;i++)
if(!b[i])
s+=i;
System.out.println(s);
}
|
747c39d3-336e-4f4c-96e1-6ada9c7843ae
|
public static void main(String[] args) {
int n=20;
long[][] a=new long[n][];
for(int i=0;i<n;i++)
a[i]=new long[n];
for(int i=0;i<n;i++) {
a[i][0]=i+2;
a[0][i]=a[i][0];
}
for(int i=1;i<n;i++)
for(int j=1;j<=i;j++) {
a[i][j]=a[i-1][j]+a[i][j-1];
a[j][i]=a[i][j];
}
System.out.println(a[n-1][n-1]);
}
|
0e1f246a-9981-4d74-89cc-05b706e03918
|
public static void main(String[] args) {
int[] p= {2,3,5,7,11,13,17,19};
int[] n=new int[p.length];
for(int i=0;i<n.length;i++)
n[i]=0;
for(int i=2;i<20;i++)
for(int j=0;j<p.length;j++)
if(div(i,p[j])>n[j])
n[j]=div(i,p[j]);
int mul=1;
for(int i=0;i<p.length;i++)
for(int j=0;j<n[i];j++)
mul*=p[i];
System.out.println(mul);
}
|
003ab889-dee3-46ee-ae69-a684c651493f
|
private static int div(int num, int prime) {
int i=-1;
do {
num/=prime;
i++;
}while(num>0);
return i;
}
|
defea912-6872-4e41-960f-13bd49eef61e
|
public static void main(String[] args) {
int[][] a=new int[3][];
for(int i=0;i<3;i++)
a[i]=new int[6];
for(int i=0;i<3;i++)
for(int j=0;j<6;j++)
a[i][j]=0;
System.out.println(test(a,0)/6);
}
|
5c026650-4dd0-4715-84cb-617eb18882a4
|
private static int test(int[][] t, int i) {
// System.out.println(i);
int co=0;
for(int a=1;a<N;a++) {
t[i][0]=a;
for(int b=a;b<N;) {
t[i][1]=b;
for(int c=b;c<N;c++) {
t[i][2]=c;
for(int d=c;d<N;d++) {
t[i][3]=d;
for(int e=d;e<N;e++) {
t[i][4]=e;
for(int f=e;f<N;f++) {
t[i][5]=f;
if(i<2)
co+=test(t,i+1);
else
co+=turn(t);
}
}
}
}
}
}
return co;
}
|
5f35555a-8a13-40b9-b19a-10842c9d26b5
|
private static int turn(int[][] a) {
for(int i=0;i<3;i++)
if(chance(a[i],a[(i+2)%3])<=.5 && chance(a[i],a[(i+1)%3])<=.5)
return 0;
return 1;
}
|
261a752a-571d-478a-8cf9-e19fdbda581e
|
private static double chance(int[] a,int[] b) {
int wi=0;
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
if(a[i]>b[j])
wi++;
return wi/36.0;
}
|
91266a42-c992-4cf3-ae7a-22426af3b092
|
public static void main(String[] args) {
//TODO
long sum = 2;
long f=1;
long s=2;
long c;
do {
c=f+s;
if(c%2==0) {
sum+=c;
System.out.println(c);
}
f=s;
s=c;
// System.out.println(c);
}while(c<4000000);
// if(c%2==1 && c>4000000)
// sum-=c;
System.out.println(sum);
}
|
27120c60-de75-47bf-9429-495e50910b99
|
public static void main(String[] args) {
int[] a=new int[700];
for(int i=1;i<700;i++)
test(a,i);
int c=0;
for(int i=2;i<10000000;i++)
if(a[sqr(i)]==89)
c++;
System.out.println(c);
}
|
2c1222a3-1d81-4202-b1c4-6c3b44eff98a
|
private static int test(int[] a, int i) {
if(a[i]>0)
return a[i];
if(i==89) {
a[i]=89;
return 89;
}
if(i==1) {
a[i]=1;
return 1;
}
a[i]=test(a,sqr(i));
return a[i];
}
|
8b52f7ea-501f-42e3-a8d5-6622de668851
|
private static int sqr(int i) {
int s=0;
do {
s+=(i%10)*(i%10);
i/=10;
}while(i!=0);
return s;
}
|
126538bc-8ddd-4fd8-bae2-58ba1686323a
|
public static void main(String[] args) {
long num=600851475143L;
long max = (long) Math.sqrt(num);
if(max%2==0)
max--;
for(;max>0;max-=2)
if(num%max==0) {
boolean p = true;
long s=(long) Math.sqrt(max);
if(s%2==0)
s--;
for(;s>2;s-=2)
if(max%s==0) {
p=false;
break;
}
if(p) {
System.out.println(max);
System.exit(0);
}
}
}
|
b0d5bd25-8547-4155-ad34-2cf580da46af
|
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = in.nextInt();
int sum=0;
for(int i=1;i<max;i++)
if(i%3==0 || i%5==0)
sum+=i;
System.out.println(sum);
}
|
cd12213b-502f-41fa-9974-23e093b36d87
|
public static void main(String[] args) {
int m=1000000;
int[] n=new int[m];
for(int i=0;i<m;i++)
n[i]=0;
int t=1;
for(int i=2;i<m;i++)
if(test(n,i)>n[t])
t=i;
System.out.println(t);
}
|
1c26ae89-7ba9-4e44-ad3f-218f00a48f0f
|
private static int test(int[] n, long i) {
if(i==1)
return 0;
if(i>=1000000) {
if(i%2==0)
return test(n,i/2)+1;
return test(n,3*i+1)+1;
}
if(n[(int) i]>0)
return n[(int) i];
if(i%2==0) {
n[(int) i]=test(n,i/2)+1;
return n[(int) i];
}
else {
n[(int) i]=test(n,3*i+1)+1;
return n[(int) i];
}
}
|
3656ac85-a4ae-44d7-8c52-74f5303e4ff6
|
public CepFacil(String zipCode, String apiKey) throws IOException, InvalidZipCodeException, AddressNotFoundException {
this.zipCode = zipCode;
String line, result = "";
try {
URL apiUrl = new URL("http://www.cepfacil.com.br/service/?filiacao=" + apiKey + "&cep=" +
CepFacil.parseZipCode(zipCode) + "&formato=texto");
BufferedReader in = new BufferedReader(new InputStreamReader(apiUrl.openStream()));
/*
* Reads line by line of the response text and attaches it to the
* result String, to be parsed later in this constructor method
*/
while ((line = in.readLine()) != null) {
result += line;
}
in.close();
this.status = result.split("=")[1].split("&")[0]; // status
/* Set all attributes as null if either the informed zipCode
* or apiKey is wrong
*/
if (this.status.equals("nao-encontrado")) {
throw new AddressNotFoundException();
} else {
/*
* Breaks the response text into pieces and set each piece as the right
* object attribute
*/
this.status = result.split("=")[1].split("&")[0]; // status
this.addressType = result.split("=")[3].split("&")[0]; // type of the way
this.state = result.split("=")[4].split("&")[0]; // state
this.city = result.split("=")[5].split("&")[0]; // city
this.neighborhood = result.split("=")[6].split("&")[0]; // neighborhood
this.street = result.split("=")[7].split("&")[0]; // name of the way
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
|
9c6662d1-6359-402b-a137-9babe7f18bd8
|
public static String parseZipCode(String zipCode) throws InvalidZipCodeException {
/*
* This method parses the user informed zip code into a String object that contains
* no dashes so the webservice can find it. So, for example, 55555-555 becomes 55555555.
* @param zipCode the zipCode from which you want to extract an address
* @return the zip code String without the dash
*/
String parsedZipCode = zipCode.replaceAll("[^0-9]+", "");
if (parsedZipCode.length() != 8) {
throw new InvalidZipCodeException(zipCode);
}
return parsedZipCode;
}
|
c1393d64-7259-471e-8b13-7861b154e767
|
public boolean isValid() {
/*
* @return true if the informed zip code corresponds to an actual address. Returns false otherwise.
*/
return this.status.equals("encontrado");
}
|
87cd6421-a3dc-42ac-a0a0-7460a36ae8a9
|
public String fullAddress() {
/*
* Returns the String object for a full description of the address, containing
* exactly the address type (street/avenue/lane), street name, neighborhood, city
* zip code, state and country name (Brazil only, of course).
*/
return this.addressType + " " + this.street + ", " + this.neighborhood + ", " + this.city + " "
+ this.zipCode + " - " + this.state + ", Brasil";
}
|
c6d15173-e21e-4cf7-b987-0ec9568f4888
|
@Override
public String toString() {
return this.addressType + " " + this.street + ", " + this.city + " " + this.zipCode + " - " +
this.state + ", Brasil";
}
|
cb0490c7-39ad-4f38-ba90-6098e6852bdb
|
public String getAPiKey() {
return this.apiKey;
}
|
9c998e1f-7058-47e2-a6c4-64dbe643effc
|
public String getZipCode() {
return this.zipCode;
}
|
05e9abb7-16af-4424-9375-74e87655636f
|
public String getState() {
return this.state;
}
|
11e30df9-ac3f-4a22-86a4-377ea4087385
|
public String getAddressType() {
return this.addressType;
}
|
4c3c3e28-a441-421d-a73c-f45314cc99ec
|
public String getCity() {
return this.city;
}
|
7ed89984-bcdd-47cd-a5bc-805662a4c50e
|
public String getNeighborhood() {
return this.neighborhood;
}
|
914f4b2c-b718-42ab-bb4d-fae88bc54c01
|
public String getStreet() {
return this.street;
}
|
dd7a123c-a66e-490b-8955-bd3a93673837
|
public String getStatus() {
return this.status;
}
|
9853f6b4-1fa4-4e22-93f4-244b57069cb1
|
public AddressNotFoundException() {
super("You either informed an invalid API token nonexistent zip code");
}
|
db8933f8-4fb4-497d-b6e4-26fee04b81cc
|
public InvalidZipCodeException(String zipCode) {
super("The informed zip code is invalid: " + zipCode + ".\n" +
"A valid Brazilian zip code contains 8 numeric characters\n");
}
|
f65c2122-a08b-41bc-a0cd-21721f4713ec
|
public static void main(String[] args) throws IOException, InvalidZipCodeException, AddressNotFoundException {
// zipCode, state, addressType, city, neighborhood, street, status
String token = "0E2ACA03-FC7F-4E87-9046-A8C46637BA9D";
CepFacil ruaCasa = new CepFacil("53416-540", token);
CepFacil ufpe;
try {
ufpe = new CepFacil("53416-540", token);
} catch (AddressNotFoundException e) {
e.printStackTrace();
}
System.out.println(ruaCasa.getZipCode());
System.out.println(ruaCasa.getStreet());
System.out.println(ruaCasa.isValid());
System.out.println(ruaCasa);
CepFacil masp = new CepFacil("01310-200", token);
System.out.println(masp); // AVENIDA PAULISTA, SÃO PAULO 01310-200 - SP, Brasil
// Throws AddressNotFoundException because there is no such address
// correspondent to the 00000-000 zip code in Brazil
try {
CepFacil infinitopolis = new CepFacil("00000-000", token);
} catch (AddressNotFoundException e) {
System.out.println("oops. Fail");
e.printStackTrace();
}
}
|
ed798a0a-f847-4f15-9d67-8971ffd8e670
|
@Test
public void testZipCodeFormat() throws InvalidZipCodeException {
String zipCode = "53416-540";
assertEquals("53416540", CepFacil.parseZipCode(zipCode));
}
|
8f6847f9-af42-4b44-9cc6-dca89f5791aa
|
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomGraphGenerator graphGenerator = new RandomGraphGenerator();
//generate graph with 4 vertex and 4 edge
RandomGraph graph = graphGenerator.generate(6, 6);
MCSTGenerator mcstGenerator = new MCSTGenerator(graph);
mcstGenerator.prim();
// mcstGenerator.checkCondition();
}
|
a0fd4df7-2d90-44c1-a4d6-b92f54f0019e
|
public Vertex (int name){
this.name = name;
}
|
7b53029f-8089-4bd7-8214-36101173d6e8
|
public Vertex(){
}
|
c47aad42-4815-4e2f-8d36-961a5ed46dac
|
public MCSTGenerator(RandomGraph graph){
this.graph = graph;
graph.printGraph();
}
|
df81876d-4aab-4b9d-8aeb-6905c12b8247
|
public void prim(){
this.vertexMap = new HashMap<Integer, Vertex>();
priorityQueue = new PriorityQueue<Vertex>(this.graph.vertexList.size(),orderComparator);
for (Vertex vertex : this.graph.vertexList) {
vertex.key = 100;
vertex.parent = null;
this.vertexMap.put(vertex.name, vertex);
// this.priorityQueue.offer(vertex);
}
this.graph.vertexList.get(0).key = 0;
priorityQueue.addAll(this.graph.vertexList);
System.out.println("------------------------------------------------------------------");
System.out.println("\nThe minimum cost spanning tree is \n");
while (priorityQueue.size()>0) {
Vertex u = priorityQueue.poll();
this.vertexOrder.add(u);
//print the minimum spanning tree and check the condition
if (u.parent != null) {
//-------------------------test----------------------
Vertex seconde = priorityQueue.peek();
//-------------------------test----------------------
int conditionCheck;
//to process the last vertex
if (seconde == null) {
PriorityQueue<Integer> lastPriorityQueue = new PriorityQueue<Integer>();
for (Vertex v : this.adjVertex(u)){
// PriorityQueue<Integer> lastPriorityQueue = new PriorityQueue<Integer>();
lastPriorityQueue.offer(this.graph.adjMatrix[v.name][u.name]);
}
if (lastPriorityQueue.size() == 1) {
conditionCheck = lastPriorityQueue.poll();
}else {
lastPriorityQueue.poll();
conditionCheck = lastPriorityQueue.poll();
}
System.out.print(u.parent.name + " -- >"+u.name + " Weight:"+u.key+" and small than "+ conditionCheck+" will not change the MSCT"+ "\n");
}else {
System.out.print(u.parent.name + " -- >"+u.name + " Weight:"+u.key+" and small than "+ seconde.key+" will not change the MSCT"+ "\n");
}
}
//update the priority queue dynamically
for (Vertex v : this.adjVertex(u)) {
if (priorityQueue.contains(v) && this.graph.adjMatrix[u.name][v.name] < v.key) {
v.parent = u;
v.key = this.graph.adjMatrix[u.name][v.name];
// System.out.println(v.key);
//update the priority queue dynamically
Vertex vertex = priorityQueue.poll();
priorityQueue.add(vertex);
//-------------------------------------test-----------------------------
// for (int i = 0; i < this.priorityQueue.size(); i++) {
// if (v.key != 100 && v.key != 0) {
//
// }
// }
}
}
}
}
|
8f5416cb-94b2-4d7b-b5e5-ac6cd2749401
|
private List<Vertex> adjVertex(Vertex v){
int i = v.name;
List<Vertex> list = new ArrayList<Vertex>();
for (int j = 0; j < this.graph.vertexList.size(); j++) {
if (this.graph.adjMatrix[i][j]>0) {
// System.out.println(j);
Vertex u = vertexMap.get(j);
list.add(u);
}
}
return list;
}
|
50769a7a-1697-45d7-903f-36ef22cad85b
|
public int compare(Vertex char1 , Vertex char2){
int char1key = char1.key;
int char2key = char2.key;
if (char1key > char2key) {
return 1;
}else if (char1key < char2key) {
return -1;
}else {
return 0;
}
}
|
e84fa11c-7119-402c-9010-14a2391db14d
|
public RandomGraph(){
}
|
94f78739-6918-4118-bb2f-315baa7fdb74
|
public RandomGraph(List<Vertex>vertexList){
this.vertexList = vertexList;
adjMatrix = new int[this.vertexList.size()][this.vertexList.size()];
for (int i = 0; i < vertexList.size(); i++) {
for (int j = 0; j < vertexList.size(); j++) {
this.adjMatrix[i][j] = 0;
}
}
}
|
2c2d55c5-c502-449f-9c21-923e97efa8a1
|
public boolean insertEdge(int i,int j,int w){
if (i != j && this.adjMatrix[i][j] == 0&& this.adjMatrix[j][i] == 0) {
this.adjMatrix[i][j] = w;
this.adjMatrix[j][i] = w;
return true;
}else {
return false;
}
}
|
82857e09-691d-4b01-8bea-fe91c4206f9c
|
public void printGraph(){
for (int i = 0; i < vertexList.size(); i++) {
for (int j = 0; j < vertexList.size(); j++) {
System.out.print(this.adjMatrix[i][j]+" ");
}
System.out.println();
}
}
|
8562c4ea-8c68-458f-8e5f-d7ac59e60e88
|
public RandomGraphGenerator(){
}
|
b025c188-d63c-4192-bdec-70634eed961f
|
public RandomGraph generate(int numOfVertex,int numOfEdge){
if (numOfEdge < numOfVertex-1 || numOfEdge> (numOfVertex*(numOfVertex-1))/2) {
System.out.println("The number of edge cannot construct a connected graph ");
return null;
}
for (int i = 0; i < numOfVertex; i++) {
Vertex vertex = new Vertex(i);
// System.out.println(vertex.name);
this.vertexList.add(vertex);
}
randomGraph = new RandomGraph(this.vertexList);
//Generate a random graph with uniform distribution of edges
int count = 0;
for (int i = 0; i < 100000; i++) {
if (count == numOfEdge) {
break;
}
if (randomGraph.insertEdge((int)(Math.random()*numOfVertex), (int)(Math.random()*numOfVertex), (int)(Math.random()*10)+1)) {
randomGraph.insertEdge((int)(Math.random()*numOfVertex), (int)(Math.random()*numOfVertex), (int)(Math.random()*10)+1);
count++;
}
}
return this.randomGraph;
// randomGraph.printGraph();
}
|
8cf0fc2d-6e40-4e27-9459-cf22af7933a4
|
public Map<Character, String> getNodeTable() {
return nodeTable;
}
|
ad7b5b48-61eb-4b4b-8cd3-a46f2623a3ec
|
public void setNodeTable(Map<Character, String> nodeTable) {
this.nodeTable = nodeTable;
}
|
89bbeed0-9688-451e-88e0-37f94b497142
|
public void put(Character character,String string){
this.nodeTable.put(character, string);
}
|
5b89354b-e6b1-4ced-996c-38326aea6622
|
public void binaryDigit(String binary) {
this.binary += binary;
}
|
2e61f5cf-c1c1-4c3f-8230-83baba0b5ebf
|
public Node getRootNode() {
return rootNode;
}
|
6e4ef2ba-c300-495a-a419-744926f0bd37
|
public void setRootNode(Node rootNode) {
this.rootNode = rootNode;
}
|
01ff13da-1949-41e2-bd31-49c6248b2a22
|
public String getOutputString() {
return outputString;
}
|
1691d839-35e9-4458-b1ac-e2cac84348e4
|
public void setOutputString(String outputString) {
this.outputString = outputString;
}
|
93a66aa7-7ba1-4b30-aa0e-032c61a0ff3a
|
public String getInputString() {
return inputString;
}
|
04571b3c-276e-4b74-b8c0-b831cfc1e31d
|
public void setInputString(String inputString) {
this.inputString = inputString;
}
|
cacfe94a-eb05-40b5-ab93-39eb1a009a7a
|
public HuffmanEncode(String string){
//replace space
// String string = inputString.replaceAll(" ", "");
// System.out.println(string);
this.inputString = string;
HashMap<Character, Integer> strhashMap = new HashMap<Character, Integer>();
int num = 0;
int count = 0;
//pick up a char to compute its frequency
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
int temp = 0;
for (int j = 0; j < string.length(); j++) {
num = string.indexOf(c,temp);
if (num!=-1) {
count++;
temp = num +1;
continue;
}else {
strhashMap.put(c,count);
count = 0;
break;
}
}
}
Iterator<Character> iteratorKey = strhashMap.keySet().iterator();
Iterator<Integer> iteratorValue = strhashMap.values().iterator();
//initliaze the priorityQueue
// priorityQueue = new PriorityQueue<Node>(strhashMap.size(),orderComparator);
//---------------------------test-------------------------------
priorityQueue = new huffmancode.PriorityQueue<Node>();
//---------------------------test-------------------------------
while (iteratorKey.hasNext()) {
Node charOfInput = new Node();
charOfInput.name = iteratorKey.next();
charOfInput.frequency = iteratorValue.next();
priorityQueue.offer(charOfInput);
}
// while (priorityQueue.size()>0) {
// System.out.printf("%c",priorityQueue.peek().name);
// priorityQueue.poll();
// }
// System.out.println(priorityQueue.size());
// Encoding(priorityQueue);
}
|
fb32f445-8d1b-43f2-86f9-7f0e4def59c5
|
public void Encoding(){
System.out.println("size is "+priorityQueue.size());
//construct the Huffman tree
while (priorityQueue.size()>1) {
Node node = new Node();
Node x = new Node();
Node y = new Node();
node.left = x = priorityQueue.poll();
node.right = y = priorityQueue.poll();
node.frequency = x.frequency+y.frequency;
priorityQueue.offer(node);
}
//process the Huffman tree
HuffmanTree huffmanTree = new HuffmanTree();
rootNode = priorityQueue.poll();
huffmanTree.setRoot(rootNode);
huffmanTree.setBinaryTreeArray(rootNode);
huffmanTree.printTree(rootNode);
huffmanTree.write();
//to output the encoding string
mapTable = huffmanTree.getNodeTable().getNodeTable();
for (int i = 0; i < this.inputString.length(); i++) {
char c = inputString.charAt(i);
String binaryString = mapTable.get(c);
this.outputString += binaryString;
}
System.out.println("This input encode string is :");
System.out.println(this.outputString);
}
|
7ef054f5-9ff4-4770-b2c1-77362c0f9741
|
public PriorityQueue(){
this.list = new ArrayList<Node>();
//we do not want to use the first place
Node placeHolder = new Node();
//---------------------------test-------------------------------
// Node node1 = new Node();
// node1.frequency =1;
// node1.name = 'a';
// Node node2 = new Node();
// node2.frequency = 3;
// node2.name = 'c';
// Node node3 = new Node();
// node3.frequency = 2;
// node3.name = 'b';
//---------------------------test-------------------------------
this.list.add(placeHolder);
//---------------------------test-------------------------------
// this.list.add(node1);
// this.list.add(node2);
// this.list.add(node3);
//---------------------------test-------------------------------
}
|
753e1968-6421-4a09-8233-ea56234d943b
|
private void buildMinHeap(List<Node> list){
// this.heapSize = this.list.size();
for (int i = (int)(Math.floor(list.size()/2)); i>=1; i--) {
this.minHeapify(list, i);
}
}
|
477a6f61-790d-48b8-9327-c8ef02fce206
|
private void minHeapify(List<Node> list,int i){
int smallest = 0;
int l = left(i);
int r = right(i);
//find the minimum element
if (l<= this.list.size()-1 && this.list.get(l).frequency<this.list.get(i).frequency) {
smallest = l;
}else {
smallest = i;
}
if (r<=this.list.size()-1 && this.list.get(r).frequency<this.list.get(smallest).frequency) {
smallest = r;
}
//exchange the i and smallest
if (smallest != i) {
Node temp = new Node();
temp = this.list.get(i);
this.list.set(i, this.list.get(smallest));
this.list.set(smallest, temp);
this.minHeapify(list, smallest);
}
}
|
2bace030-f581-4730-b339-814f9af5b82b
|
private int left(int i){
return 2*i;
}
|
a8d9f77b-230b-4a0b-845c-8f5920c2bd1b
|
private int right(int i){
return 2*i+1;
}
|
6c940a5c-2a5c-4cd0-ba9a-1fe0c3690cc3
|
public Node poll(){
Node minNode = new Node();
if (list.size()<=1) {
System.out.println("The priority is empty!");
return minNode;
}
minNode = this.list.get(1);
this.list.set(1, this.list.get(this.list.size()-1));
this.list.remove(this.list.size()-1);
this.minHeapify(this.list, 1);
return minNode;
}
|
960247e2-60aa-497f-b936-10bcbb2d4c4b
|
public boolean offer(Node node){
this.list.add(node);
this.buildMinHeap(this.list);
return true;
}
|
459a5dc2-f468-42f0-b105-df391e317b4a
|
public int size(){
return this.list.size()-1;
}
|
04b24cab-86cc-448c-af4f-be881ae480fe
|
public NodeTable getNodeTable() {
return nodeTable;
}
|
201a2a96-51b3-4db7-9d3b-b745f3935d81
|
public void setNodeTable(NodeTable nodeTable) {
this.nodeTable = nodeTable;
}
|
e256656c-1cc3-4a2f-8624-1855a981bc53
|
public Node getRoot() {
return root;
}
|
c64c0789-058c-4e1c-b9b0-403ea9e18766
|
public void setRoot(Node root) {
this.root = root;
}
|
4c5cbdc3-f8e7-47f8-9cb5-576c1443ce27
|
public void printTree(Node node){
if (node.left != null && node.right != null) {
printTree(node.left);
printTree(node.right);
}else {
nodeTable.put(node.name, node.binary);
output += node.name +" = "+ node.binary + " , ";
}
}
|
a25579cc-bcb4-46da-b1b3-ceec400f0b32
|
public void setBinaryTreeArray(Node node){
if (node.left != null) {
Node nodeLeft = node.left;
nodeLeft.binaryDigit(node.binary+"0");
setBinaryTreeArray(nodeLeft);
}
if (node.right != null) {
Node nodeRight = node.right;
nodeRight.binaryDigit(node.binary+"1");
setBinaryTreeArray(nodeRight);
}
}
|
6ac8255d-342e-4e75-8340-36b728309b28
|
public void write(){
System.out.println("The Huffman tree is ");
System.out.println(output);
}
|
42f9bfcd-dda4-4325-885f-458a80406789
|
public static void main(String[] args){
String content = null;
try {
//--------------------test----------------------------
File huffmanFile = new File("C:\\Users\\Administrator\\temp\\Huffman.txt");
String encodString = "GBK";
if (huffmanFile.isFile()&&huffmanFile.exists()) {
InputStreamReader reader = new InputStreamReader(new FileInputStream(huffmanFile),encodString);
BufferedReader bufferedReader = new BufferedReader(reader);
while ((content = bufferedReader.readLine())!=null) {
System.out.println(content);
//encode
HuffmanEncode huffmanEncode = new HuffmanEncode(content);
huffmanEncode.Encoding();
//decode
HuffmanDecode huffmanDecode = new HuffmanDecode(huffmanEncode.getOutputString(),huffmanEncode.getRootNode());
huffmanDecode.decoding();
}
}
//--------------------test----------------------------
// System.out.println("please input here: ");
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// String string = bufferedReader.readLine();
//
// //encode
// HuffmanEncode huffmanEncode = new HuffmanEncode(string);
// huffmanEncode.Encoding();
//
// //decode
// HuffmanDecode huffmanDecode = new HuffmanDecode(huffmanEncode.getOutputString(),huffmanEncode.getRootNode());
// huffmanDecode.decoding();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
|
8f1c6d0b-50f0-4172-a838-b3b0cca121c3
|
public String getHuffmanString() {
return huffmanString;
}
|
2ee7d36f-2641-4575-bcde-e82123cb081a
|
public void setHuffmanString(String huffmanString) {
this.huffmanString = huffmanString;
}
|
e3acb2bb-5579-4c49-865d-55ecf6dbb09d
|
public HuffmanDecode(String string,Node root){
this.huffmanString = string;
this.root = root;
}
|
84aa1e9c-6b1d-408f-b7a6-e092470e5655
|
public void decoding(){
Node currentNode = this.root;
for (int i = 0; i < this.huffmanString.length(); i++) {
char c = this.huffmanString.charAt(i);
//find the leaf
if (c == '0') {
currentNode = currentNode.left;
}else {
currentNode = currentNode.right;
}
//decoding if currentNode is not leaf we will continue loop
if(currentNode.left != null && currentNode.right != null) {
continue;
}else {
this.originalString += currentNode.name;
currentNode = this.root;
}
}
this.printOriginalString();
}
|
4a5f9cf2-88da-4565-ab26-db484a3d53a2
|
private void printOriginalString(){
System.out.println("This original string is :");
System.out.println(this.originalString);
}
|
bfb6924e-a892-427a-9b17-f8e33e9b97e6
|
public MyFormat() {
super("Adjust your settings");
panel.setLayout(new MigLayout());
panel.add(bridgeSet = new JLabel("Bridge settings:"), "wrap");
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(numberBars, "span, grow");
panel.add(numOfBars = new JLabel("Number of bars:"));
panel.add(numberBars, "span, grow");
panel.add(deckElev = new JLabel("Deck elevation: "));
panel.add(deckElevation);
panel.add(meters = new JLabel("meters"), "wrap");
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(standAbut, "span, grow");
panel.add(archAbut, "span, grow");
panel.add(heightOfArch = new JLabel("Height of arch: "));
panel.add(archHeight);
panel.add(meters = new JLabel("meters"), "wrap");
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(noPier, "span, grow");
panel.add(pier, "span, grow");
panel.add(heightOfPier = new JLabel("Height of pier: "));
panel.add(pierHeight);
panel.add(meters = new JLabel("meters"), "wrap");
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(cabelAnch = new JLabel("Cable anchorage:"),"wrap");
panel.add(noCabAnch,"span, grow");
panel.add(oneCabAnch,"span, grow");
panel.add(twoCabAnch,"span, grow");
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(concreteType = new JLabel("Concrete type: "),"wrap");
panel.add(medStrCon,"span, grow");
panel.add(highStrCon,"span, grow");
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(continueBtn);
panel.add(quitBtn);
add(panel);
}
|
d2d2781f-9145-4c8b-9094-6238e8ed0244
|
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MyFormat mf = new MyFormat();
mf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mf.pack();
mf.setLocationByPlatform(true);
mf.setVisible(true);
}
});
}
|
cb203a69-ac4b-4ff9-8c0a-3d9a9503bd11
|
public void run() {
MyFormat mf = new MyFormat();
mf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mf.pack();
mf.setLocationByPlatform(true);
mf.setVisible(true);
}
|
20fe9b0c-2210-4048-94fb-bc683d165770
|
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("*** damn ***");
for(int i=0; i<5; i++){
System.out.println("*** print this value: " + i + " ***");
}
try{
}
catch(Exception e){
System.err.println("");
}
error1();
System.out.println("*** test 1 ***");
error2();
System.out.println("*** test 2 ***");
error3();
}
|
647b1f1f-a45e-4226-bfd6-f30d89b0d87d
|
public static void error1(){
System.err.println("error 1");
}
|
50fb5cdb-0211-4065-b669-e5262cfe8090
|
public static void error2(){
System.err.println("error 2");
}
|
ab399649-d6c6-4d8a-90d4-629e436eafa0
|
public static void error3(){
System.err.println("error 3");
}
|
aa55b0b1-bc56-4ceb-bc9d-c58fb8b9026f
|
public static void main(String[] args) {
System.out.println("*** sending values from: Param001");
Param002.receive(Param002.createNewIssueCase);
Param002.receive(Param002.createLink);
Param002.receive(Param002.addressNewLink);
System.out.println("*** after sending printing all values ***");
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.