## The challenge

Count the number of occurrences of each character and return it as a list of tuples in order of appearance. For empty output return an empty list.

Example:

 ``````1 `````` ``````ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)] ``````

## The solution in Python code

Option 1:

 ``````1 2 3 4 `````` ``````from collections import Counter def ordered_count(input): c = Counter(input) return sorted(list(c.items()), key=lambda x: input.index(x[0])) ``````

Option 2:

 ``````1 2 3 4 5 6 `````` ``````def ordered_count(_input): l = [] for i in _input: if i not in l: l.append(i) return [(i, _input.count(i)) for i in l] ``````

Option 3:

 ``````1 2 `````` ``````def ordered_count(inp): return [(i, inp.count(i)) for i in sorted(set(inp), key=inp.index)] ``````

## Test cases to validate our solution

 `````` 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 `````` ``````test.describe("Basic Tests") tests = ( ('abracadabra', [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]) ) for t in tests: inp, exp = t test.assert_equals(ordered_count(inp), exp) test.describe("Random Tests") def random_tests(): from string import ( ascii_letters, punctuation, digits ) from collections import ( OrderedDict, Counter ) from random import ( randint, choice ) class _OrderedCounter(Counter, OrderedDict): pass def reference(seq): return list(_OrderedCounter(seq).items()) CHARS = "".join((" ", ascii_letters, punctuation, digits)) for _ in range(100): test_case = "".join(choice(CHARS) for _ in range(randint(1, 1000))) test.assert_equals(ordered_count(test_case), reference(test_case)) random_tests() ``````