Sha256: 78109b6c90c7400d4ddefa9201e278d5d86713a9fbb862db388feacdc16d08f1

Contents?: true

Size: 1.19 KB

Versions: 78

Compression:

Stored size: 1.19 KB

Contents

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

final class NucleotideCounter {
  private final String sequence;

  NucleotideCounter(String sequence) {
    this.sequence = sequence;
    for (char c : sequence.toCharArray()) {
      if (isCountable(c)) {
        throw new IllegalArgumentException(c + " is not a nucleotide");
      }
    }
  }

  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

78 entries across 78 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.179 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.178 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.177 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.176 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.175 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.174 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.173 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.172 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.171 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.170 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.169 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.167 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.166 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.165 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.164 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.163 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.162 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.161 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java
trackler-2.2.1.160 tracks/java/exercises/nucleotide-count/.meta/src/reference/java/NucleotideCounter.java