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

Feat: Add list-processing algorithms #28

Merged
merged 33 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
da01346
sorting containter
KellerLiptrap Apr 26, 2024
0c08514
Searching list functions
Barker0103 Apr 26, 2024
6448fc7
sorting
KellerLiptrap Apr 26, 2024
20c30b1
type annotations for sorting
KellerLiptrap Apr 26, 2024
a4a3f46
type annotations
Barker0103 Apr 26, 2024
0dcc362
Type annotations
Barker0103 Apr 26, 2024
d0164cf
chore: Add the basics.py file with all basic python functions.
dyga01 Apr 26, 2024
1d5dabc
chore: Add the basics.py file with all basic functions.
dyga01 Apr 26, 2024
2e916cd
added doc strings
KellerLiptrap Apr 26, 2024
54feecc
fix: re-formatted all test functions
dyga01 Apr 28, 2024
be4cc5e
list intersection
VitalJoseph Apr 28, 2024
7941e09
list symmetric difference
VitalJoseph Apr 28, 2024
9238499
list concatenation
VitalJoseph Apr 28, 2024
4be68b8
list elementwise addition
VitalJoseph Apr 28, 2024
d3ce32a
list pairwise multiplication
VitalJoseph Apr 28, 2024
3551bac
docstrings
VitalJoseph Apr 28, 2024
08316ce
reformat
VitalJoseph Apr 28, 2024
15c9467
Feat: Add combination algorithms
tuduun Apr 28, 2024
d282079
Merge branch 'main' into list-processor
simojo Apr 29, 2024
0fc171b
Merge branch 'main' into list-processor
simojo Apr 29, 2024
0b35260
Fix: Folder name
tuduun Apr 29, 2024
f4f739a
Add __init__.py
tuduun Apr 29, 2024
1e7cdda
Add test modules
tuduun Apr 29, 2024
ccfedbf
chore: using team fours sorting module
simojo Apr 29, 2024
9dc1727
fix: fixing errors lingering from other team
simojo Apr 29, 2024
76ffebd
fix: recursion encountered by using already named function
simojo Apr 29, 2024
9d6dd04
Merge branch 'main' into list-processor
tuduun Apr 29, 2024
8e0af9c
chore: removing poetry.toml
simojo Apr 29, 2024
3c94102
fix: duped dependencies
simojo Apr 29, 2024
23b6de5
fix: resolving minor linting errors
simojo Apr 29, 2024
e090059
Merge branch 'main' into list-processor
simojo Apr 29, 2024
90c3b57
Merge branch 'main' into list-processor
simojo Apr 29, 2024
4d24d27
doc: adding information about added algorithms in README
simojo Apr 29, 2024
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
Binary file modified .coverage
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.devenv/
.venv/
__pycache__/
poetry.toml
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ poetry run bosco --starting-size 100 --number-doubles 5 --file bosco/sorting.py

The command uses the following inputs:

- `--starting-size` for the initial list size to start the doubling experiment
- `--starting-size` for the initial list size to start the doubling experiment.
- `--number-doubles` for the number of times the input size will be doubled
during the doubling experiment
during the doubling experiment.
- `--file` for the path to the file containing the sorting algorithm you want to
run the doubling experiment on
run the doubling experiment on.
- `--function-name` for the name of the function containing the sorting
algorithm you want to test
algorithm you want to test.
- `--element-type` for the data type to fill the randomly generated containers.

This project supplies an array of Python files with list operations in them:

* `bosco/list_processing/basics.py`: basic list operations
* `bosco/list_processing/combination.py`: list operations involving combination
* `bosco/list_processing/search.py`: searching algorithms for lists
* `bosco/list_processing/sorting.py`: sorting algorithms for lists
* `bosco/list_processing/other.py`: other uncategorized operations

## Example of command and output

