Sha256: fac134556ba3745ad4ab36808c747dea478d13e1bae175d8190d0784457312c0

Contents?: true

Size: 973 Bytes

Versions: 135

Compression:

Stored size: 973 Bytes

Contents

class WordCount {
  Map<String, int> countWords(String input) {
    Map<String, int> count = new Map();

    /// make a list of all words using regex.
    /// \\w+ matches any number of letters
    ///  e.g. one two three
    /// \\w+'\\w  => matches any number of letters then "'" and then a single letter
    ///  e.g. don't
    /// \\d+ matches one or more digits
    List<String> allWords = new List();
    var wordMatches = new RegExp("\\w+'\\w|\\w+|d+").allMatches(input.toLowerCase());
    for (var match in wordMatches) {
      allWords.add(match.group(0));
    }

    /// Lists aka "arrays" have a forEach method that applies a function to
    /// each element of the list.
    /// initialize the Map with all words and there counter to 0
    allWords.forEach((singleWord) => count[singleWord] = 0);

    /// count all words and set there counter accordingly
    allWords.forEach((singleWord) => count[singleWord] = count[singleWord] + 1);
    return count;
  }
}

Version data entries

135 entries across 135 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.179 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.178 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.177 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.176 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.175 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.174 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.173 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.172 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.171 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.170 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.169 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.167 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.166 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.165 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.164 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.163 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.162 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.161 tracks/dart/exercises/word-count/lib/example.dart
trackler-2.2.1.160 tracks/dart/exercises/word-count/lib/example.dart