Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. package dailyprogrammer;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class E277 {
  8. public static void main(String[] args){
  9.  
  10. String[] inputs = {
  11. "4 8",
  12. "1536 78360",
  13. "51478 5536",
  14. "46410 119340",
  15. "7673 4729",
  16. "4096 1024",
  17. "aaaa aaaa",
  18. "ab cd",
  19. "daily programmer",
  20. "3 x cb y ab z xa ab cb ab x x y z y z xay"
  21.  
  22.  
  23. };
  24. for(int i=0; i<inputs.length; i++){
  25. System.out.println(simplify(inputs[i]));
  26. }
  27.  
  28. }
  29.  
  30. public static String simplify(String input){
  31. String[] i = input.split(" ");
  32. if(i.length != 2){
  33. if(!isInteger(i[0])){
  34. System.out.println(input);
  35. System.out.println("Invalid input type!");
  36. System.exit(0);
  37. }
  38. //Assume substitution case
  39. //System.out.println("Substitutions required for input, parsing...");
  40. Map subs = new HashMap();
  41. ArrayList<String> newfracs = new ArrayList<String>();
  42. ArrayList<Character> subbedvars = new ArrayList<Character>();
  43. for(int e=1; e<i.length; e+=2){
  44. if(e<(1+Integer.parseInt(i[0])*2)){
  45. subbedvars.add(i[e].charAt(0));
  46. subs.put(i[e].charAt(0), i[e+1]);
  47. //System.out.println("Variable substitution registered: " + i[e] + " -> " + subs.get(i[e].charAt(0)));
  48. }
  49. }
  50.  
  51. //The variable subsitution may themselves include substitutions which must be carried out
  52. //System.out.println("Performing substitutions within substitutions...");
  53. ArrayList<String[]> newsubs = new ArrayList<String[]>();
  54. for(int j=0; j<subs.size(); j++){
  55. newsubs.add(new String[] {""+subbedvars.get(j), ""+subs.get(subbedvars.get(j))});
  56. }
  57.  
  58. //Iterate through the set of maps until no changes occur in an iteration
  59. boolean changemade = true;
  60. while(changemade){
  61. changemade = false;
  62. for(int j=0; j<newsubs.size(); j++){
  63. String subbed = "";
  64. for(int k=0; k<newsubs.get(j)[1].length(); k++){
  65. if(subs.containsKey(newsubs.get(j)[1].charAt(k))){
  66. subbed+=subs.get(newsubs.get(j)[1].charAt(k));
  67. changemade = true;
  68. }
  69. else{
  70. subbed+=newsubs.get(j)[1].charAt(k);
  71. }
  72. }
  73. newsubs.get(j)[1] = subbed;
  74. }
  75. }
  76.  
  77. //Create a new map with the changes made above
  78. Map subs2 = new HashMap();
  79. for(int j=0; j<newsubs.size(); j++){
  80. subs2.put(newsubs.get(j)[0], newsubs.get(j)[1]);
  81. }
  82.  
  83.  
  84. //Apply the map to the fractions
  85. for(int e=1; e<i.length; e+=2){
  86. if(e<(1+Integer.parseInt(i[0])*2)){
  87. //Do nothing
  88. }
  89. else{
  90. String f = i[e] + " " + i[e+1];
  91. String f2 = "";
  92. for(int j=0; j<f.length(); j++){
  93. if(subs2.containsKey(""+f.charAt(j))){
  94. f2+=subs2.get(""+f.charAt(j));
  95. }
  96. else{
  97. f2+=f.charAt(j);
  98. }
  99. }
  100. newfracs.add(f2);
  101. }
  102. }
  103.  
  104. for(int j=0; j<newfracs.size(); j++){
  105. //This output would display the input with substituted values
  106. //We don't want this, so take the end of the line and stick the original input
  107. //back on the front
  108.  
  109. String simp = i[2+subs.size()*2 + 2*j-1] + "/" + i[2+subs.size()*2 + 2*j] + " --> " + simplify(newfracs.get(j)).split(" ")[4];
  110. System.out.println(simp);
  111. }
  112. }
  113.  
  114. if(i.length != 2){
  115. return "";
  116. }
  117. if(isInteger(i[0]) && isInteger(i[1])){
  118. //Integer fraction, calculate GCD and divide through
  119. int a = Integer.parseInt(i[0]);
  120. int b = Integer.parseInt(i[1]);
  121.  
  122. //Find the gcd of the a,b
  123. int g = gcd(a,b);
  124.  
  125. b/=g;
  126. a/=g;
  127.  
  128. if(b == 1){
  129. return i[0]+"/"+i[1]+" --> "+a;
  130. }
  131. return i[0]+"/"+i[1]+" --> "+a+"/"+b;
  132. }
  133. else if(!isInteger(i[0]) && !isInteger(i[1])){
  134. //Algebraic fraction (i.e. non-numeric), count the
  135. //number of similar letters and delete as necessary
  136. String a = i[0];
  137. String b = i[1];
  138. for(int j=0; j<a.length(); j++){
  139. if(b.contains(""+a.charAt(j))){
  140. b = b.substring(0, b.indexOf(a.charAt(j))) + b.substring(b.indexOf(a.charAt(j))+1,b.length());
  141. a = a.substring(0, j) + a.substring(j+1,a.length());
  142. j=-1;
  143. }
  144. }
  145. if(a.length() == 0 && b.length() == 0){
  146. //Numerator and denominator blank, 1/1
  147. return i[0]+"/"+i[1]+" --> "+"1";
  148. }
  149. else if(a.length() == 0){
  150. //Numerator blank, 1/b
  151. return i[0]+"/"+i[1]+" --> "+"1/"+b;
  152. }
  153. else if(b.length() == 0){
  154. //Denominator blank, dividing by 1, a
  155. return i[0]+"/"+i[1]+" --> "+a;
  156. }
  157. else{
  158. //Standard output
  159. return i[0]+"/"+i[1]+" --> "+a+"/"+b;
  160. }
  161. }
  162. else{
  163. //Input returned
  164. return i[0]+"/"+i[1];
  165. }
  166. }
  167.  
  168. public static int gcd(int p, int q) {
  169. //Euclidean algorithm for greatest common divisor
  170. if(q == 0){
  171. return p;
  172. }
  173. else{
  174. return gcd(q, p%q);
  175. }
  176. }
  177.  
  178. private static boolean isInteger(String s){
  179. //Integer check
  180. try{
  181. Integer.parseInt(s);
  182. }
  183. catch(NumberFormatException e){
  184. return false;
  185. }
  186. return true;
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement