janek9971

Pyton

Oct 28th, 2025 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | Source Code | 0 0
  1. @pytest.mark.parametrize("array, k, expected", [([1, 2, 3, 4, 5, 6, 7], 3, [3, 4, 5, 6, 1, 2])])
  2. def test_rotate(array, k, expected):
  3.     assert rotate(array, k) == expected
  4.  
  5.  
  6. @pytest.mark.parametrize("s, expected", [(",,,,,,,,,,,,acva", False)])
  7. def test_palindrome(s, expected):
  8.     assert isPalindrome(s) == expected
  9.  
  10.  
  11. @pytest.mark.parametrize("s, expected", [(",,,,,,,,,,,,acva", False)])
  12. def test_text_to_telex_encoding(s, expected):
  13.     assert text_to_telex_encoding(s) == expected
  14.  
  15.  
  16.  
  17.  
  18. def isPalindrome(s: str) -> bool:
  19.     left = 0
  20.     i = len(s) - 1
  21.     while i >= 0 and left < i:
  22.         if s[i].isalnum() and s[left].isalnum():
  23.             if s[i].lower() != s[left].lower():
  24.                 return False
  25.             while left < i:
  26.                 left += 1
  27.                 if s[left].isalnum():
  28.                     break
  29.         i -= 1
  30.     return True
  31.  
  32.  
  33. def rotate(nums: list[int], k: int) -> None:
  34.     k = k % len(nums)
  35.     startIndex = len(nums) - k
  36.     left = 0
  37.     if k == 1:
  38.         for x in range(0, len(nums) - 1):
  39.             nums[x], nums[len(nums) - 1] = nums[len(nums) - 1], nums[x]
  40.         return
  41.     for right in range(startIndex, len(nums)):
  42.         nums[left], nums[right] = nums[right], nums[left]
  43.         left += 1
  44.  
  45.     def reverse(left, right):
  46.         while left < right:
  47.             nums[left], nums[right] = nums[right], nums[left]
  48.             left += 1
  49.             right -= 1
  50.  
  51.     reverse(k, len(nums) - 1)
  52.  
  53.  
  54. def a_by_dd_remove_b(s: str) -> None:
  55.     write_idx = 0
  56.     curr_idx = 0
  57.     a_count = 0
  58.     while curr_idx < len(s):
  59.         if s[curr_idx] != "b":
  60.             s[write_idx] = s[curr_idx]
  61.             write_idx += 1
  62.         if s[curr_idx] == "a":
  63.             a_count += 1
  64.  
  65.     curr_idx = write_idx - 1
  66.     write_idx += a_count - 1
  67.     final_size = write_idx + 1
  68.     while curr_idx >= 0:
  69.         if s[curr_idx] == "a":
  70.             s[write_idx - 1 : write_idx + 1] = "dd"
  71.             write_idx -= 2
  72.         else:
  73.             s[write_idx] = s[curr_idx]
  74.             write_idx -= 1
  75.         curr_idx -= 1
  76.     return final_size
  77.  
  78.  
  79. def text_to_telex_encoding(size: int, s: list[str]) -> int:
  80.     ex_mark, ex_mark_len = "EXCLAMATION MARK", 13
  81.     dot, dot_len = "DOT", 3
  82.     comma, comma_len = "COMMA", 5
  83.     q_mark, q_mark_len = "QUESTION MARK", 13
  84.  
  85.     def is_punctuation(ch) -> bool:
  86.         if ch == "!" or ch == "?" or ch == "," or ch == ".":
  87.             return True
  88.         return False
  89.  
  90.     cnt = 0
  91.  
  92.     for i in range(len(s)):
  93.         if s[i] == "!":
  94.             cnt += ex_mark_len
  95.         elif s[i] == ",":
  96.             cnt += comma_len
  97.         elif s[i] == ".":
  98.             cnt += dot_len
  99.         elif s[i] == "?":
  100.             cnt += q_mark_len
  101.         else:
  102.             cnt += 1
  103.  
  104.     curr_idx = len(s) - 1
  105.     write_idx = cnt - 1
  106.     s.extend([None] * cnt - len(s))
  107.  
  108.     while curr_idx >= 0 and write_idx > curr_idx:
  109.         if not is_punctuation(s[curr_idx]):
  110.             s[write_idx] = s[curr_idx]
  111.             write_idx -= 1
  112.             curr_idx -= 1
  113.         else:
  114.             if s[curr_idx] == "!":
  115.                 s[write_idx - ex_mark : write_idx + 1] = ex_mark
  116.             elif s[curr_idx] == ",":
  117.                 s[write_idx - comma_len, : write_idx + 1] = comma
  118.             elif s[curr_idx] == ".":
  119.                 s[write_idx - dot_len : write_idx + 1] = dot
  120.             elif s[curr_idx] == "?":
  121.                 s[write_idx - q_mark_len : write_idx + 1] = q_mark
  122.     return len(s)
  123.  
Advertisement
Add Comment
Please, Sign In to add comment