fork download
  1. import java.io.BufferedReader;
  2.  
  3. import java.io.File;
  4.  
  5. import java.io.FileReader;
  6.  
  7. import java.util.Collection;
  8.  
  9. import java.util.HashMap;
  10.  
  11. import java.util.Iterator;
  12.  
  13. import java.util.Map;
  14.  
  15. import java.util.Map.Entry;
  16.  
  17.  
  18. /**
  19.   * Find the Frequency of Codon and Print the combinations of CODONS for the given Word
  20.   * @author
  21.   */
  22. class CountCodon {
  23.  
  24. private static int count;
  25.  
  26. //private static String file = "C:\\input.txt";
  27.  
  28. public static void main(String[] args) throws Exception {
  29.  
  30. //BufferedReader br = new BufferedReader(new FileReader(new File(file)));
  31. //String[] words = br.readLine().trim().split(" ");
  32. String[] words = "pelpevettrrrlrplvlgqtlrqvvhrdpgrrlevpelkmdtkkvr ".trim().split(" ");
  33.  
  34. Map<Character,String[]> hash=init();
  35. for(String word : words)
  36. {
  37. count=0;
  38. word=word.trim().toUpperCase();
  39. combinations(hash, word , word.length(),0 ,"");
  40. System.out.println("Total Combinations :"+count);
  41. Collection<String[]> codons = hash.values();
  42.  
  43. Iterator it = codons.iterator();
  44.  
  45. while(it.hasNext())
  46. {
  47. String[] codes = (String[]) it.next();
  48. for(String code : codes )
  49. {
  50. int freq = getFrequencyCount(code, word,hash);
  51. System.out.println("CODON : "+code + " ---> " + "FREQ : " +freq);
  52. }
  53.  
  54. }
  55.  
  56.  
  57.  
  58. }
  59. private static void solve(String word, String codon) {
  60. Map<Character,String[]> hash=init();
  61.  
  62. combinations(hash, word , word.length(),0 ,"");
  63. System.out.println("Total Combinations :"+count);
  64.  
  65. int freq = getFrequencyCount(codon, word,hash);
  66. System.out.println("CODON : "+codon + " ---> " + "FREQ : " +freq);
  67. System.out.println("######################################");
  68. }*/
  69.  
  70.  
  71. /**
  72.   * Returns the frequency count of the CODON
  73.   * @param codon
  74.   * @param word
  75.   * @param hash
  76.   * @return
  77.   */
  78. private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
  79.  
  80. int currCount = 1;
  81.  
  82. int len=word.length();
  83. //count the total combinations
  84. for(int i=0;i<len;i++)
  85. {
  86. if(hash.get(word.charAt(i)) != null)
  87. currCount = currCount * hash.get(word.charAt(i)).length ;
  88. }
  89.  
  90. //System.out.println("Total Combinations :"+currCount);
  91.  
  92. //Iterate over map and fetch, the character and size of the codons corresponding to that character
  93. char c = 0;
  94. int size=0;
  95. for(Entry<Character, String[]> entry : hash.entrySet())
  96. {
  97. String[] vals =entry.getValue();
  98. for(String v:vals)
  99. {
  100. if(v.equalsIgnoreCase(codon)) //if codon is found, get the character
  101. {
  102. c=entry.getKey();
  103. size=vals.length;
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. //If no charater is found corresponding to the CODON
  110. if(c==0)
  111. return 0;
  112.  
  113. //No. of times the character appears in the word, tricky
  114. int charCount = word.split(c+"").length-1;
  115.  
  116. return charCount * (currCount/size);
  117. }
  118.  
  119. /**
  120.   * Prints All combinations of the codons
  121.   * @param hash: storage unit of character and corresponding codons
  122.   * @param word: the given word , eg:
  123.   * @param len: length or 'word'
  124.   * @param i: currrent character in 'word'
  125.   * @param s: combination formed in intermediate steps
  126.   */
  127. private static void combinations(Map<Character, String[]> hash,
  128. String word, int len , int i,String s) {
  129.  
  130. //if currCharacter reaches end of word, print
  131. if(i>=len)
  132. {
  133. System.out.println(s);
  134. count++;
  135. return;
  136. }
  137.  
  138. //current Character
  139. char c = word.charAt(i);
  140. //Codons corresponding to the character
  141. String[] codons = hash.get(c);
  142.  
  143. String temp = s;
  144. if(codons!=null)
  145. {
  146. for(int j=0;j<codons.length;j++)
  147. {
  148. s=temp; //initialize s , with the value held in temp everytime s gets modified
  149. s = s+":"+codons[j];
  150. combinations(hash,word,len,i+1,s);
  151. }
  152. }
  153. }
  154.  
  155. /**
  156.   * Initialzing Map
  157.   * @return: returns map of character and its corresponding Codons
  158.   */
  159. private static Map<Character,String[]> init() {
  160. Map<Character,String[]> hash = new HashMap<Character,String[]>();
  161. hash.put('F',new String[]{"UUU","UUC"});
  162. hash.put('I',new String[]{"AUU","AUC","AUA"});
  163. hash.put('P',new String[]{"CCU","CCC","CCA","CCG"});
  164. hash.put('S',new String[]{"UCU","UCC","UCA","UCG","AGU","AGC"});
  165. hash.put('T',new String[]{"ACU","ACC","ACA","ACG"});
  166. hash.put('A',new String[]{"GCU","GCC","GCA","GCG"});
  167. hash.put('E',new String[]{"CAA","CAG"});
  168. hash.put('L',new String[]{"UUA","UUG","CUU","CUC","CUA","CUG"});
  169. hash.put('V',new String[]{"GUU","GUC","GUA","GUG"});
  170. hash.put('T',new String[]{"UAU","UAC"});
  171. hash.put('R',new String[]{"CGU","CGC","CGA","CGG","AGA","AGG"});
  172. hash.put('K',new String[]{"AAA","AAG"});
  173. hash.put('Y',new String[]{"UAU","UAC"});
  174. hash.put('H',new String[]{"CAU","CAC"});
  175. hash.put('N',new String[]{"AAU","AAC"});
  176. hash.put('D',new String[]{"GAU","GAC"});
  177. hash.put('C',new String[]{"UGU","UGC"});
  178. hash.put('W',new String[]{"UGG"});
  179. hash.put('G',new String[]{"GGU","GGC","GGA","GGG"});
  180. hash.put('M',new String[]{"AUG"});
  181. return hash;
  182. }
  183.  
  184. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:59: error: illegal start of expression
    	private static void solve(String word, String codon) {
    	^
Main.java:59: error: illegal start of expression
    	private static void solve(String word, String codon) {
    	        ^
Main.java:59: error: ';' expected
    	private static void solve(String word, String codon) {
    	              ^
Main.java:59: error: ')' expected
    	private static void solve(String word, String codon) {
    	                                ^
Main.java:59: error: illegal start of expression
    	private static void solve(String word, String codon) {
    	                                     ^
Main.java:59: error: ';' expected
    	private static void solve(String word, String codon) {
    	                                             ^
Main.java:59: error: not a statement
    	private static void solve(String word, String codon) {
    	                                              ^
Main.java:59: error: ';' expected
    	private static void solve(String word, String codon) {
    	                                                   ^
Main.java:68: error: illegal start of expression
    	}*/
    	 ^
Main.java:68: error: illegal start of expression
    	}*/
    	  ^
Main.java:78: error: illegal start of expression
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	^
Main.java:78: error: illegal start of expression
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	        ^
Main.java:78: error: ';' expected
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	              ^
Main.java:78: error: ')' expected
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	                                           ^
Main.java:78: error: illegal start of expression
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	                                                  ^
Main.java:78: error: ';' expected
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	                                                         ^
Main.java:78: error: not a statement
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	                                                          ^
Main.java:78: error: ';' expected
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	                                                              ^
Main.java:78: error: ';' expected
    	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {
    	                                                                                             ^
Main.java:127: error: illegal start of expression
    	private static void combinations(Map<Character, String[]> hash,
    	^
Main.java:127: error: illegal start of expression
    	private static void combinations(Map<Character, String[]> hash,
    	        ^
Main.java:127: error: ';' expected
    	private static void combinations(Map<Character, String[]> hash,
    	              ^
Main.java:127: error: '.class' expected
    	private static void combinations(Map<Character, String[]> hash,
    	                                                        ^
Main.java:128: error: ')' expected
    			String word, int len , int i,String s) {
    			      ^
Main.java:128: error: illegal start of expression
    			String word, int len , int i,String s) {
    			           ^
Main.java:128: error: ';' expected
    			String word, int len , int i,String s) {
    			            ^
Main.java:128: error: not a statement
    			String word, int len , int i,String s) {
    			                 ^
Main.java:128: error: ';' expected
    			String word, int len , int i,String s) {
    			                    ^
Main.java:128: error: ';' expected
    			String word, int len , int i,String s) {
    			                                   ^
Main.java:128: error: not a statement
    			String word, int len , int i,String s) {
    			                                    ^
Main.java:128: error: ';' expected
    			String word, int len , int i,String s) {
    			                                     ^
Main.java:159: error: illegal start of expression
    	private static Map<Character,String[]> init() {
    	^
Main.java:159: error: illegal start of expression
    	private static Map<Character,String[]> init() {
    	        ^
Main.java:159: error: ';' expected
    	private static Map<Character,String[]> init() {
    	                  ^
Main.java:159: error: illegal start of expression
    	private static Map<Character,String[]> init() {
    	                                       ^
Main.java:159: error: illegal start of expression
    	private static Map<Character,String[]> init() {
    	                                            ^
Main.java:159: error: not a statement
    	private static Map<Character,String[]> init() {
    	                                           ^
Main.java:159: error: ';' expected
    	private static Map<Character,String[]> init() {
    	                                             ^
Main.java:184: error: reached end of file while parsing
    }
     ^
39 errors
stdout
Standard output is empty