

import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


_SCREAMING_SNAKE_CASE  :						Tuple         =    False
try:
       _SCREAMING_SNAKE_CASE  :						Union[str, Any]         =    _is_package_available('''google.colab''')
except ModuleNotFoundError:
       pass






@input.register
class  UpperCAmelCase__	:






  """simple docstring"""


  def __init__(							self		:	List[str]			,     __lowerCamelCase		:	str = None			,     __lowerCamelCase		:	list = []      )	->							int:
       SCREAMING_SNAKE_CASE__					=					0
       SCREAMING_SNAKE_CASE__					=					choices
       SCREAMING_SNAKE_CASE__					=					prompt
       if sys.platform == "win32":
            SCREAMING_SNAKE_CASE__					=					'''*'''
       else:
            SCREAMING_SNAKE_CASE__					=					'''➔ '''






  def    lowercase_	(							self		:	Any			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	str = ""      )	->							int:
       if sys.platform != "win32":
            writeColor(self.choices[index]			,     32			,     __lowerCamelCase      )
       else:
            forceWrite(self.choices[index]			,     __lowerCamelCase      )






  def    lowercase_	(							self		:	Optional[int]			,     __lowerCamelCase		:	int      )	->							Optional[Any]:
       if index == self.position:
            forceWrite(f''' {self.arrow_char} '''      )
            self.write_choice(__lowerCamelCase      )
       else:
            forceWrite(f'''    {self.choices[index]}'''      )
       reset_cursor()






  def    lowercase_	(							self		:	Tuple			,     __lowerCamelCase		:	Direction			,     __lowerCamelCase		:	int = 1      )	->							List[Any]:
       SCREAMING_SNAKE_CASE__					=					self.position
       if direction == Direction.DOWN:
            if self.position + 1 >= len(self.choices      ):
                 return
            self.position += num_spaces
       else:
            if self.position - 1 < 0:
                 return
            self.position -= num_spaces
       clear_line()
       self.print_choice(__lowerCamelCase      )
       move_cursor(__lowerCamelCase			,     direction.name      )
       self.print_choice(self.position      )






  @input.mark(KEYMAP['''up''']      )
  def    lowercase_	(							self		:	Any      )	->							List[str]:
       self.move_direction(Direction.UP      )






  @input.mark(KEYMAP['''down''']      )
  def    lowercase_	(							self		:	Optional[Any]      )	->							Optional[Any]:
       self.move_direction(Direction.DOWN      )






  @input.mark(KEYMAP['''newline''']      )
  def    lowercase_	(							self		:	Tuple      )	->							Union[str, Any]:
       move_cursor(len(self.choices      ) - self.position			,     '''DOWN'''      )
       return self.position






  @input.mark(KEYMAP['''interrupt''']      )
  def    lowercase_	(							self		:	Tuple      )	->							str:
       move_cursor(len(self.choices      ) - self.position			,     '''DOWN'''      )
       raise KeyboardInterrupt






  @input.mark_multiple(*[KEYMAP[str(__lowerCamelCase      )] for number in range(10      )]      )
  def    lowercase_	(							self		:	Dict      )	->							Union[str, Any]:
       SCREAMING_SNAKE_CASE__					=					int(chr(self.current_selection      )      )
       SCREAMING_SNAKE_CASE__					=					index - self.position
       if index == self.position:
            return
       if index < len(self.choices      ):
            if self.position > index:
                 self.move_direction(Direction.UP			,     -movement      )
            elif self.position < index:
                 self.move_direction(Direction.DOWN			,     __lowerCamelCase      )
            else:
                 return
       else:
            return







  def    lowercase_	(							self		:	Any			,     __lowerCamelCase		:	int = 0      )	->							List[str]:
       if self.prompt:
            linebreak()
            forceWrite(self.prompt			,     '''\n'''      )
            if in_colab:
                 forceWrite('''Please input a choice index (starting from 0), and press enter'''			,     '''\n'''      )
            else:
                 forceWrite('''Please select a choice using the arrow or number keys, and selecting with enter'''			,     '''\n'''      )
       SCREAMING_SNAKE_CASE__					=					default_choice
       for i in range(len(self.choices      )      ):
            self.print_choice(__lowerCamelCase      )
            forceWrite('''\n'''      )
       move_cursor(len(self.choices      ) - self.position			,     '''UP'''      )
       with cursor.hide():
            while True:
                 if in_colab:
                      try:
                           SCREAMING_SNAKE_CASE__					=					int(builtins.input()      )
                      except ValueError:
                           SCREAMING_SNAKE_CASE__					=					default_choice
                 else:
                      SCREAMING_SNAKE_CASE__					=					self.handle_input()
                 if choice is not None:
                      reset_cursor()
                      for _ in range(len(self.choices      ) + 1      ):
                           move_cursor(1			,     '''UP'''      )
                           clear_line()
                      self.write_choice(__lowerCamelCase			,     '''\n'''      )
                      return choice





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()


