Skip to content

Instantly share code, notes, and snippets.

View Ventsislav-Yordanov's full-sized avatar

Ventsislav Yordanov Ventsislav-Yordanov

View GitHub Profile
from typing import List
Vector = List[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
print(new_vector)
def greeting(name: str) -> str:
return 'Hello ' + name
print(greeting("Ventsi"))
print(greeting(42))
def capitalize_sentences(sentences):
result = sentences.copy()
for index, item in enumerate(result):
result[index] = item.capitalize()
return result
sentences = ["this is test sentence", "I love Python", "positive thinking is nice!"]
print("Before calling the function:", sentences)
capitalized_sentences = capitalize_sentences(sentences)
print("After calling the function:", sentences)
def check_anagrams(word1, word2):
"""
Check if the two passed words are anagrams.
Returns True if the word1 and word2 are anagrams, otherwise returns False
"""
assert type(word1) == str, "The word1 must be a string"
assert type(word2) == str, "The word2 must be a string"
return sorted(word1) == sorted(word2)
def check_anagrams(word1, word2):
"""
Check if the two passed words are anagrams.
Returns True if the word1 and word2 are anagrams, otherwise returns False
"""
assert type(word1) == str
assert type(word12) == str
return sorted(word1) == sorted(word2)
def capitalize_sentences(sentences):
for index, item in enumerate(sentences):
sentences[index] = item.capitalize()
sentences = ["this is test sentence", "I love Python", "positive thinking is nice!"]
print("Before calling the function:", sentences)
capitalize_sentences(sentences)
print("After calling the function:", sentences)
try:
file = open("filename.txt")
file.write("Python is awesome!")
except:
print("An error occured when writing to the file")
finally:
file.close()
try:
print(sorted([1, 2, 3]))
except Exception as error:
print("Something went wrong")
print(error)
else:
print("Nothing went wrong")
try:
print(sorted(1))
except Exception as error:
print("Something went wrong")
print(error)
else:
print("Nothing went wrong")
try:
print(int("Hello"))
except ValueError as error:
# Use your logger to log the error
# Example:
# logger.error(error)