"""simple docstring"""







import itertools
import string
from collections.abc import Generator, Iterable
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Iterable[str]      ,							UpperCAmelCase_   :    int  )							->      Generator[tuple[str, ...], None, None]:

							'''simple docstring'''





							__snake_case      :							Optional[int]						=			iter(UpperCAmelCase_  )
							while True:
														__snake_case      :							Optional[int]						=			tuple(itertools.islice(UpperCAmelCase_      ,							UpperCAmelCase_  )  )
														if not chunk:
																					return
														yield chunk
def   __UpperCAmelCase    (   UpperCAmelCase_   :    str  )							->      str:

							'''simple docstring'''





							__snake_case      :							Any						=			''.join([c.upper() for c in dirty if c in string.ascii_letters]  )
							__snake_case      :							Union[str, Any]						=			''

							if len(UpperCAmelCase_  ) < 2:
														return dirty

							for i in range(len(UpperCAmelCase_  ) - 1  ):
														clean += dirty[i]

														if dirty[i] == dirty[i + 1]:
																					clean += "X"

							clean += dirty[-1]

							if len(UpperCAmelCase_  ) & 1:
														clean += "X"

							return clean
def   __UpperCAmelCase    (   UpperCAmelCase_   :    str  )							->      list[str]:

							'''simple docstring'''





							__snake_case      :							List[str]						=			'ABCDEFGHIKLMNOPQRSTUVWXYZ'
							# we're using a list instead of a '2d' array because it makes the math
							# for setting up the table and doing the actual encoding/decoding simpler
							__snake_case      :							Optional[int]						=			[]

							# copy key chars into the table if they are in `alphabet` ignoring duplicates
							for char in key.upper():
														if char not in table and char in alphabet:
																					table.append(UpperCAmelCase_  )

    # fill the rest of the table in with the remaining alphabet chars
							for char in alphabet:
														if char not in table:
																					table.append(UpperCAmelCase_  )

							return table
def   __UpperCAmelCase    (   UpperCAmelCase_   :    str      ,							UpperCAmelCase_   :    str  )							->      str:

							'''simple docstring'''





							__snake_case      :							str						=			generate_table(UpperCAmelCase_  )
							__snake_case      :							Union[str, Any]						=			prepare_input(UpperCAmelCase_  )
							__snake_case      :							Tuple						=			''

							# https://en.wikipedia.org/wiki/Playfair_cipher#Description
							for chara, chara in chunker(UpperCAmelCase_      ,							2  ):
														__snake_case      :							Any						=			divmod(table.index(UpperCAmelCase_  )      ,							5  )
														__snake_case      :							Tuple						=			divmod(table.index(UpperCAmelCase_  )      ,							5  )

														if rowa == rowa:
																					ciphertext += table[rowa * 5 + (cola + 1) % 5]
																					ciphertext += table[rowa * 5 + (cola + 1) % 5]
														elif cola == cola:
																					ciphertext += table[((rowa + 1) % 5) * 5 + cola]
																					ciphertext += table[((rowa + 1) % 5) * 5 + cola]
														else:  # rectangle
																					ciphertext += table[rowa * 5 + cola]
																					ciphertext += table[rowa * 5 + cola]

							return ciphertext

def   __UpperCAmelCase    (   UpperCAmelCase_   :    str      ,							UpperCAmelCase_   :    str  )							->      str:

							'''simple docstring'''





							__snake_case      :							Optional[int]						=			generate_table(UpperCAmelCase_  )
							__snake_case      :							Any						=			''

							# https://en.wikipedia.org/wiki/Playfair_cipher#Description
							for chara, chara in chunker(UpperCAmelCase_      ,							2  ):
														__snake_case      :							Union[str, Any]						=			divmod(table.index(UpperCAmelCase_  )      ,							5  )
														__snake_case      :							Tuple						=			divmod(table.index(UpperCAmelCase_  )      ,							5  )

														if rowa == rowa:
																					plaintext += table[rowa * 5 + (cola - 1) % 5]
																					plaintext += table[rowa * 5 + (cola - 1) % 5]
														elif cola == cola:
																					plaintext += table[((rowa - 1) % 5) * 5 + cola]
																					plaintext += table[((rowa - 1) % 5) * 5 + cola]
														else:  # rectangle
																					plaintext += table[rowa * 5 + cola]
																					plaintext += table[rowa * 5 + cola]

							return plaintext