### Command
Expand Down
Empty file.
130 changes: 130 additions & 0 deletions bosco/list_processing/basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""Basic python functions."""

import random
from typing import List, Any


def get_first_item(lst: List[Any]) -> Any:
"""Returns the first item of the list if it exists, otherwise returns None."""
return lst[0] if lst else None


def get_last_item(lst: List[Any]) -> Any:
"""Returns the last item of the list if it exists, otherwise returns None."""
return lst[-1] if lst else None


def get_list_length(lst: List[Any]) -> int:
"""Returns the length of the list."""
return len(lst)


def reverse_list(lst: List[Any]) -> List[Any]:
"""Returns a new list that is the reverse of the input list."""
return lst[::-1]


def sort_list(lst: List[Any]) -> List[Any]:
"""Returns a new list that is the sorted version of the input list."""
return sorted(lst)


def append_item(lst: List[Any], item: Any) -> List[Any]:
"""Appends an item to the end of the list and returns the list."""
lst.append(item)
return lst


def remove_item(lst: List[Any], item: Any) -> List[Any]:
"""Removes an item from the list if it exists and returns the list."""
if item in lst:
lst.remove(item)
return lst


def count_item(lst: List[Any], item: Any) -> int:
"""Returns the count of an item in the list."""
return lst.count(item)


def clear_list(lst: List[Any]) -> List[Any]:
"""Clears all items from the list and returns the list."""
lst.clear()
return lst


def insert_item(lst: List[Any], index: int, item: Any) -> List[Any]:
"""Inserts an item at a specific index in the list and returns the list."""
lst.insert(index, item)
return lst


def delete_random_item(lst: List[Any]) -> List[Any]:
"""Deletes a random item from the list and returns the list."""
if lst:
del lst[random.randint(0, len(lst) - 1)]
return lst


def copy_list(lst: List[Any]) -> List[Any]:
"""Returns a copy of the input list."""
return lst.copy()


def list_in(lst: List[Any], item: Any) -> bool:
"""Returns True if the item is in the list, False otherwise."""
return item in lst


def extend(lst1: List[Any], lst2: List[Any]) -> List[Any]:
"""Extends lst1 with the items in lst2 and returns the extended list."""
lst1.extend(lst2)
return lst1


def index(lst: List[Any], item: Any) -> int:
"""Returns the index of the first occurrence of an item in the list."""
return lst.index(item)


def pop(lst: List[Any], index: int = -1) -> Any:
"""Removes and returns the item at the given index from the list."""
return lst.pop(index)


def minimum(lst: List[Any]) -> Any:
"""Returns the smallest item in the list."""
return min(lst)


def maximum(lst: List[Any]) -> Any:
"""Returns the largest item in the list."""
return max(lst)


def filter_list(lst: List[Any], condition: callable) -> List[Any]:
"""Returns a new list containing only the items that satisfy the given condition."""
return [item for item in lst if condition(item)]


def map_list(lst: List[Any], function: callable) -> List[Any]:
"""Applies a function to each item in the list and returns a new list with the results."""
return [function(item) for item in lst]


def reduce_list(lst: List[Any], function: callable, initial: Any) -> Any:
"""Applies a function of two arguments cumulatively to the items of the list, from left to right, so as to reduce the list to a single output."""
result = initial
for item in lst:
result = function(result, item)
return result


def zip_lists(lst1: List[Any], lst2: List[Any]) -> List[tuple]:
"""Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument lists."""
return list(zip(lst1, lst2))


def flatten_list(lst: List[List[Any]]) -> List[Any]:
"""Flattens a list of lists into a single list."""
return [item for sublist in lst for item in sublist]
27 changes: 27 additions & 0 deletions bosco/list_processing/combination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""List Combining Algorithms."""
from typing import List, Any, Tuple


def concatenate_lists(*lists: List[Any]) -> List[Any]:
"""Concatenate any number of list you give it."""
result = []
for lst in lists:
result.extend(lst)
return result


def zip_lists(*lists: List[Any]) -> Tuple[Any]:
"""Combine multiple lists into a single list of tuples,
where each tuple contains elements from the corresponding positions of the lists."""
return list(zip(*lists))


