Difference between revisions of "Finite State Machine"

From Progzoo
Jump to: navigation, search
(Even Binary Numbers)
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
==Even Binary Numbers==
 
==Even Binary Numbers==
  
<question class='EvenBinary'>
+
<question className='EvenBinary'>
 
<prog><![CDATA[
 
<prog><![CDATA[
public class P{
+
public class EvenBinary{
  
 
   public static void main(String[] args) {
 
   public static void main(String[] args) {
Line 52: Line 52:
 
</question>
 
</question>
  
==What is the function==
 
  
<question class='P'>
+
==Even Binary Numbers - Abstracted==
 +
 
 +
<question className='EvenBinary'>
 
<prog><![CDATA[
 
<prog><![CDATA[
public class P{
+
import java.util.HashMap;
   public static void main(String argv[])
+
public class EvenBinary{
 +
 
 +
   public static void main(String[] args) {
 +
    String [] ls = {"10", "111", "01"};
 +
    for (String s : ls)
 +
      System.out.printf("%10s, %s\n", s,sm(s));
 +
}
 +
 +
  public enum States {S,A,B};
 +
  public static boolean sm(String s)
 
   {
 
   {
    for (int i=0;i<5;i++)
+
    HashMap<States,HashMap<String,States>> d =  
      System.out.printf("%d%8d\n",i,f(i));
+
      new HashMap<States,HashMap<String,States>>();
  }
+
    d.put(States.S,new HashMap<String,States>());
 
+
    d.put(States.A,new HashMap<String,States>());
  static int f(int i){
+
    d.put(States.B,new HashMap<String,States>());
     if (i==0)
+
    d.get(States.S).put("1",States.B);
       return 0;
+
    d.get(States.A).put("0",States.A);
     else
+
    d.get(States.A).put("1",States.B);
      return 2+f(i-1);
+
    d.get(States.B).put("0",States.A);
 +
    d.get(States.B).put("1",States.B);
 +
   
 +
    States state = States.S;
 +
    int i=0;
 +
    while (i<s.length() && state!=null)
 +
     {
 +
      String c = s.substring(i,i+1);
 +
      state = d.get(state).get(c);
 +
       i++;
 +
     }
 +
    return state==States.A;
 
   }
 
   }
 
}
 
}
Line 73: Line 94:
 
</question>
 
</question>
  
 +
==Divisible by Three (decimal)==
  
==What is the function==
+
<question className='Div3'>
 
+
<question class='P'>
+
 
<prog><![CDATA[
 
<prog><![CDATA[
public class P{
 
  public static void main(String argv[])
 
  {
 
    for (int i=0;i<5;i++)
 
      System.out.printf("%d%8d\n",i,f(i));
 
  }
 
  
  static int f(int i){
+
public class Div3 {
    if (i==0)
+
      return 1;
+
    else
+
      return 2*f(i-1);
+
  }
+
}
+
]]></prog>
+
</question>
+
==What is the function==
+
  
<question class='P'>
+
   public static void main(String[] args) {
<prog><![CDATA[
+
    String [] ls = {"10", "111", "01","123"};
public class P{
+
    for (String s : ls)
   public static void main(String argv[])
+
      System.out.printf("%10s, %s\n", s,sm(s));
  {
+
    for (int i:new int[]{2, 42, 111, 2753})
+
      System.out.printf("%5d%8d\n",i,f(i));
+
 
   }
 
   }
 
+
  public enum States {FAIL,A,B,C};
   static int f(int i){
+
   public static boolean sm(String s)
     if (i==0)
+
  {
       return 0;
+
    States state = States.A;
    else
+
    int i=0;
       return (i%10)+f(i/10);
+
    while (i<s.length() &&
 +
        state!=States.FAIL)
 +
     {
 +
      char c = s.charAt(i);
 +
      switch (state){
 +
      case A:
 +
        if (c=='0' || c=='3' || c=='6' || c=='9')
 +
          state = States.A;
 +
        else if (c=='1'||c=='4'||c=='7')
 +
          state = States.B;
 +
        else if (c=='2'||c=='5'||c=='8')
 +
          state = States.C;
 +
        break;
 +
       case B:
 +
        if (c=='0' || c=='3' || c=='6' || c=='9')
 +
          state = States.B;
 +
        else if (c=='1'||c=='4'||c=='7')
 +
          state = States.C;
 +
        else if (c=='2'||c=='5'||c=='8')
 +
          state = States.A;
 +
        break;
 +
       case C:
 +
        if (c=='0' || c=='3' || c=='6' || c=='9')
 +
          state = States.C;
 +
        else if (c=='1'||c=='4'||c=='7')
 +
          state = States.A;
 +
        else if (c=='2'||c=='5'||c=='8')
 +
          state = States.B;
 +
        break;
 +
      }
 +
      i++;
 +
    }
 +
    return state==States.A;
 
   }
 
   }
 
}
 
}
 
 
 
]]></prog>
 
]]></prog>
 
</question>
 
</question>

Latest revision as of 16:03, 1 March 2012

Finite State Machines

Even Binary Numbers


[Font] [Default] [Show] [Resize] [History] [Profile]


Even Binary Numbers - Abstracted


[Font] [Default] [Show] [Resize] [History] [Profile]

Divisible by Three (decimal)


[Font] [Default] [Show] [Resize] [History] [Profile]