Sha256: 88e041338238fa9826625ed5162cebe9164b0588139cb6347f3af8c1bf9896a8

Contents?: true

Size: 1.03 KB

Versions: 100

Compression:

Stored size: 1.03 KB

Contents

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

final class NucleotideCounter {
  private final String sequence;

  NucleotideCounter(String sequence) {
    this.sequence = sequence;
  }

  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;
  }

  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

100 entries across 100 versions & 1 rubygems

Version Path
trackler-2.2.1.100 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.99 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.98 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.97 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.96 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.95 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.94 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.93 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.92 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.91 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.90 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.89 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.88 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.87 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.86 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.85 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.84 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.83 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.82 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.81 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java