Skip to content

Commit e9c2711

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent b166b82 commit e9c2711

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Kata - Find Nearest Fibonacci Number
2+
3+
completed at: 2024-09-10 10:51:31
4+
by: Jakub Červinka
5+
6+
Given a positive integer (n) find the nearest fibonacci number to (n).
7+
8+
If there are more than one fibonacci with equal distance to the given number return the smallest one.
9+
10+
Do it in a efficient way. 5000 tests with the input range 1 <= n <= 2^512 should not exceed 200 ms.
11+
12+
"""
13+
14+
def nearest_fibonacci(number):
15+
a, b = 0, 1
16+
while b < number:
17+
a, b = b, a + b
18+
a_num, num_b = number - a, b - number
19+
return a if a_num <= num_b else b
20+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Kata - Splitting the Workload (part I)
2+
3+
completed at: 2024-09-10 09:50:23
4+
by: Jakub Červinka
5+
6+
Imagine you have a number of jobs to execute. Your workers are not permanently connected to your network, so you have to distribute all your jobs in the beginning. Luckily, you know exactly how long each job is going to take.
7+
8+
Let
9+
```
10+
x = [2,3,4,6,8,2]
11+
```
12+
be the amount of time each of your jobs is going to take.
13+
14+
Your task is to write a function ```splitlist(x)```, that will return two lists ```a``` and ```b```, such that ```abs(sum(a)-sum(b))``` is minimised.
15+
16+
One possible solution to this example would be
17+
```
18+
a=[2, 4, 6]
19+
b=[3, 8, 2]
20+
```
21+
with ```abs(sum(a)-sum(b)) == 1```.
22+
23+
The order of the elements is not tested, just make sure that you minimise ```abs(sum(a)-sum(b))``` and that ```sorted(a+b)==sorted(x)```.
24+
25+
You may assume that ```len(x)<=15``` and ```0<=x[i]<=100```.
26+
27+
When done, please continue with [part II](https://www.codewars.com/kata/586e6b54c66d18ff6c0015cd)!
28+
29+
30+
"""
31+
32+
import itertools
33+
34+
def split_list(x):
35+
n = len(x)
36+
37+
# edge case
38+
if n == 1:
39+
return x, []
40+
41+
best_diff = float('inf')
42+
best_a, best_b = [], []
43+
44+
for i in range(1, n // 2 + 1): # only need to check half of the list length
45+
for subset in itertools.combinations(x, i):
46+
a = list(subset)
47+
48+
b = x.copy()
49+
for elem in a:
50+
b.remove(elem)
51+
52+
diff = abs(sum(a) - sum(b))
53+
54+
if diff < best_diff:
55+
best_diff = diff
56+
best_a, best_b = a, b
57+
58+
if best_diff == 0:
59+
return best_a, best_b
60+
print(best_a, best_b)
61+
return best_a, best_b
62+

output/sum_of_intervals.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""Kata - Sum of Intervals
2+
3+
completed at: 2024-09-10 10:32:42
4+
by: Jakub Červinka
5+
6+
Write a function called `sumIntervals`/`sum_intervals` that accepts an array of intervals, and returns the sum of all the interval lengths. Overlapping intervals should only be counted once.
7+
8+
### Intervals
9+
10+
Intervals are represented by a pair of integers in the form of an array. The first value of the interval will always be less than the second value. Interval example: `[1, 5]` is an interval from `1` to `5`. The length of this interval is `4`.
11+
12+
### Overlapping Intervals
13+
14+
List containing overlapping intervals:
15+
16+
```c -- actual language id doesn't matter -- just want syntax highlighting
17+
[
18+
[1, 4],
19+
[7, 10],
20+
[3, 5]
21+
]
22+
```
23+
24+
The sum of the lengths of these intervals is `7`. Since `[1, 4]` and `[3, 5]` overlap, we can treat the interval as `[1, 5]`, which has a length of `4`.
25+
26+
### Examples:
27+
28+
```c -- idem
29+
sumIntervals( [
30+
[1, 2],
31+
[6, 10],
32+
[11, 15]
33+
] ) => 9
34+
35+
sumIntervals( [
36+
[1, 4],
37+
[7, 10],
38+
[3, 5]
39+
] ) => 7
40+
41+
sumIntervals( [
42+
[1, 5],
43+
[10, 20],
44+
[1, 6],
45+
[16, 19],
46+
[5, 11]
47+
] ) => 19
48+
49+
sumIntervals( [
50+
[0, 20],
51+
[-100000000, 10],
52+
[30, 40]
53+
] ) => 100000030
54+
```
55+
56+
### Tests with large intervals
57+
58+
Your algorithm should be able to handle large intervals. All tested intervals are subsets of the range `[-1000000000, 1000000000]`.
59+
60+
~~~if:lambdacalc
61+
### Encodings
62+
63+
purity: `LetRec`
64+
numEncoding: `ScottBinary` ( subsets are actually in range `[0, 2000]` )
65+
export constructor `Pair` for your `Pair` encoding
66+
export constructors `nil, cons` for your `List` encoding
67+
~~~
68+
69+
"""
70+
71+
def sum_of_intervals(intervals):
72+
intervals.sort(key=lambda x: x[0])
73+
first = intervals[0]
74+
if len(intervals) == 1:
75+
return first[1] - first[0]
76+
77+
merged_intervals = [list(first), ]
78+
79+
for i_start, i_end in intervals[1:]:
80+
for i, (ci_start, ci_end) in enumerate(merged_intervals):
81+
starts_in = ci_start <= i_start <= ci_end
82+
ends_in = ci_start <= i_end <= ci_end
83+
if starts_in or ends_in:
84+
ci_start = min(ci_start, i_start)
85+
ci_end = max(ci_end, i_end)
86+
merged_intervals[i] = [ci_start, ci_end]
87+
break
88+
else:
89+
merged_intervals.append([i_start, i_end])
90+
91+
total = sum(
92+
b - a
93+
for a, b in merged_intervals
94+
)
95+
return total

0 commit comments

Comments
 (0)