diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-20 21:40:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-20 21:40:27 +0100 |
| commit | b521af146ef7c2bd338bc5355e2fc8f6c49cbd30 (patch) | |
| tree | 8d8e24fcdbb56212b91343e1306cce425826dc53 /challenge-068/paulo-custodio/python/ch-2.py | |
| parent | 962fb60ab30715e2dedc4fda8619042d22fba65e (diff) | |
| parent | 4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 (diff) | |
| download | perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.gz perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.bz2 perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.zip | |
Merge pull request #10874 from pauloscustodio/master
Add Python solution to challenge 067
Diffstat (limited to 'challenge-068/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-068/paulo-custodio/python/ch-2.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-068/paulo-custodio/python/ch-2.py b/challenge-068/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..55e0db5737 --- /dev/null +++ b/challenge-068/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 068 +# +# TASK #2 > Reorder List +# Submitted by: Mohammad S Anwar +# You are given a singly linked list $L as below: +# +# L0 ? L1 ? ... ? Ln-1 ? Ln +# Write a script to reorder list as below: +# +# L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? +# You are ONLY allowed to do this in-place without altering the nodes' values. +# +# Example +# Input: 1 ? 2 ? 3 ? 4 +# Output: 1 ? 4 ? 2 ? 3 + +import unittest + +def reorder_list(l): + # get second element + tail = l[1] + # get and remove last element + p = tail + last = None + while len(p) > 1: + last = p + p = p[1] + eln = last.pop() + + return [l[0], [eln[0], tail]] + +class TestReorderList(unittest.TestCase): + def test_reorder_list(self): + self.assertEqual(reorder_list([1, [2, [3, [4]]]]), [1, [4, [2, [3]]]]) + +if __name__ == '__main__': + unittest.main() |
