Skip to content

Instantly share code, notes, and snippets.

@klimach
Created September 20, 2018 13:23
Show Gist options
  • Save klimach/f3d9f1b3b693f5f44469b6b57a70043e to your computer and use it in GitHub Desktop.
Save klimach/f3d9f1b3b693f5f44469b6b57a70043e to your computer and use it in GitHub Desktop.
Week0/Homework1 Alexandr Klimach
# Task 2/1
myList = list(range(1,30,3))
print(myList)
# Task 2/2
myList_2 = myList.copy()
myList_2.append(1)
myList_2.append(5)
myList_2.append(13)
myList_2.append(20)
print(myList_2)
# Task 2/3
myList_3 = set(myList_2)
print(myList_3)
# Task 2/4
def compare_elements(a:list, b:set):
if len(a) > len(b):
return print('List is bigger')
else:
return print('Set is bigger')
compare_elements(myList_2, myList_3)
# Task 3
def get_value_from_list(data:list, index:int):
try:
return data[index]
except:
return None
print(get_value_from_list(myList, 2))
print(get_value_from_list(myList, 22))
# Task 4
def create_dict(name:str, age:int, hobby:str):
cdict = {}
cdict['Name'] = name
cdict['Age'] = age
cdict['Hobby'] = hobby
return cdict
print(create_dict('Alex', 23, 'Music'))
# Advanced level
# Fibonacci
def fibo(n:int):
fibo = []
x = 0
y = 1
for i in range(n):
x, y = y, x + y
fibo.append(x)
return fibo
print(fibo(20))
# Matrix
def create_matrix(first_element:int, rows:int, columns:int):
# Classical matrix
a = []
for r in range(rows):
a.append([])
for c in range(columns):
a[r].append(first_element)
first_element += 1
for r in a:
print(r)
# Decorative matrix
def row_sharpe():
csharpe = '##'
for c in range(columns):
csharpe += '###'
print(csharpe)
row_sharpe()
for r in a:
nums = ''
for c in r:
if c <= 9:
nums += ' ' + str(c) + ' '
else:
nums += str(c) + ' '
print('#' + nums + '#')
row_sharpe()
create_matrix(7,4,4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment