Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highly Repetitive & Pattern Incrementing #4

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PyTest Compatible
  • Loading branch information
aflah02 committed Jun 10, 2023
commit bf554708efdf49907bf2aa69479d682fafca61fb
37 changes: 2 additions & 35 deletions filters/highly_repetitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,6 @@ def break_and_compare(ls: list, k: int) -> list:
ls (list): The input list.
k (int): The integer value used for splitting and comparing the list.

Returns:

A list based on the comparisons and operations performed.

Algorithm:
1. Get the length of the input list and assign it to variable `n`.
2. Reduce the value of `n` until it is divisible by `k` without any remainder. This ensures that `n` is a multiple of `k`.
3. Create a new list `to_break` containing the first `n` elements of the input list `ls`.
4. Create a new list `residual` containing the remaining elements of `ls` beyond the first `n` elements.
5. Calculate the chunk size by dividing `n` by `k` and assign it to variable `chunk_size`.
6. Enter a loop that continues until the length of `residual` is equal to the calculated `chunk_size`.
- Split the `to_break` list into chunks of size `chunk_size` using list slicing and list comprehension, and assign the result to the `chunks` variable.
- Set the `chunksMatch` variable to `True`.
- Iterate through each chunk in `chunks` starting from the second chunk.
- If the current chunk is not equal to the first chunk, set `chunksMatch` to `False` and break the loop.
- Check if `chunksMatch` is still `True`.
- If so, compare the `residual` list to the first chunk, up to the length of `residual`.
- If they are equal, return the first chunk as the result.
- Decrease the `chunk_size` by 1 to try a smaller chunk size.
- Calculate the new `new_residual` list by slicing `to_break` starting from `chunk_size * k`.
- Update `to_break` to contain only elements up to `chunk_size * k`.
- Update `residual` by concatenating `new_residual` with the previous `residual`.
7. If the loop completes without returning a result, return an empty list `[]`.
"""
n = len(ls)
while n % k != 0:
Expand Down Expand Up @@ -59,24 +36,14 @@ def break_and_compare(ls: list, k: int) -> list:
def break_and_compare_wrapper(ls: list, start_k: int, end_k: int) -> list:
"""

This function serves as a wrapper for the `break_and_compare` function. It takes an additional two integer parameters `start_k` and `end_k` to define a range of values for `k`. It iterates over this range and calls `break_and_compare` for each value of `k` within the range.
This function serves as a wrapper for the `break_and_compare` function. It takes an additional two integer parameters `start_k` and `end_k` to define a range of values for `k`.
It iterates over this range and calls `break_and_compare` for each value of `k` within the range.

Parameters:
- `ls` (list): The input list.
- `start_k` (int): The starting value of `k` for the range (inclusive).
- `end_k` (int): The ending value of `k` for the range (inclusive).

Returns:
- A tuple containing the result and the corresponding value of `k` if a result is found.
- If no result is found, it returns an empty list `[]` and -1.

Algorithm:
1. Convert the input list `ls` to a new list from any other iterable type.
2. Iterate over the range of values from `start_k` to `end_k` (inclusive) using a `for` loop with variable `k`.
3. Call the `break_and_compare` function with the input list `ls` and the current value of `k`, and assign the result to the `result` variable.
4. Check if the `result` is not an empty list (indicating a successful result).
- If so, return a tuple containing the `result` and the current value of `k`.
5. If no result is found within the range, return an empty list `[]` and -1 as a tuple.
"""
# end_k is inclusive
ls = list(ls)
Expand Down
64 changes: 0 additions & 64 deletions filters/highly_repetitive_tests.py

This file was deleted.

73 changes: 73 additions & 0 deletions filters/test_highly_repetitive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from .highly_repetitive import break_and_compare, break_and_compare_wrapper
# Test cases for break_and_compare

# Test case 1: Matching chunks exist
def test_break_and_compare_matching_chunks_exist():
ls = [1, 2, 3, 1, 2, 3, 1, 2, 3]
k = 3
expected = [1, 2, 3]
output = break_and_compare(ls, k)
assert output == expected, f"Test case 1 failed. Output: {output}, Expected: {expected}"

# Test case 2: No matching chunks
def test_break_and_compare_no_matching_chunks():
ls = [1, 2, 3, 4, 5, 6, 7]
k = 3
expected = []
output = break_and_compare(ls, k)
assert output == expected, f"Test case 2 failed. Output: {output}, Expected: {expected}"

# Test case 3: Empty list
def test_break_and_compare_empty_list():
ls = []
k = 4
expected = []
output = break_and_compare(ls, k)
assert output == expected, f"Test case 3 failed. Output: {output}, Expected: {expected}"

# Test case 4: Chunk size larger than list length
def test_break_and_compare_chunk_size_larger_than_list_length():
ls = [1, 2, 3]
k = 4
expected = []
output = break_and_compare(ls, k)
assert output == expected, f"Test case 4 failed. Output: {output}, Expected: {expected}"

# Test cases for break_and_compare_wrapper

# Test case 1: Matching chunks within the range
def test_break_and_compare_wrapper_matching_chunks_within_range():
ls = [1, 2, 3, 1, 2, 3, 1, 2, 3]
start_k = 2
end_k = 4
expected = ([1, 2, 3], 3)
output = break_and_compare_wrapper(ls, start_k, end_k)
assert output == expected, f"Test case 1 failed. Output: {output}, Expected: {expected}"

# Test case 2: No matching chunks within the range
def test_break_and_compare_wrapper_no_matching_chunks_within_range():
ls = [1, 2, 3, 4, 5, 6, 7]
start_k = 2
end_k = 5
expected = ([], -1)
output = break_and_compare_wrapper(ls, start_k, end_k)
assert output == expected, f"Test case 2 failed. Output: {output}, Expected: {expected}"

# Test case 3: Empty list with range
def test_break_and_compare_wrapper_empty_list_with_range():
ls = []
start_k = 1
end_k = 3
expected = ([], -1)
output = break_and_compare_wrapper(ls, start_k, end_k)
assert output == expected, f"Test case 3 failed. Output: {output}, Expected: {expected}"

# Test case 4: Single-element list with range
def test_break_and_compare_wrapper_single_element_list_with_range():
ls = [1]
start_k = 1
end_k = 3
expected = ([1], 1)
output = break_and_compare_wrapper(ls, start_k, end_k)
assert output == expected, f"Test case 4 failed. Output: {output}, Expected: {expected}"