Skip to content

Commit 264b213

Browse files
committed
Add Lexico Problem
1 parent 4ca5749 commit 264b213

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codefortomorrow.advanced.chapter14.practice;
2+
3+
import java.io.*;
4+
5+
/*
6+
Practice: Use File I/O to compare two files lexicographically.
7+
Lexicographical order is very similar to alphabetical order,
8+
except that it includes more than just lowercase alphabets.
9+
Given two text files f1 and f2, write a program that will
10+
compare their contents and output to the console the
11+
title of the file which comes first lexicographically.
12+
13+
Hint: Java’s String.compareTo method may be of use to you.
14+
*/
15+
16+
public class Lexico{
17+
18+
public static void main(String[] args){
19+
// Add code here
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And then the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And than the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions;
2+
3+
import java.io.*;
4+
5+
/*
6+
Practice: Use File I/O to compare two files lexicographically.
7+
Lexicographical order is very similar to alphabetical order,
8+
except that it includes more than just lowercase alphabets.
9+
Given two text files f1 and f2, write a program that will
10+
compare their contents and output to the console the
11+
title of the file which comes first lexicographically.
12+
13+
Hint: Java’s String.compareTo method may be of use to you.
14+
*/
15+
16+
public class Lexico{
17+
18+
public static void main(String[] args){
19+
BufferedReader br = null;
20+
BufferedReader br2 = null;
21+
try{
22+
br = new BufferedReader(new FileReader("f1.txt"));
23+
br2 = new BufferedReader(new FileReader("f2.txt"));
24+
25+
String line1 = br.readLine();
26+
String line2 = br2.readLine();
27+
int c = line1.compareTo(line2);
28+
while (c == 0){
29+
line1 = br.readLine();
30+
line2 = br2.readLine();
31+
c = line1.compareTo(line2);
32+
}
33+
if(c > 0)
34+
System.out.println("File 2");
35+
else
36+
System.out.println("File 1");
37+
} catch(IOException e){
38+
e.printStackTrace();
39+
}
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And then the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And than the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.

0 commit comments

Comments
 (0)