Giter VIP home page Giter VIP logo

algorithms's People

Contributors

aig031 avatar ankit167 avatar c-utkarsh avatar christianbender avatar ericklarac avatar geon0325 avatar goswami-rahul avatar himanshu-77 avatar hsi1032 avatar ihchen avatar izanbf1803 avatar jacobirr avatar jiangying000 avatar jiwoogit avatar jkliang9714 avatar keon avatar kmaziarz avatar nickolaswiebe avatar nikhilhassija avatar ofek avatar orenovadia avatar pandawhocodes avatar probablytom avatar quang2705 avatar saadbenn avatar shruti19 avatar ss18 avatar tratneshsingh avatar vicky1999 avatar yunshuipiao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithms's Issues

Repo Python version?

This repo doesn't seem to be consistent about which version of Python the code is written for.

  • Some files use type hints (SyntaxError in Python 2)
  • Some files use print statements (SyntaxError in Python 3)
  • garage.py has a mix of print statements and print as a function (although Python 2 doesn't treat it as a function)

I guess my question is, which version(s) of Python is this repo supposed to be for?

Importing __future__.print_function and adding brackets to all the print statements is fairly easy, but the type hinting is Python 3 only (although they could be moved to the docstrings for cross-compatibility).

separate test module

Since we now have tests in most of the algorithms, I think it is a good idea to have a separate test_module.py file in each module. We can move all the tests from one module to it, and any further algorithms must have their tests in this file. It will make the repo easy to manage.
@keon what do you think? I can start working on this.

Organizing the repo neatly

Hey guys, I was wondering we should put all the algorithms in an algorithm folder or something. I feel like It's getting big with all those new files being added to it. I have to scroll all the way down or up to get to where I want and right now I feel it is a bit disorganized. It would be very easy for readers to just click the algorithm folder and look for the intended algorithms inside it. The extra junk can remain outside. What do you guys think?

Small mistake in sort/merge_sort.py array merge

code location: keon/algorithms/blob/master/sort/merge_sort.py

mistake 1

origion code: line - 30

    # Add the left overs if there's any left to the result
    if left:
        arr.extend(left[left_cursor:])
    if right:
        arr.extend(right[right_cursor:])
    return arr

should be ==>

    # Add the left overs if there's any left to the result
    if left_cursor < len(left)::
        arr.extend(left[left_cursor:])
    if right_cursor < len(right)::
        arr.extend(right[right_cursor:])
    return arr

more pythonic efficient way ,no construct sub array ==>

    # Add the left overs if there's any left to the result
    for i in range(left_cursor,len(left)):
        arr.append(left[i])
    for i in range(right_cursor,len(right)):
        arr.append(right[i])
    return arr

mistake 2

origion code : line - 38

print(merge_sort(array, 0, len(array)-1))
print(array)

should be ==>

print(merge_sort(array))

Does anyone know how to set up Travis for java?

I know this may not the right place to ask this question but I am working on an Android Studio project and am not very familiar with Travis. All I want is to catch syntax errors and undefined names.

I am asking this questions because the travis for this repo does this nicely. However, I'll be working with Java and was wondering anyone with better understanding could point me in the right direction?

@danghai @keon you guys seem to be familiar with travis.

BTW sorry for asking questions not related to the repo but I have been super busy lately with projects and stuff.

Bug in algorithms/array/flatten.py, line 10

Run the flatten(inputArr, outputArr=None) function in python3.5.
It seams ok when the test case is [2, 1, [3, [4, 5], 6], 7, [8]].
But when using [[3, [4, 5], 6], 7, [8]], function outputs [7, 8] not [3, 4, 5, 6, 7, 8].
That is because not None and not [] will be both True in line 10.
Take a = [[3, [4, 5], 6], 7, [8]] as example, first call flatten(a, None).
It will call flatten([3, [4, 5], 6], []). Run till line 10, not outputArr is True where outputArr is [].
Then outputArr will be assigned a new empty list. So it lose [3, 4, 5, 6] in recursive.
It is fine because the first element is single so not [2] is False in given test case.

Bug can fix in:
if outputArr is None:
or
def flatten(inputArr, outputArr=[]) and delete the if statement.

@array/garage your math is wrong

in the garage problem, when i set the initial = [1,2,3,4,0,5,6] and final = [1,6,0,4,5,3,2] ,in your math, the result is 7. But my math will use only require 5 steps to do it which are
[1, 2, 3, 4, 5, 0, 6]
[1, 2, 0, 4, 5, 3, 6]
[1, 2, 6, 4, 5, 3, 0]
[1, 0, 6, 4, 5, 3, 2]
[1, 6, 0, 4, 5, 3, 2]

Tests in directory tests

tests
If I ran the tests I get this kind of error message:

from search.binary_search import binary_search, binary_search_recur
ImportError: No module named 'search'

or

from array.delete_nth import delete_nth, delete_nth_naive
ImportError: No module named 'array.delete_nth'; 'array' is not a package

Complexity of algorithms

There can be several approaches to solve a problem- the optimal, suboptimal or the brute force approach.
For an algorithm written in x.py, is it a good option to may be discuss the various approaches (write separate function in the file, for each approach)?, or just post the optimal solution?

The reason I brought this up is because, some algorithms in the repo have the optimal code written, while some do not. If a single solution to a problem is to be posted, then it would be good to stick to the optimal one across all files.

Many syntax errors that must be resolved.

Travis caught many errors during its first build with flake8.
It is requested and advised to not add further algorithms until the current known errors are resolved because otherwise, Travis will always show errors for your PR, even if your changes are correct.
Let's fix those errors first! (check the above link).

Character encoding issue with heap/skyline.py

Running on python 2.7 on Mac High Sierra I get the following error.

 python skyline.py 
  File "skyline.py", line 10
SyntaxError: Non-ASCII character '\xe2' in file skyline.py on line 11, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Python 3 compatibility?

Are all of these files Python 3 compatible?
If so, please add a line or two about their compatibility with Python 2/3 on the download page.

Thanks

Please avoid directly commit to master

A quick reminder to the newer collaborators :)
Please avoid directly committing to this repo. It is always a good idea and practice to open a PR for any change ( adding an algorithm or modifing something). In this way, other collaborators can review and suggest changes, which will be more helpful for the repo. This will also let identify errors and avoid merges to the main repo causing errors like this one like this one.
@SaadBenn @danghai @christianbender