"""simple docstring"""







import numpy

# List of input, output pairs
_a					:					Optional[int]=			(
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
_a					:					int=			(((515, 22, 13), 555), ((61, 35, 49), 150))
_a					:					List[Any]=			[2, 4, 1, 5]
_a					:					Optional[int]=			len(train_data)
_a					:					str=			0.0_0_9
def   __UpperCAmelCase    (   UpperCAmelCase_   :    List[Any]      ,							UpperCAmelCase_   :    List[str]="train"  )							->      int:

	'''simple docstring'''





	return calculate_hypothesis_value(UpperCAmelCase_      ,							UpperCAmelCase_  ) - output(
	    UpperCAmelCase_      ,							UpperCAmelCase_  )
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Tuple  )							->      Dict:

	'''simple docstring'''





	__snake_case      :							int						=			0
	for i in range(len(UpperCAmelCase_  ) - 1  ):
		hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
	hyp_val += parameter_vector[0]
	return hyp_val
def   __UpperCAmelCase    (   UpperCAmelCase_   :    str      ,							UpperCAmelCase_   :    List[Any]  )							->      List[Any]:

	'''simple docstring'''





	if data_set == "train":
		return train_data[example_no][1]
	elif data_set == "test":
		return test_data[example_no][1]
	return None
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Optional[Any]      ,							UpperCAmelCase_   :    Optional[int]  )							->      Any:

	'''simple docstring'''





	if data_set == "train":
		return _hypothesis_value(train_data[example_no][0]  )
	elif data_set == "test":
		return _hypothesis_value(test_data[example_no][0]  )
	return None
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Union[str, Any]      ,							UpperCAmelCase_   :    Any=m  )							->      Union[str, Any]:

	'''simple docstring'''





	__snake_case      :							Union[str, Any]						=			0
	for i in range(UpperCAmelCase_  ):
		if index == -1:
			summation_value += _error(UpperCAmelCase_  )
		else:
			summation_value += _error(UpperCAmelCase_  ) * train_data[i][0][index]
	return summation_value
def   __UpperCAmelCase    (   UpperCAmelCase_   :    int  )							->      str:

	'''simple docstring'''





	__snake_case      :							Dict						=			summation_of_cost_derivative(UpperCAmelCase_      ,							UpperCAmelCase_  ) / m
	return cost_derivative_value
def   __UpperCAmelCase    (   )							->      List[str]:

	'''simple docstring'''





	global parameter_vector
	# Tune these values to set a tolerance value for predicted output
	__snake_case      :							Dict						=			0.000_002
	__snake_case      :							Optional[int]						=			0
	__snake_case      :							str						=			0
	while True:
		j += 1
		__snake_case      :							Optional[int]						=			[0, 0, 0, 0]
		for i in range(0      ,							len(UpperCAmelCase_  )  ):
			__snake_case      :							Union[str, Any]						=			get_cost_derivative(i - 1  )
			__snake_case      :							Any						=			(
			    parameter_vector[i] - LEARNING_RATE * cost_derivative
			)
		if numpy.allclose(
		    UpperCAmelCase_      ,							UpperCAmelCase_      ,							atol=UpperCAmelCase_      ,							rtol=UpperCAmelCase_      ,							):
			break
		__snake_case      :							Optional[Any]						=			temp_parameter_vector
	print(('Number of iterations:', j)  )
def   __UpperCAmelCase    (   )							->      int:

	'''simple docstring'''





	for i in range(len(UpperCAmelCase_  )  ):
		print(('Actual output value:', output(UpperCAmelCase_      ,							'test'  ))  )
		print(('Hypothesis output:', calculate_hypothesis_value(UpperCAmelCase_      ,							'test'  ))  )


if __name__ == "__main__":
	run_gradient_descent()
	print("\nTesting gradient descent for a linear hypothesis function.\n")
	test_gradient_descent()





