Skip to content

Commit ab72e6e

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent 3ed4f51 commit ab72e6e

4 files changed

+231
-0
lines changed

output/binary_contiguous_array.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Kata - Binary Contiguous Array
2+
3+
completed at: 2024-04-25 19:53:56
4+
by: Jakub Červinka
5+
6+
An array consisting of `0`s and `1`s, also called a binary array, is given as an input.
7+
8+
### Task
9+
10+
Find the length of the longest contiguous subarray which consists of **equal** number of `0`s and `1`s.
11+
12+
### Example
13+
14+
```
15+
s = [1,1,0,1,1,0,1,1]
16+
|_____|
17+
|
18+
[0,1,1,0]
19+
20+
length = 4
21+
```
22+
23+
### Note
24+
25+
<!-- this should show the first block for every language except LC -->
26+
```c
27+
0 <= length(array) < 120 000
28+
```
29+
```lambdacalc
30+
0 <= length list <= 25
31+
```
32+
33+
~~~if:lambdacalc
34+
### Encodings
35+
36+
purity: `LetRec`
37+
numEncoding: `BinaryScott`
38+
export constructors `nil, cons` for your `List` encoding
39+
~~~
40+
"""
41+
42+
def binarray(arr):
43+
count = 0
44+
map = {0: -1}
45+
max_length = 0
46+
47+
arr = [1 if x else -1 for x in arr]
48+
49+
for i, number in enumerate(arr):
50+
count += number
51+
52+
if count in map:
53+
max_length = max(max_length, (i - map[count]))
54+
else:
55+
map[count] = i
56+
57+
return max_length
58+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Kata - Calculate the function f(x) for a simple linear sequence (Medium)
2+
3+
completed at: 2024-04-25 18:09:00
4+
by: Jakub Červinka
5+
6+
This is a follow-up from my previous Kata which can be found here: http://www.codewars.com/kata/5476f4ca03810c0fc0000098
7+
8+
This time, for any given linear sequence, calculate the function [f(x)] and return it as a function in Javascript or Lambda/Block in Ruby.
9+
10+
For example:
11+
12+
```javascript
13+
getFunction([0, 1, 2, 3, 4])(5) => 5
14+
getFunction([0, 3, 6, 9, 12])(10) => 30
15+
getFunction([1, 4, 7, 10, 13])(20) => 61
16+
```
17+
18+
```ruby
19+
get_function([0, 1, 2, 3, 4]).call(5) => 5
20+
get_function([0, 3, 6, 9, 12]).call(10) => 30
21+
get_function([1, 4, 7, 10, 13]).call(20) => 61
22+
```
23+
24+
```python
25+
get_function([0, 1, 2, 3, 4])(5) => 5
26+
get_function([0, 3, 6, 9, 12])(10) => 30
27+
get_function([1, 4, 7, 10, 13])(20) => 61
28+
```
29+
30+
```csharp
31+
GetFunction(new[] { 0, 1, 2, 3, 4 })(5) => 5
32+
GetFunction(new[] { 0, 3, 6, 9, 12 })(10) => 30
33+
GetFunction(new[] { 1, 4, 7, 10, 13 })(20) => 61
34+
```
35+
36+
```php
37+
get_function([0, 1, 2, 3, 4])(5) // => 5
38+
get_function([0, 3, 6, 9, 12])(10) // => 30
39+
get_function([1, 4, 7, 10, 13])(20) // => 61
40+
```
41+
42+
Assumptions for this kata are:
43+
```
44+
The sequence argument will always contain 5 values equal to f(0) - f(4).
45+
The function will always be in the format "nx +/- m", 'x +/- m', 'nx', 'x' or 'm'
46+
If a non-linear sequence simply return 'Non-linear sequence' for javascript, ruby, and python. For C#, throw an ArgumentException.
47+
```
48+
"""
49+
50+
from itertools import pairwise
51+
52+
def get_function(seq):
53+
dif = seq[1] - seq[0]
54+
for a, b in pairwise(seq):
55+
if b - a != dif:
56+
return "Non-linear sequence"
57+
return lambda x: seq[0] + dif * x
58+
59+

