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
44
45
46
47
48
49
50
51
52
53
54
|
### https://theweeklychallenge.org/blog/perl-weekly-challenge-285/
"""
Task 1: No Connection
Submitted by: [49]Mohammad Sajid Anwar
__________________________________________________________________
You are given a list of routes, @routes.
Write a script to find the destination with no further outgoing
connection.
Example 1
Input: @routes = (["B","C"], ["D","B"], ["C","A"])
Output: "A"
"D" -> "B" -> "C" -> "A".
"B" -> "C" -> "A".
"C" -> "A".
"A".
Example 2
Input: @routes = (["A","Z"])
Output: "Z"
Task 2: Making Change
"""
### solution by pokgopun@gmail.com
def nc(routes: tuple):
dct = dict()
for src,dst in routes:
if dct.get(src,True):
dct[src] = False
dct.setdefault(dst,True)
for k,v in dct.items():
if v:
return k
return None
import unittest
class TestNc(unittest.TestCase):
def test(self):
for inpt, otpt in {
(("B","C"), ("D","B"), ("C","A")): "A",
(("A","Z"),): "Z",
}.items():
self.assertEqual(nc(inpt),otpt)
unittest.main()
|