  Python. 24     
 


  ,    , ,    Python,       . 24  :   ,   ,   ,   , ,    .          ,  ,  , ,     .     ;       .           .   ,        .





 

  Python. 24     





     


   .  ,      :     ,            .      .  ,           .

   24  .     Python  . ,     ,  ,         .        :   ,   ,    .

   , , ,   .       :  ,  ,    .    ,          .       .

,        .    :            .     ;         .     ,              .

        .    ,     ,           .    ,         .           .




    


   Python 3.12.       ;    .      python.org.     :   python3 task.py  py task.py. ,      Python 3,     .

     .         .      ,            .       :        .

        . ,       ,      .          .      ;          .

  solve   ,    .       :    .                       .       Python-  .




    


 Python  ,         .            ?,          . , ?return result      return result.     .        ;     .

      ,    Python    .                  .     ,  list.append,  :   .          .   ,    .       ,     .

      .       ,           copied.txt,   UTF-8.      clean.py        .        :       .  python3 clean.py,  python3 task.py.

from pathlib import Path

text = Path("copied.txt").read_text(encoding="utf-8")

lines = text.splitlines()

marked = [s for s in lines if "?" in s]

stripped = [s.split("?", 1)[1] for s in marked]

clean = [s.replace("", " ") for s in stripped]

result = "\n".join(clean).replace("\u00a0", " ") + "\n"

f = open("task.py", "x", encoding="utf-8")

f.write(result)

f.close()

   task.py   x:     ,    FileExistsError     .       .     w    ,      .        ;   task.py  .

     ,      .        .   ,      .      ,       .               .




,   


    ,        .   :  ,      ,            ,   .         ,      .

  ,     .            ;     ,     .   ,   :     ,            .

 ,    .     ,       ,    .       ,   ,      .      ;  ,     .

       .     ,      .       ,       .    .           ,      .




     


 n     ;       V    E.  O(n)         ,     .         .    ,        .

              ,      .      .        ,        ;       .

   ,     .       n ,   ,     .     ,        .     .        .

           .    T  T+1 ,    T   .  ,     T,      .     ,       .




    


         .     ,    .        .       :      ,     .

    assert     .   , Python  AssertionError       OK.        .       -O:    assert   .

      :  ,   ,  ,     .     ,       .         :     .

     .    ,       .   ,        .      :   ,     ,   .




 1.    


       .      ,      :     .  solve(items)        .   ,     : '', ''  ' '   .   ,     .   ;        .




 solve: (['', '', '', '', ''],)

 : ['', '', '']




  ,      .       :        .




?def solve(items):

?seen = set()

?result = []

?for item in items:

?if item not in seen:

?seen.add(item)

?result.append(item)

?return result


  

?cases = [((['', '', '', '', ''],),

?['', '', '']),

?(([],), []),

?((['', '', '', ''],), ['', '']),

?((['', '', '', ' '],), ['', '', ' ']),

?((['x', 'x', 'x'],), ['x'])]

?for args, expected in cases:

?assert solve(*args) == expected

?print('OK:  1')

  : OK:  1


  

     . ''   seen,     ,   .  ''    .   ''     .   '',   '' .  ,            .

     : seen      ,  result       .     ,         .   ,     ;        .

  ,    ,  ,    ,   ,   .      :      .  ,    ;   items    .          :   .


  

n   , u    .   O(n) -  O(u)    .     :    O(n?) .           .


 

 list(set(items)):  ,       .     result ,      O(n?) .


 

     ,       ?


 

:     seen,   result  item.upper().        .      ,      .




 2.   


     .         :        .  solve(values)         (, _).      ,   .     .      ;     .      .




 solve: ([4, 4, 1, 1, 1, 4, 0, 0],)

 : [(4, 2), (1, 3), (4, 1), (0, 2)]




        .     . ,    ,     .




?def solve(values):

?if not values:

?return []

?result = []

?current = values[0]

?count = 1

?for index in range(1, len(values)):

?value = values[index]

?if value == current:

?count += 1

?else:

?result.append((current, count))

?current = value

?count = 1