def merge_lists(list1: List[Any], list2: List[Any]) -> List[Any]:
"""Combine two lists in a way that the elements are typically interleaved or merged according to a specific criterion."""
result = []
for i in range(max(len(list1), len(list2))):
if i < len(list1):
result.append(list1[i])
if i < len(list2):
result.append(list2[i])
return result
47 changes: 47 additions & 0 deletions bosco/list_processing/other.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Other List Algorithms."""

from typing import List, Any, Union, Tuple


def is_subset(first_list: List[Any], second_list: List[Any]) -> bool:
"""Determine if one set is a subset of another set."""
for element_one in first_list:
matched = False
for element_two in second_list:
if element_one == element_two:
matched = True
break
if not matched:
return False
return True


def list_intersection(list1: List[Any], list2: List[Any]) -> List[Any]:
"""Make a new list containing only the elements that are common to both input lists."""
return [x for x in list1 if x in list2]


def list_symmetric_difference(list1: List[Any], list2: List[Any]) -> List[Any]:
"""Make a new list containing elements that are present in only one of the input lists"""
return [x for x in list1 + list2 if (x not in list1) or (x not in list2)]


def list_concatenation(
list1: List[Union[int, float, str]], list2: List[Union[int, float, str]]
) -> List[Union[int, float, str]]:
"""Concatenates two lists into a single list."""
return list1 + list2


def list_elementwise_addition(
list1: List[Union[int, float]], list2: List[Union[int, float]]
) -> List[Union[int, float]]:
"""Performs element-wise addition of two lists of numbers."""
return [x + y for x, y in zip(list1, list2)]


def list_pairwise_multiplication(
list1: List[Union[int, float]], list2: List[Union[int, float]]
) -> List[Union[int, float]]:
"""Performs pairwise multiplication of two lists of numbers."""
return [x * y for x, y in zip(list1, list2)]
99 changes: 99 additions & 0 deletions bosco/list_processing/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""List searching algorithms."""

from typing import List, Optional
import math


def compute_iterative_binary_search(values: List[int], target: int) -> bool:
"""Search a list using iterative binary search."""
end = len(values) - 1
start = 0
while start <= end:
mid = (start + end) // 2
if values[mid] == target:
return True
elif values[mid] < target:
start = mid + 1
elif values[mid] > target:
end = mid - 1
return False


def compute_recursive_binary_search(
values: List[int], target: int, start: int = 0, end: Optional[int] = None
) -> bool:
"""Search a list using recursive binary search."""
if end is None:
end = len(values) - 1
if start > end:
return False
mid = (start + end) // 2
if values[mid] == target:
return True
elif values[mid] < target:
return compute_recursive_binary_search(values, target, mid + 1, end)
elif values[mid] > target:
return compute_recursive_binary_search(values, target, start, mid - 1)


def compute_jump_search(search_list: List[int], x: int) -> int:
"""Search a list using jump search function."""
n = len(search_list)
step = int(math.floor(math.sqrt(n)))
prev = 0
while search_list[min(step, n) - 1] < x:
prev = step
step += int(math.floor(math.sqrt(n)))
if prev >= n:
return -1
while search_list[prev] < x:
prev = prev + 1
if prev == min(step, n):
return -1
if search_list[prev] == x:
return prev
return -1


def compute_interpolation_search(search_list: List[int], x: int) -> int:
"""Find indices of two corners."""
lo = 0
n = len(search_list)
hi = n - 1
while lo <= hi and x >= search_list[lo] and x <= search_list[hi]:
if lo == hi:
if search_list[lo] == x:
return lo
return -1
pos = lo + int(
(
(float(hi - lo) / (search_list[hi] - search_list[lo]))
* (x - search_list[lo])
)
)
if search_list[pos] == x:
return pos
if list[pos] < x:
lo = pos + 1
else:
hi = pos - 1
return -1


def compute_linear_search(search_list: List[int], x: int) -> int:
"""Search a list using linear search function."""
for i in range(len(search_list)):
if search_list[i] == x:
return i
return -1


def compute_exponential_search(search_list: List[int], x: int) -> int:
"""Return the position of first occurrence of x in array."""
n = len(search_list) - 1
if search_list[0] == x:
return 0
i = 1
while i < n and search_list[i] <= x:
i = i * 2
return compute_recursive_binary_search(search_list, x, i / 2, min(i, n))
File renamed without changes.
2 changes: 1 addition & 1 deletion bosco/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def bosco(
starting_size: int = typer.Option(100),
number_doubles: int = typer.Option(5),
file: str = typer.Option("./bosco/sorting.py"),
file: str = typer.Option("./bosco/list_processing/sorting.py"),
function_name: str = typer.Option("bubble_sort"),
element_type: str = typer.Option(
"int", help="Type of the generated elements (int, float, or str)"
Expand Down
Loading
Loading