output/element_equals_its_index.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Kata - Element equals its index
2+
3+
completed at: 2024-04-25 19:08:20
4+
by: Jakub Červinka
5+
6+
```if:python
7+
Given a sorted array of distinct integers, write a function `index_equals_value` that returns the lowest index for which `array[index] == index`.
8+
Return `-1` if there is no such index.
9+
```
10+
```if:haskell,javascript
11+
Given a sorted array of distinct integers, write a function `indexEqualsValue` that returns the lowest index for which `array[index] == index`.
12+
Return `-1` if there is no such index.
13+
```
14+
```if:rust
15+
Given a sorted slice of distinct integers, write a function `index_equals_value` that returns the lowest index for which `slice[index] == index`.
16+
Return `-1` if there is no such index.
17+
```
18+
19+
Your algorithm should be very performant.
20+
21+
[input] array of integers ( with `0`-based nonnegative indexing )
22+
[output] integer
23+
24+
### Examples:
25+
26+
<!-- no need to add more languages really. but leave "python" in or words get coloured -->
27+
```python
28+
input: [-8,0,2,5]
29+
output: 2 # since array[2] == 2
30+
31+
input: [-1,0,3,6]
32+
output: -1 # since no index in array satisfies array[index] == index
33+
```
34+
### Random Tests Constraints:
35+
36+
```if:python,javascript
37+
Array length: 200 000
38+
```
39+
```if:rust
40+
Slice length: 200 000
41+
```
42+
```if:haskell
43+
Array length: 10 000
44+
```
45+
46+
Amount of tests: 1 000
47+
48+
```if:python
49+
Time limit: 1.5 s
50+
```
51+
```if:haskell,javascript,rust
52+
Time limit: 150 ms
53+
```
54+
___
55+
56+
If you liked this Kata check out my more complex creations:
57+
58+
Find the neighbourhood in big dimensions. [Neighbourhood collection](https://www.codewars.com/collections/5b2f4db591c746349d0000ce)
59+
60+
The [Rubik's cube](https://www.codewars.com/kata/5b3bec086be5d8893000002e)
61+
"""
62+
63+
def index_equals_value(arr):
64+
left, right = 0, len(arr) - 1
65+
smallest_match = float('inf')
66+
67+
while left <= right:
68+
mid = left + (right - left) // 2
69+
if arr[mid] == mid:
70+
smallest_match = min(smallest_match, mid)
71+
right = mid - 1
72+
elif arr[mid] > mid:
73+
right = mid - 1
74+
else:
75+
left = mid + 1
76+
77+
return smallest_match if smallest_match != float('inf') else -1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Kata - Simple Sum (with restrictions)
2+
3+
completed at: 2024-04-25 19:20:50
4+
by: Jakub Červinka
5+
6+
## Task
7+
8+
You're given two non-negative numbers `(0 <= x,y)`. The goal is to create a function named `simple_sum` which return their sum.
9+
10+
## Restrictions
11+
12+
The restrictions can be summurized as 'bit and logical operations are allowed'.
13+
14+
Here comphrensive description. You're code __mustn't__ contain:
15+
16+
```
17+
1. `import`, `getattr`, `exec`, `eval`, `sum` # with next point produce short solutions,
18+
2. `int` class methods names # but here is another idea.
19+
3. `__loader__`, `vars`, `__dict__` # even more of them.
20+
4. `if`, `in`, `is` # they start with `i` :)
21+
5. `+`, `-`, `*`, `/`, `//`, `%` # arithmetic operations
22+
6. `==`, `!=`, `<`, `>`, `<=`, `>=` # comparisions
23+
```
24+
25+
## Note
26+
27+
**Inspired by [suic](https://www.codewars.com/users/553bfb4e8234ef15340000b9)'s katas**
28+
29+
**Please assess the difficulty of the kata when you finish it :)**
30+
"""
31+
32+
def simple_sum(a, b):
33+
while b:
34+
swc = a ^ b
35+
carry = (a & b) << 1
36+
a, b = swc, carry
37+
return a

0 commit comments

Comments
 (0)