Skip to content

contribution guidelines checks #1787

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

Merged
merged 16 commits into from
Mar 4, 2020
10 changes: 5 additions & 5 deletions machine_learning/linear_regression.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
Linear regression is the most basic type of regression commonly used for
predictive analysis. The idea is pretty simple, we have a dataset and we have
a feature's associated with it. The Features should be choose very cautiously
as they determine, how much our model will be able to make future predictions.
We try to set these Feature weights, over many iterations, so that they best
fits our dataset. In this particular code, i had used a CSGO dataset (ADR vs
predictive analysis. The idea is pretty simple: we have a dataset and we have
features associated with it. Features should be chosen very cautiously
as they determine how much our model will be able to make future predictions.
We try to set the weight of these features, over many iterations, so that they best
fit our dataset. In this particular code, I had used a CSGO dataset (ADR vs
Rating). We try to best fit a line through dataset and estimate the parameters.
"""
import requests
Expand Down
46 changes: 27 additions & 19 deletions sorts/comb_sort.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""
This is pure python implementation of comb sort algorithm.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python --> Python # it is a brand name, not a snake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even needed to say its implemented in Python given that this entire repository should just be python algorithms?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine to write that this is a Python implementation.
The code might be copied elsewhere outside this repo.

Comb sort is a relatively simple sorting algorithm originally designed by Wlodzimierz Dobosiewicz in 1980.
Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort.
It was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort algorithm.
In bubble sort, distance (or gap) between two compared elements is always one.
Comb sort improvement is that gap can be much more than 1, in order to prevent slowing down by small values
at the end of a list.

More info on: https://en.wikipedia.org/wiki/Comb_sort

This is pure python implementation of comb sort algorithm
For doctests run following command:
python -m doctest -v comb_sort.py
or
Expand All @@ -15,40 +20,43 @@

def comb_sort(data):
"""Pure implementation of comb sort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
:param data: mutable collection with comparable items
:return: the same collection in ascending order
Examples:
>>> comb_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> comb_sort([])
[]
>>> comb_sort([-2, -5, -45])
[-45, -5, -2]
>>> comb_sort([99,45,-7,8,2,0,-15,3])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comma space please.

[-15, -7, 0, 2, 3, 8, 45, 99]
"""
shrink_factor = 1.3
gap = len(data)
swapped = True
i = 0
completed = False

while gap > 1 or swapped:
# Update the gap value for a next comb
gap = int(float(gap) / shrink_factor)
while not completed:

swapped = False
i = 0
# Update the gap value for a next comb
gap = int(gap / shrink_factor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gap //= shrink_factor # will give us integer division

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not work, return type is still float

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Sorry for the mistake.

>>> 35.2 / 7
5.028571428571429
>>> 35.2 // 7
5.0

if gap <= 1:
completed = True

while gap + i < len(data):
if data[i] > data[i + gap]:
index = 0
while index + gap < len(data):
if data[index] > data[index + gap]:
# Swap values
data[i], data[i + gap] = data[i + gap], data[i]
swapped = True
i += 1
data[index], data[index + gap] = data[index + gap], data[index]
completed = False
index += 1

return data


if __name__ == "__main__":

import doctest
doctest.testmod()

user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(comb_sort(unsorted))