Naming conventions

Not that this is all too important, but given that in a (lesser) sense yours is also a python programming showcase, wouldn't it be desirable to honour pep8 suggestions? For example, to avoid camel case names like isEmpty.

math/prime_test.py

the prime_test function returns true for 35, while 35 is not a prime.

Add Documentation

This is an extension of issue #264
To prepare for packaging, we need to properly explain how api works in our documentation.

Error: test case Tarjan in test_graph

I have error when I try to run test_graph for tarjan. I am not sure about this algorithm.
Hello, @mecforlove . It is very nice when you take a review about it? Thanks

FF
======================================================================
FAIL: test_tarjan_example_1 (test_graph.TestTarjan)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/danghai/algorithms/graph/test_graph.py", line 28, in test_tarjan_example_1
    self.assertEqual(g.sccs, [['F', 'G'], ['H', 'D', 'C'], ['E', 'B', 'A']])
AssertionError: Lists differ: [[F, G], [H, D, C], [B, A, E]] != [['F', 'G'], ['H', 'D', 'C'], ['E', 'B', 'A']]

First differing element 2:
[B, A, E]
['E', 'B', 'A']

- [[F, G], [H, D, C], [B, A, E]]
+ [['F', 'G'], ['H', 'D', 'C'], ['E', 'B', 'A']]

======================================================================
FAIL: test_tarjan_example_2 (test_graph.TestTarjan)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/danghai/algorithms/graph/test_graph.py", line 44, in test_tarjan_example_2
    self.assertEqual(g.sccs, [['B', 'E', 'A'], ['D', 'C'], ['G', 'F'], ['H']])
AssertionError: Lists differ: [[E, A, B], [D, C], [F, G], [H]] != [['B', 'E', 'A'], ['D', 'C'], ['G', 'F'], ['H']]

First differing element 0:
[E, A, B]
['B', 'E', 'A']

- [[E, A, B], [D, C], [F, G], [H]]
+ [['B', 'E', 'A'], ['D', 'C'], ['G', 'F'], ['H']]

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=2)

No License

How is type of license for this project?

Is this work? algorithms/tree/bst/is_bst.py

def is_BST(root):
    """
    :type root: TreeNode
    :rtype: bool
    """
    if not root:
        return True
    stack = []
    pre = None
    while root and stack:
        while root:
            stack.append(root)
            root = root.left
        root = stack.pop()
        if pre and root.val <= pre.val:
            return False
        pre = root
        root = root.right
    return True

Bit Manipulation: Some interesting questions

  1. Flip Bit to win: You have an integer and you can flip exactly one bit from a 0 to 1. Write code to find the length of the longest sequence of 1s you could create.
    For example:
    Input: 1775 ( or: 11011101111)
    Output: 8

  2. Conversion: Write a function to determine the number of bits you would need to flip to convert integer A to integer B.
    For example:
    Input: 29 (or: 11101), 15 (or: 01111)
    Output: 2

Plan for testing

I want to add some additional matrix algorithms and some tests for that.

So, do you have a testing plan of test format, rules of in/output example data, test script name and others?

Anagram.py defines all_perms method twice

Not sure if this was intended, but the code doesn't seem to access the first all_perms function.

def all_perms(elements):
if len(elements) <=1:
yield elements
else:
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
yield perm[:i] + elements[0:1] + perm[i:]
def all_perms(elements):
if len(elements) <=1:
return elements
else:
tmp = []
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
tmp.append(perm[:i] + elements[0:1] + perm[i:])
return tmp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.