Posts Biodiversity
Post
Cancel

Biodiversity

We are interested in how much biological diversity exists in line of cells. Each cell may either be alive (represented by A) or dead (represented by D).

Instructions

View Instructions.

Remember, if you are given a coding problem, the solution is probably HashSet ~Mr. Allen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;

class Biodiversity
{
   public static int getDiversity(String lineOfCells)
   {
      Set<String>strands = new HashSet<String>();
      boolean inStrand = false;
      int countStrand = 0;
      for(int i = 0; i<lineOfCells.length(); i++)
      {
        if(countStrand == strands.size())
        {
          //return 1;
        }
        if(i ==0 && lineOfCells.charAt(0)=='A')
        {
          inStrand = true;
          countStrand++;
          System.out.println("first letter a");
          continue;
        }
        
        if(lineOfCells.charAt(i)=='D')
        {
          if(inStrand)
          {
            inStrand = false;
            strands.add("" + countStrand);
            countStrand = 0;
          }
        }
        else if(inStrand)
        {
          countStrand++;
        }
        else
        {
          inStrand = true;
          countStrand++;
        }
        
      }
      if(countStrand>0)
      {
        strands.add("" + countStrand);
      }
      return strands.size();
   }
}
This post is licensed under CC BY 4.0