Dmitry Telenkov hai 1 ano
pai
achega
c8c2a4d69b
Modificáronse 1 ficheiros con 33 adicións e 5 borrados
  1. 33 5
      courses/python_for_begginers/func.py

+ 33 - 5
courses/python_for_begginers/func.py

@@ -214,7 +214,7 @@ def add_numbers(a: int, b: int | float | str) -> int:
 
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
 
-# from typing import List, Dict, Tuple, Optional, Any, Union
+from typing import List, Dict, Tuple, Optional, Any, Union
 
 def list_upper(lst: List[str]):
 	for elem in lst:
@@ -226,9 +226,37 @@ def list_upper(lst: List[str]):
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
 
 def first_repeated_word(s: str):
+	"""Находит первый дубль в строке
+
+	Args:
+		s (str): строка
+
+	Returns:
+		str: первое повторяющееся слово
+	"""
 	l = s.split()
-	for i in l:
-		print(i)
+	d_index = dict()
+
+	for i in range(len(l)):
+		if (l.count(l[i]) > 1) and l[i] not in d_index.values():
+			d_index.update({l.index(l[i], i+1, len(l)) : l[i]})
+	
+	if len(d_index) == 0:
+		return None
+
+	m = len(l)
+	if m == 0:
+		return None
+
+	for key,value in d_index.items():
+		if key < m:
+			m = key
 
-first_repeated_word("ab ca bc Ab cA aB bc")
-	
+	return d_index.get(m)
+
+assert first_repeated_word("ab ca bc ab") == "ab"
+assert first_repeated_word("ab ca bc Ab cA aB bc") == "bc"
+assert first_repeated_word("ab ca bc ca ab bc") == "ca"
+assert first_repeated_word("hello hi hello") == "hello"
+	
+print(first_repeated_word.__doc__)