TelenkovDmitry пре 3 година
родитељ
комит
277426a147
3 измењених фајлова са 65 додато и 6 уклоњено
  1. 28 0
      misc.py
  2. 10 0
      squqre_sum.py
  3. 27 6
      to_camel_case.py

+ 28 - 0
misc.py

@@ -0,0 +1,28 @@
+
+# Инвертирует строку
+from ast import IsNot
+
+
+def solution(string):
+    return string[::-1]
+
+
+#print(solution("qwerty"))
+
+# ------------------------------------------------------------------------------- #
+
+
+def unique_in_order(iterable):
+    s = set(iterable)
+    l = []
+    for i in iterable:
+        if i in s:
+            l.append(i)
+            s.remove(i)
+    
+    return l
+
+    
+
+
+print(unique_in_order("qwereteyty"))

+ 10 - 0
squqre_sum.py

@@ -0,0 +1,10 @@
+#Complete the square sum function so that it squares each number passed into it 
+#and then sums the results together.
+
+#For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9.
+
+def square_sum(numbers):
+    return sum(i*i for i in numbers)
+
+print(square_sum([1, 2, 2]))
+print(square_sum([-1, -2]))

+ 27 - 6
py_task.py → to_camel_case.py

@@ -5,31 +5,47 @@
 #"the-stealth-warrior" gets converted to "theStealthWarrior"
 #"The_Stealth_Warrior" gets converted to "TheStealthWarrior"
 
+
+# 'if s else s' - 'защита от пустой строки'
+# title - преобразует символы в верхний регистр
+# translate - заменяет символы
 def to_camel_case(s):
-    return s[0] + s.title().translate(None, "-_")[1:] if s else s
+    #return s[0] + s.title().translate(None, "-_")[1:] if s else s # Так не работает
+    return s[0] + s.title().translate({ord('_'):None, ord('-'):None})[1:] if s else s
 
 
+'''
+# replace - замена
+# Интересный for по списку и join
 def to_camel_case(text):
     removed = text.replace('-', ' ').replace('_', ' ').split()
     if len(removed) == 0:
         return ''
     return removed[0]+ ''.join([x.capitalize() for x in removed[1:]])
+'''
 
-
+'''
+# Как и в первой функции только без защиты от пустой строки
 def to_camel_case(text):
     return text[:1] + text.title()[1:].replace('_', '').replace('-', '')
+'''
 
-
+'''
+# Разобраться с регулярками
 import re
 def to_camel_case(text):
     return re.sub('[_-](.)', lambda x: x.group(1).upper(), text)
+'''
 
-
+'''
+# Микс из первого и второго варианта
 def to_camel_case(text):
     words = text.replace('_', '-').split('-')
     return words[0] + ''.join([x.title() for x in words[1:]])
+'''
 
-
+'''
+# Перебор по символу
 def to_camel_case(text):
     cap = False
     newText = ''
@@ -43,8 +59,13 @@ def to_camel_case(text):
             newText = newText + t
             cap = False
     return newText
+'''
 
-
+'''
+# Мозгодробительный вариант
 def to_camel_case(text):
     return "".join([i if n==0 else i.capitalize() for n,i in enumerate(text.replace("-","_").split("_"))])
+'''
 
+print(to_camel_case("the-stealth-warrior"))
+print(to_camel_case("The_Stealth_Warrior"))