aboutsummaryrefslogtreecommitdiff
path: root/challenge-182/roger-bell-west/python/ch-2.py
blob: c06b97aedb5d42c12a422401fa912677cfc2dd51 (plain)
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
#! /usr/bin/python3

from collections import defaultdict

import unittest

def commonpath(p):
  pa = []
  pl = []
  for sp in p:
    q = sp.split("/")
    pl.append(len(q))
    pa.append(q)
  out = []
  for cl in range(min(pl)):
    ex = False
    tx = pa[0][cl]
    for pe in pa:
      if pe[cl] != tx:
        ex = True
        break
    if ex:
      break
    out.append(tx)
  return "/".join(out)

class TestCommonpath(unittest.TestCase):

        def test_ex1(self):
          self.assertEqual(commonpath([
            "/a/b/c/1/x.pl",
            "/a/b/c/d/e/2/x.pl",
            "/a/b/c/d/3/x.pl",
            "/a/b/c/4/x.pl",
            "/a/b/c/d/5/x.pl"
          ]),
                           "/a/b/c", 'example 1')

unittest.main()