mfgnik

Untitled

May 17th, 2020
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import typing as tp
  2.  
  3.  
  4. def merge(a: tp.Sequence[int], b: tp.Sequence[int]) -> tp.Sequence[int]:
  5.     """
  6.    Merge two sorted lists in one sorted list
  7.    :param a: first sorted list
  8.    :param b: second sorted list
  9.    :return: merged sorted list
  10.    """
  11.     result_list = []
  12.     if len(b) == 0:
  13.         return a
  14.     if len(a) == 0:
  15.         return b
  16.     index_first = 0
  17.     index_second = 0
  18.     while index_first < len(a) and index_second < len(b):
  19.         if a[index_first] < b[index_second]:
  20.             result_list.append(a[index_first])
  21.             index_first += 1
  22.         else:
  23.             result_list.append(b[index_second])
  24.             index_second += 1
  25.     if index_first < len(a):
  26.         result_list.extend(a[index_first:])
  27.     if index_second < len(b):
  28.         result_list.extend(b[index_second:])
  29.     return result_list
Advertisement
Add Comment
Please, Sign In to add comment