Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dailyprogrammer;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class E277 {
- public static void main(String[] args){
- String[] inputs = {
- "4 8",
- "1536 78360",
- "51478 5536",
- "46410 119340",
- "7673 4729",
- "4096 1024",
- "aaaa aaaa",
- "ab cd",
- "daily programmer",
- "3 x cb y ab z xa ab cb ab x x y z y z xay"
- };
- for(int i=0; i<inputs.length; i++){
- System.out.println(simplify(inputs[i]));
- }
- }
- public static String simplify(String input){
- String[] i = input.split(" ");
- if(i.length != 2){
- if(!isInteger(i[0])){
- System.out.println(input);
- System.out.println("Invalid input type!");
- System.exit(0);
- }
- //Assume substitution case
- //System.out.println("Substitutions required for input, parsing...");
- Map subs = new HashMap();
- ArrayList<String> newfracs = new ArrayList<String>();
- ArrayList<Character> subbedvars = new ArrayList<Character>();
- for(int e=1; e<i.length; e+=2){
- if(e<(1+Integer.parseInt(i[0])*2)){
- subbedvars.add(i[e].charAt(0));
- subs.put(i[e].charAt(0), i[e+1]);
- //System.out.println("Variable substitution registered: " + i[e] + " -> " + subs.get(i[e].charAt(0)));
- }
- }
- //The variable subsitution may themselves include substitutions which must be carried out
- //System.out.println("Performing substitutions within substitutions...");
- ArrayList<String[]> newsubs = new ArrayList<String[]>();
- for(int j=0; j<subs.size(); j++){
- newsubs.add(new String[] {""+subbedvars.get(j), ""+subs.get(subbedvars.get(j))});
- }
- //Iterate through the set of maps until no changes occur in an iteration
- boolean changemade = true;
- while(changemade){
- changemade = false;
- for(int j=0; j<newsubs.size(); j++){
- String subbed = "";
- for(int k=0; k<newsubs.get(j)[1].length(); k++){
- if(subs.containsKey(newsubs.get(j)[1].charAt(k))){
- subbed+=subs.get(newsubs.get(j)[1].charAt(k));
- changemade = true;
- }
- else{
- subbed+=newsubs.get(j)[1].charAt(k);
- }
- }
- newsubs.get(j)[1] = subbed;
- }
- }
- //Create a new map with the changes made above
- Map subs2 = new HashMap();
- for(int j=0; j<newsubs.size(); j++){
- subs2.put(newsubs.get(j)[0], newsubs.get(j)[1]);
- }
- //Apply the map to the fractions
- for(int e=1; e<i.length; e+=2){
- if(e<(1+Integer.parseInt(i[0])*2)){
- //Do nothing
- }
- else{
- String f = i[e] + " " + i[e+1];
- String f2 = "";
- for(int j=0; j<f.length(); j++){
- if(subs2.containsKey(""+f.charAt(j))){
- f2+=subs2.get(""+f.charAt(j));
- }
- else{
- f2+=f.charAt(j);
- }
- }
- newfracs.add(f2);
- }
- }
- for(int j=0; j<newfracs.size(); j++){
- //This output would display the input with substituted values
- //We don't want this, so take the end of the line and stick the original input
- //back on the front
- String simp = i[2+subs.size()*2 + 2*j-1] + "/" + i[2+subs.size()*2 + 2*j] + " --> " + simplify(newfracs.get(j)).split(" ")[4];
- System.out.println(simp);
- }
- }
- if(i.length != 2){
- return "";
- }
- if(isInteger(i[0]) && isInteger(i[1])){
- //Integer fraction, calculate GCD and divide through
- int a = Integer.parseInt(i[0]);
- int b = Integer.parseInt(i[1]);
- //Find the gcd of the a,b
- int g = gcd(a,b);
- b/=g;
- a/=g;
- if(b == 1){
- return i[0]+"/"+i[1]+" --> "+a;
- }
- return i[0]+"/"+i[1]+" --> "+a+"/"+b;
- }
- else if(!isInteger(i[0]) && !isInteger(i[1])){
- //Algebraic fraction (i.e. non-numeric), count the
- //number of similar letters and delete as necessary
- String a = i[0];
- String b = i[1];
- for(int j=0; j<a.length(); j++){
- if(b.contains(""+a.charAt(j))){
- b = b.substring(0, b.indexOf(a.charAt(j))) + b.substring(b.indexOf(a.charAt(j))+1,b.length());
- a = a.substring(0, j) + a.substring(j+1,a.length());
- j=-1;
- }
- }
- if(a.length() == 0 && b.length() == 0){
- //Numerator and denominator blank, 1/1
- return i[0]+"/"+i[1]+" --> "+"1";
- }
- else if(a.length() == 0){
- //Numerator blank, 1/b
- return i[0]+"/"+i[1]+" --> "+"1/"+b;
- }
- else if(b.length() == 0){
- //Denominator blank, dividing by 1, a
- return i[0]+"/"+i[1]+" --> "+a;
- }
- else{
- //Standard output
- return i[0]+"/"+i[1]+" --> "+a+"/"+b;
- }
- }
- else{
- //Input returned
- return i[0]+"/"+i[1];
- }
- }
- public static int gcd(int p, int q) {
- //Euclidean algorithm for greatest common divisor
- if(q == 0){
- return p;
- }
- else{
- return gcd(q, p%q);
- }
- }
- private static boolean isInteger(String s){
- //Integer check
- try{
- Integer.parseInt(s);
- }
- catch(NumberFormatException e){
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement