nathanwailes

LeetCode 271 - Encode and Decode Strings - 2023.09.28 solution

Sep 28th, 2023
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. """
  2. # Encode and Decode Strings
  3.  
  4. Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
  5.  
  6. Please implement `encode` and `decode`
  7.  
  8. **Example 1:**
  9.  
  10. ```
  11. Input: ["lint","code","love","you"]
  12. Output: ["lint","code","love","you"]
  13.  
  14. Explanation:
  15. One possible encode method is: "lint:;code:;love:;you"
  16. ```
  17.  
  18. **Example 2:**
  19.  
  20. ```
  21. Input: ["we", "say", ":", "yes"]
  22. Output: ["we", "say", ":", "yes"]
  23.  
  24. Explanation:
  25. One possible encode method is: "we:;say:;:::;yes"
  26. ```
  27. """
  28.  
  29.  
  30. from typing import List
  31.  
  32.  
  33. class Solution:
  34.     """
  35.    @param: strs: a list of strings
  36.    @return: encodes a list of strings to a single string.
  37.    """
  38.     def encode(self, strs):
  39.         output_string = ""
  40.         for input_string in strs:
  41.             output_string += str(len(input_string)) + ':' + input_string
  42.         return output_string
  43.  
  44.     """
  45.    @param: str: A string
  46.    @return: decodes a single string to a list of strings
  47.    """
  48.     def decode(self, str):
  49.         output_strings = []
  50.         i = 0
  51.  
  52.         print(str)
  53.         while i < len(str):
  54.             length = ""
  55.             while str[i] != ':':
  56.                 length += str[i]
  57.                 i += 1
  58.             length = int(length)
  59.            
  60.             i += 1  # Skip the ':'
  61.  
  62.             output_strings.append(str[i:i + length])
  63.  
  64.             i = i + length
  65.         return output_strings
  66.  
  67.  
  68. def run_tests():
  69.   solution = Solution()
  70.  
  71.   # First we'll do the test case shown in the task description so that will be the one shown to people first if their code isn't working.  If that passes then we'll do other test cases.
  72.   test_cases = [{
  73.     'input': ["lint", "code", "love", "you"],
  74.     'output': ["lint", "code", "love", "you"]
  75.   }, {
  76.     'input': ["we", "say", ":", "yes"]
  77.   }]
  78.  
  79.   for test_case in test_cases:
  80.     input = test_case['input']
  81.  
  82.     encoded_string = solution.encode(input)
  83.     if not isinstance(encoded_string, str):
  84.       print(
  85.         'Test case failed.\nInput: {}\nExpected encode() return type to be str\nGot: {}\n'
  86.         .format(input, type(encoded_string)))
  87.       return
  88.  
  89.     decoded_string = solution.decode(encoded_string)
  90.     if not decoded_string == input:
  91.       print('Test case decode() failed.\nExpected: {}\nGot: {}\n'.format(
  92.         input, decoded_string))
  93.       return
  94.   print("All test cases passed!")
  95.  
Advertisement
Add Comment
Please, Sign In to add comment