Sha256: 1fc2ee303bb0974c985b9c6875430e1cf3aa35f975f03a08f96b9af79d25df94

Contents?: true

Size: 1.03 KB

Versions: 218

Compression:

Stored size: 1.03 KB

Contents

import java.util.HashMap;
import java.util.Map;

public final class DNA {
  private final String sequence;

  public DNA(String sequence) {
    this.sequence = sequence;
  }

  public int count(char base) {
    if (isCountable(base))
      throw new IllegalArgumentException(base + " is not a nucleotide");

    try {
      return nucleotideCounts().get(base);
    } catch (NullPointerException e) {
      return 0;
    }
  }

  private static boolean isCountable(char base) {
    final String COUNTABLE_NUCLEOTIDES = "ACGT";
    return COUNTABLE_NUCLEOTIDES.indexOf(base) == -1;
  }

  public Map<Character, Integer> nucleotideCounts() {
    Map<Character, Integer> counts = emptyCounts();
    for (char c : sequence.toCharArray()) {
      counts.put(c, counts.get(c) + 1);
    }
    return counts;
  }

  private static Map<Character, Integer> emptyCounts() {
    Map<Character, Integer> counts = new HashMap<Character, Integer>();
    counts.put('A', 0);
    counts.put('C', 0);
    counts.put('T', 0);
    counts.put('G', 0);
    return counts;
  }
}

Version data entries

218 entries across 218 versions & 1 rubygems

Version Path
trackler-2.2.1.0 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.6 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.5 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.4 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.3 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.2 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.1 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.2.0.0 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.55 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.54 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.53 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.52 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.51 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.50 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.49 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.48 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.47 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.46 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.45 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java
trackler-2.1.0.44 tracks/java/exercises/nucleotide-count/src/example/java/DNA.java