this notebook shows some essentials and practical python codes to help in your coding test like hackerrank or codility
Two most important things
Prep
During the Test
Psychology
Next Step
In [13]:
from IPython.display import Image
Image("../img/big_o1.png", width=600)
Out[13]:
In [17]:
Image("../img/big_o2.png", width=800)
Out[17]:
In [21]:
Image("../img/big_o3.png", width=500)
Out[21]:
Example 1
In [ ]:
counter = 0
for item in query:
for item2 in query:
counter += 1
Example 2
In [ ]:
counter = 0
list1 = []
for item in query:
list1.append(item)
for item2 in query:
for item3 in query:
counter += 1
In [5]:
import cProfile
cProfile.run('print(10)')
In [2]:
set = set([1,1,2,2,4,5,6])
set
Out[2]:
In [3]:
# convert to list
list(set)
Out[3]:
In [13]:
sort = sorted([4,-1,23,5,6,7,1,4,5])
sort
Out[13]:
In [106]:
print([4,-1,23,5,6,7,1,4,5].sort())
In [32]:
# reverse
sort = sorted([4,1,23,5,6,7,1,4,5],reverse=True)
print(sort)
# OR
print(sort[::-1])
In [15]:
list1 = [1,2,3,4,5]
In [18]:
# last number
list1[-1]
Out[18]:
In [85]:
# get every 2nd feature
list1[::2]
Out[85]:
In [97]:
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(array[0])
print(array[-1])
print(array[0:2])
print(array[-3:-1])
In [100]:
# filling an empty array
empty = []
for i in range(10):
empty.append(i)
empty
Out[100]:
In [101]:
# remove item
empty.remove(1)
empty
Out[101]:
In [103]:
# sum
sum(empty)
Out[103]:
In [21]:
import math
In [23]:
list1 = [1,2,3,4,5]
print('max: ',max(list1))
print('min: ',min(list1))
In [34]:
abs(-10.1)
Out[34]:
In [63]:
'-'.join('abcdef')
Out[63]:
In [72]:
# individual split
[i for i in 'ABCDEFG']
Out[72]:
In [88]:
import textwrap
textwrap.wrap('ABCDEFG',2)
Out[88]:
In [71]:
import re
re.findall('.{1,2}', 'ABCDEFG')
Out[71]:
In [79]:
from itertools import permutations
# permutations but without order
list(permutations(['1','2','3'],2))
Out[79]:
In [76]:
# permutations but with order
from itertools import combinations
list(combinations([1,2,3], 2))
Out[76]:
In [62]:
test = 'a'
if test.isupper():
print('Upper')
elif test.islower():
print('Lower')
In [47]:
for i in range(5):
if i==2:
break
print(i)
In [46]:
for i in range(5):
if i==2:
continue
print(i)
In [48]:
for i in range(5):
if i==2:
pass
print(i)
In [108]:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
In [ ]: