๐Ÿ’ป Exercise: Exercise: Synthesise worker outputs

๐Ÿ“ Instructions

Write synthesize_outputs(outputs, dedupe=True). Strip each output, drop blanks, and (when dedupe) remove duplicates while preserving first-seen order. Return {'parts': [...], 'combined': '<newline-joined parts>', 'count': len(parts)}. Standard library only.

๐Ÿงช Initial Code / Tests

๐Ÿ“„ evaluate.py
from unittest import TestCase
from exercise import synthesize_outputs


class Evaluate(TestCase):
    def test_dedupes_preserving_order(self):
        self.assertEqual(synthesize_outputs(['a', 'b', 'a'])['parts'], ['a', 'b'])

    def test_drops_blanks(self):
        self.assertEqual(synthesize_outputs(['x', '', '  '])['parts'], ['x'])

    def test_combined_is_newline_joined(self):
        self.assertEqual(synthesize_outputs(['a', 'b'])['combined'], 'a\nb')

    def test_count_matches_parts(self):
        out = synthesize_outputs(['a', 'b', 'a', 'c'])
        self.assertEqual(out['count'], 3)

โœ… Solutions

๐Ÿ“„ exercise.py
def synthesize_outputs(outputs, dedupe=True):
    parts = []
    seen = set()
    for o in outputs:
        o = (o or '').strip()
        if not o:
            continue
        if dedupe and o in seen:
            continue
        seen.add(o)
        parts.append(o)
    return {'parts': parts, 'combined': '\n'.join(parts), 'count': len(parts)}