Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. package lec10b;
  2.  
  3. /**
  4.  * lec10b: This is a TEAM CHALLENGE.
  5.  *
  6.  * A message consists entirely of uppercase letters and is encoded
  7.  * according to the following scheme: A=1, B=2, ..., Z=26. For
  8.  * example, the message BEAN encodes as 25114 and the message
  9.  * TEAM encodes as 205113.
  10.  *
  11.  * Such messages cannot be decoded unambiguously:
  12.  *     25114 decodes as BEAN, BEAAD, YAAD, YAN, YKD, and BEKD.
  13.  *    205113 decodes as TEAM, TEAAC, and TEKC.
  14.  *
  15.  *
  16.  * Complete the following tasks:
  17.  *
  18.  * (1) Implement the numDecodings() method.
  19.  *
  20.  * (2) Which of the 15 provided test cases does your method pass?
  21.  *
  22.  *     Answer:
  23.  *
  24.  * (3) What is the Big-O running time of your method?
  25.  *
  26.  *     Answer:
  27.  *
  28.  * (4) List the names and usernames of all team members who actively
  29.  *     contributed to this solution:
  30.  *
  31.  *     Answer:
  32.  *
  33.  */
  34.  
  35. public class Coder {
  36.  
  37.   /**
  38.    * TODO
  39.    *
  40.    * Returns the number of decodings corresponding to the encoded
  41.    * message. Assume that msg consists only of digit characters and is
  42.    * the result of a proper encoding. Note that this excludes the
  43.    * inputs 0123 and 123045.
  44.    */
  45.  
  46. //  private static Long total = 0L;
  47.  
  48.   public static Long numDecodings(String msg) {
  49.     return numDecodingsHelper(msg, 0L);
  50.   }
  51.  
  52.   private static Long numDecodingsHelper(String msg, Long total) {
  53.     int pointer = msg.length();
  54.  
  55.     if(msg.length() == 0)
  56.       return 1L;
  57.  
  58.     if (pointer >= 1) {
  59.       String sub1 = msg.substring(pointer - 1);
  60.       if(validNum(sub1))
  61.         total += numDecodingsHelper(msg.substring(0,pointer-1));
  62.     }
  63.  
  64.     if (pointer >= 2) {
  65.       String sub2 = msg.substring(pointer - 2);
  66.       if(validNum(sub2))
  67.         total += numDecodingsHelper(msg.substring(0,pointer-2));
  68.     }
  69.  
  70.     return total;
  71.   }
  72.  
  73.   public static boolean validNum(String num){
  74.     int numb = Integer.parseInt(num);
  75.     if(num.startsWith("0"))
  76.       return false;
  77.     if(numb > 0 &&  numb < 27)
  78.       return true;
  79.     return false;
  80.   }
  81.  
  82.  
  83.   /**
  84.    * Returns the result of appending str to itself n times. This is a
  85.    * convenient utility for generating tests.
  86.    */
  87.  
  88.   public static String repeat(String str, int n) {
  89.     StringBuilder ans = new StringBuilder();
  90.     for (int i = 0; i < n; i++)
  91.       ans.append(str);
  92.     return ans.toString();
  93.   }
  94.  
  95.   /**
  96.    * Simple testing.
  97.    */
  98.  
  99.   public static void main(String... args) {
  100.     /*  1 */ assert numDecodings("") == 1L;
  101.     /*  2 */ assert numDecodings("3") == 1L;
  102.     /*  3 */ assert numDecodings("12") == 2L;
  103.     /*  4 */ assert numDecodings("25114") == 6L;
  104.     /*  5 */ assert numDecodings("205113") == 3L;
  105.     /*  6 */ assert numDecodings(repeat("1", 10)) == 89L;
  106.     /*  7 */ assert numDecodings(repeat("3", 20)) == 1L;
  107.     /*  8 */ assert numDecodings(repeat("1", 44)) == 1134903170L;
  108.     /*  9 */ assert numDecodings(repeat("3", 45)) == 1L;
  109.     /* 10 */ assert numDecodings(repeat("1", 46)) == 2971215073L;
  110.     /* 11 */ assert numDecodings(repeat("3", 47)) == 1L;
  111.     /* 12 */ assert numDecodings(repeat("1", 48)) == 7778742049L;
  112.     /* 13 */ assert numDecodings(repeat("3", 49)) == 1L;
  113.     /* 14 */ assert numDecodings(repeat("1", 50)) == 20365011074L;
  114.     /* 15 */ assert numDecodings(repeat("1", 100)) == 1298777728820984005L;
  115.     System.out.println("All tests passed...");
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement