Skip to content

Commit 0617ba7

Browse files
committed
Add exercises from Codility
1 parent e6d9066 commit 0617ba7

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

src/binary_period.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def calc_binary_period(n):
2+
binary_digits = [0] * 30
3+
l = 0
4+
while (n > 0):
5+
binary_digits[l] = n % 2
6+
n //= 2
7+
l += 1
8+
for p in range(1, 1 + l):
9+
ok = True
10+
for i in range(l//2):
11+
if binary_digits[l-i] != binary_digits[l - i - p]:
12+
ok = False
13+
break
14+
if ok:
15+
return p
16+
return -1

src/ex_phonebill.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Challenge from past interview
3+
'''
4+
import re
5+
6+
def calc_bill_value(S):
7+
'''
8+
Calculate the Bill value for a fictions phone call register list
9+
Length call, phone Number in the format "HH:MM:SS,000-000-000\n"
10+
Conditions
11+
1. time less then 5 min : cost 3 cents/s
12+
2. time greater then 5 min : cost 150 cents/min
13+
3. all the calls from the number longest call time overall are free
14+
:param S:
15+
:return:
16+
'''
17+
18+
value = 0
19+
20+
# Convert Data to (hour,minute,second,phonenumber)
21+
regexpdata = r'([0-9][0-9]):([0-9][0-9]):([0-9][0-9]),([0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9])'
22+
data = re.findall(regexpdata, S)
23+
24+
ddata = {}
25+
# Store Phone with longest call time
26+
phonemaxcalls = ""
27+
timemaxcalls = 0
28+
# Get Duration
29+
for item in data:
30+
# Convert Time to s
31+
timeSeconds = int(item[0]) * 3600 + int(item[1]) * 60 + int(item[2])
32+
33+
# Add Time to phonenumber register
34+
if item[3] not in ddata:
35+
ddata[item[3]] = timeSeconds
36+
else:
37+
ddata[item[3]] += timeSeconds
38+
39+
# Get Longest Call time
40+
if ddata[item[3]] > timemaxcalls:
41+
phonemaxcalls = item[3]
42+
timemaxcalls = ddata[item[3]]
43+
# elif ddata[item[3]] == timemaxcalls:
44+
# # Check smallest Number in case of tier if the right number is needed to show up in the bill
45+
# if item[3] < phonemaxcalls:
46+
# phonemaxcalls = item[3]
47+
# timemaxcalls = ddata[item[3]]
48+
49+
# Remove Longest Call time (dont check smallest tier number since the value it dont change de value)
50+
del (ddata[phonemaxcalls])
51+
52+
# Generate Bill
53+
for phonenumber, time in ddata.items():
54+
pricecall = 0
55+
if time < 300:
56+
pricecall = time * 3
57+
else:
58+
pricecall = (time // 60 + 1) * 150
59+
60+
value += pricecall
61+
62+
# Return
63+
return value

src/local_maxmin.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Find local max and min
3+
@author Akafael
4+
"""
5+
6+
def count_local_minmax(A):
7+
"""
8+
Count local max and min
9+
@author Akafael
10+
"""
11+
12+
n = len(A)
13+
14+
# Trivial Solution
15+
if n <= 1:
16+
return 1
17+
18+
# Calculate the diff
19+
B = []
20+
count = 0
21+
isRising = False
22+
isFalling = False
23+
for i in range(1, n - 1):
24+
B.append(A[i + 1] - A[i])
25+
26+
# Count Pikes
27+
if B[i] > 0:
28+
isRising = True
29+
if isFalling == True:
30+
count += 1
31+
isFalling == False
32+
elif B[i] < 0:
33+
isFalling = True
34+
if isRising == True:
35+
count += 1
36+
isRising == False
37+
38+
return count

src/pythonic_data_structures.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# List Comprehensions
2+
3+
# Generate a list with square numbers from 0 to 10
4+
squares = [x ** 2 for x in range(10)]
5+
6+
# List with element that are not equal to 4
7+
not4s = [x for x in squares if x != 4]

src/template.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
3+
4+
def solution(A):
5+
"""
6+
Solution
7+
:type A: object
8+
"""
9+
return A
10+
11+
12+
class TestSolution(unittest.TestCase):
13+
14+
def test_example1(self):
15+
self.assertEqual(5, solution(5))
16+
17+
18+
if __name__ == '__main__':
19+
unittest.main()
20+
solution(1)

0 commit comments

Comments
 (0)