?result.append((current, count))

?return result


  

?cases = [(([4, 4, 1, 1, 1, 4, 0, 0],), [(4, 2), (1, 3), (4, 1), (0, 2)]),

?(([],), []),

?(([7],), [(7, 1)]),

?(([-2, -2, -2],), [(-2, 3)]),

?(([1, 2, 1],), [(1, 1), (2, 1), (1, 1)])]

?for args, expected in cases:

?assert solve(*args) == expected

?print('OK:  2')

  : OK:  2


  

       (4, 2).    ,        .       :       .      (4, 1),        (0, 2).

   result      .  current  count     ,    :     .     count.   ,    ,     .

       ,        ,    .   append    .        :     values[0]     .           values[1:].


  

n   , r   .  O(n),   O(1)    O(r)  .       O(1); -  .


 

    :        ,      .      .


 

 ,     ?


 

   (value, count)  count  value    .      n,        .        .




 3.    


     ;       .  solve(values, target)       target.    (i, j),  0 <= i < j < len(values)    target,  None,   .   :   j,      j  i.  ,      ;     .      .




 solve: ([4, 4, 9, 6, 1], 10)

 : (0, 3)




       .          .       .




?def solve(values, target):

?first = {}

?for j, value in enumerate(values):

?needed = target - value

?if needed in first:

?return (first[needed], j)

?if value not in first:

?first[value] = j

?return None


  

?cases = [(([4, 4, 9, 6, 1], 10), (0, 3)),

?(([], 0), None),

?(([5], 10), None),

?(([5, 5, 5], 10), (0, 1)),

?(([1, 4, 3, 6], 7), (1, 2)),

?(([-3, 8, 2, 5], 5), (0, 1))]

?for args, expected in cases:

?assert solve(*args) == expected

?print('OK:  3')

  : OK:  3


  

       4: 0.      6      .   ,    .     3  4  :  (0, 3).  (2, 4)    10,     ;  (1, 3)   .

  j  first          j.             .       :        .       target == 2 * value     .

          . ,      :     .   j     target - value,       .      .     ,             .


  

n   , u     .   O(n)   O(1)    -, O(u)  .      O(n?);     .


 

  first[value] = j:        .          .


 

         (i, j)?


 

.  [1, 4, 3, 6]   7  (1, 2),  j = 2   j = 3.     i    (0, 3).     .




 4.   


     .        ,     .  solve(left, right)   :     ,       .        left;          left.     ,    ,   .     .




 solve: (['b', 'a', 'b', 'c', 'a', 'b'], ['a', 'b', 'b', 'd'])

 : ['b', 'a', 'b']




     :       .       ,    .




?def solve(left, right):

?from collections import Counter

?

?remaining = Counter(right)

?result = []

?for item in left:

?if remaining[item] > 0:

?result.append(item)

?remaining[item] -= 1

?return result


  

?cases = [((['b', 'a', 'b', 'c', 'a', 'b'], ['a', 'b', 'b', 'd']),

?['b', 'a', 'b']),

?(([], ['a']), []),

?((['a', 'b'], []), []),

?((['x', 'x', 'x'], ['x', 'x']), ['x', 'x']),

?((['B', 'b', 'a'], ['a', 'b']), ['b', 'a'])]

?for args, expected in cases:

?assert solve(*args) == expected

?print('OK:  4')

  : OK:  4


  

        b     a  d.  b  ,  a  .  b    b.  c  ;  a   b  .   d    ,    left   .  [b, a, b].

: remaining[x]   x  right     x    left.    ,        .  result     left. ,   ,         .

      ,      ,   .     ,      .        :     . Counter     ,        .           left.


  

n = len(left), m = len(right), u     right.   O(n + m) -, O(u)    .  ,    -;    O((n + m)?) .       .


 

  set(left) & set(right):        .  item in right   , ,    .


 

   left  right   ?


 

   :     ,    .      left.           left      .




  .


   .

   ,     (https://www.litres.ru/book/raznoe/algoritmy-na-python-24-zadachi-s-resheniiami-i-proverkami-74417087/)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


