

def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					0
     SCREAMING_SNAKE_CASE__					=					len(lowercase_       )

     for i in range(n - 1       ):
          for j in range(i + 1   ,	lowercase_       ):
               if arr[i] > arr[j]:
                    num_inversions += 1

     return num_inversions
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if len(lowercase_       ) <= 1:
          return arr, 0
     SCREAMING_SNAKE_CASE__					=					len(lowercase_       ) // 2
     SCREAMING_SNAKE_CASE__					=					arr[0:mid]
     SCREAMING_SNAKE_CASE__					=					arr[mid:]

     SCREAMING_SNAKE_CASE__					=					count_inversions_recursive(lowercase_       )
     SCREAMING_SNAKE_CASE__					=					count_inversions_recursive(lowercase_       )
     SCREAMING_SNAKE_CASE__					=					_count_cross_inversions(lowercase_   ,	lowercase_       )

     SCREAMING_SNAKE_CASE__					=					inversion_p + inversions_q + cross_inversions
     return c, num_inversions
def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[]
     SCREAMING_SNAKE_CASE__					=					0
     while i < len(lowercase_       ) and j < len(lowercase_       ):
          if p[i] > q[j]:
               # if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
               # These are all inversions. The claim emerges from the
               # property that P is sorted.
               num_inversion += len(lowercase_       ) - i
               r.append(q[j]       )
               j += 1
          else:
               r.append(p[i]       )
               i += 1

     if i < len(lowercase_       ):
          r.extend(p[i:]       )
     else:
          r.extend(q[j:]       )

     return r, num_inversion
def        UpperCAmelCase_     (		):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[10, 2, 1, 5, 5, 2, 11]

     # this arr has 8 inversions:
     # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

     SCREAMING_SNAKE_CASE__					=					count_inversions_bf(lowercase_       )
     SCREAMING_SNAKE_CASE__					=					count_inversions_recursive(lowercase_       )

     assert num_inversions_bf == num_inversions_recursive == 8

     print('''number of inversions = '''   ,	lowercase_       )

     # testing an array with zero inversion (a sorted arr_1)

     arr_a.sort()
     SCREAMING_SNAKE_CASE__					=					count_inversions_bf(lowercase_       )
     SCREAMING_SNAKE_CASE__					=					count_inversions_recursive(lowercase_       )

     assert num_inversions_bf == num_inversions_recursive == 0
     print('''number of inversions = '''   ,	lowercase_       )

     # an empty list should also have zero inversions
     SCREAMING_SNAKE_CASE__					=					[]
     SCREAMING_SNAKE_CASE__					=					count_inversions_bf(lowercase_       )
     SCREAMING_SNAKE_CASE__					=					count_inversions_recursive(lowercase_       )

     assert num_inversions_bf == num_inversions_recursive == 0
     print('''number of inversions = '''   ,	lowercase_       )


if __name__ == "__main__":
   main()





import math






class  UpperCAmelCase__	:






  """simple docstring"""


  def    lowercase_	(							self		:	int			,     __lowerCamelCase		:	list[list[float]]			,     __lowerCamelCase		:	list[int]      )	->							int:
       SCREAMING_SNAKE_CASE__					=					0.0
       SCREAMING_SNAKE_CASE__					=					0.0
       for i in range(len(__lowerCamelCase      )      ):
            da += math.pow((sample[i] - weights[0][i])			,     2      )
            da += math.pow((sample[i] - weights[1][i])			,     2      )
            return 0 if da > da else 1
       return 0







  def    lowercase_	(							self		:	Optional[int]			,     __lowerCamelCase		:	list[list[int | float]]			,     __lowerCamelCase		:	list[int]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	float      )	->							list[list[int | float]]:
       for i in range(len(__lowerCamelCase      )      ):
            weights[j][i] += alpha * (sample[i] - weights[j][i])
       return weights



def        UpperCAmelCase_     (		):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

     # weight initialization ( n, C )
     SCREAMING_SNAKE_CASE__					=					[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

     # training
     SCREAMING_SNAKE_CASE__					=					SelfOrganizingMap()
     SCREAMING_SNAKE_CASE__					=					3
     SCREAMING_SNAKE_CASE__					=					0.5

     for _ in range(_A       ):
          for j in range(len(_A       )       ):
               # training sample
               SCREAMING_SNAKE_CASE__					=					training_samples[j]

               # Compute the winning vector
               SCREAMING_SNAKE_CASE__					=					self_organizing_map.get_winner(_A   ,	_A       )

               # Update the winning vector
               SCREAMING_SNAKE_CASE__					=					self_organizing_map.update(_A   ,	_A   ,	_A   ,	_A       )

    # classify test sample
     SCREAMING_SNAKE_CASE__					=					[0, 0, 0, 1]
     SCREAMING_SNAKE_CASE__					=					self_organizing_map.get_winner(_A   ,	_A       )

     # results
     print(F'''Clusters that the test sample belongs to : {winner}'''       )
     print(F'''Weights that have been trained : {weights}'''       )


# running the main() function
if __name__ == "__main__":
       main()


