Git Product home page Git Product logo

tryalgo's Introduction

PyPI PyPI Pylint score Codecov

Algorithmic Problem Solving

Algorithms and data structures for preparing programming competitions (e.g. ICPC, see more) and coding interviews.
By Christoph Dürr and Jill-Jênn Vie.

Our book is available in French, English, Simplified and Traditional Chinese.

Install

pip install tryalgo

Documentation

Shortest paths on the graph of Paris.

To run it yourself:

pip install -r examples/requirements.txt
jupyter notebook  # Then go to examples folder

Usage

Dynamic programming some example with coin change:

from tryalgo import coin_change

print(coin_change([3, 5, 11], 29))  # True because 29 = 6 x 3 + 0 x 5 + 1 x 11

Des chiffres et des lettres (that inspired Countdown)

from tryalgo.arithm_expr_target import arithm_expr_target

arithm_expr_target([25, 50, 75, 100, 3, 6], 952)

Returns '((((75*3)*(100+6))-50)/25)=952'.

Tests

All algorithms are thoroughly tested. These tests can be used to practice your programming skills!

python -m unittest

Most snippets from the book are within 76 columns (French version) or 75 columns (English version).

Our code is checked. Using optional requirements, you can check it too:

pip install pycodestyle pylint
make pycodestyle  # PEP8
make pylint

Found a bug?

Please drop an issue.

Authors

© 2016–2023, Christoph Dürr and Jill-Jênn Vie ([email protected]).
Released under the MIT License.

Contributors

Thanks!

  • Louis Abraham
  • Lilian Besson
  • Xavier Carcelle
  • Stéphane Henriot
  • Ryan Lahfa
  • Olivier Marty
  • Samuel Tardieu

tryalgo's People

Contributors

bluesheeptoken avatar jilljenn avatar karthiikselvam avatar louisabraham avatar naereen avatar oliviermarty avatar puigfp avatar raitobezarius avatar remym19 avatar samueltardieu avatar shloub avatar xcarcelle avatar xtof-durr 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

tryalgo's Issues

Data Input Format for Gale-Shapley

How would you format the data for the SPOC example data, specifically the two 4x4 data sets.
women
4 3 1 2
2 1 3 4
1 3 4 2
4 3 1 2
men
3 2 4 1
2 3 1 4
3 1 2 4
3 2 4 1

I tried:
women = [[4,3,1,2],[2,1,3,4],[1,3,4,2],[4,3,1,2]]
men = [[3,2,4,1],[2,3,1,4],[3,1,2,4],[3,2,4,1]]

spouse = gale_shapley(men, women)
print(spouse)

When I call the function, I get the following error message.
in
61 #men.append([3,2,4,1])
62
---> 63 spouse = gale_shapley(men, women)
64 print(spouse)

in gale_shapley(men, women)
20 for r in range(n):
21 print(j,r,women[j][r])
---> 22 rank[j][women[j][r]] = r
23 singles = deque(range(n)) # all men are single and get in the queue
24 while singles:

IndexError: list assignment index out of range

I did try modifying the data so that it is zero based, but you still get an error. The data in the problem is generally like what was given above.

Any help appreciated!

Benchmark union of rectangles

On the blog someone found out that implementations may not be as fast as expected; the code should be profiled.
(According to Christoph, it was due to the testcases.)

cut_nodes_edges2 not in tryalgo-1.0

When installing tryalgo with pip command (using internet connection), cut_nodes_edges2 is not in source code, so tryalgo test script makes an error.

import tryalgo

Why can't we import directly
from tryalgo import opt_bin_search_tree1, decode_root_matrix_to_level
but need to write
from tryalgo.dyn_prog_tricks import opt_bin_search_tree1, decode_root_matrix_to_level
?
In other words at what place are the modules moved one level up for export?

Add pyproject.toml

DEPRECATION: tryalgo is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at pypa/pip#8559

Small improvements to code

  • random_eulerien_graph n'assure pas que le graphe généré est connexe, peut-être souhaite-t-on que ce soit fait ;
  • Line 133 should be different for directed or undirected graph

Add SPFA

Just for fun and to thank cp-algorithms.com great website

Misleading comment in the book for Rabin Karp algorithm

Hello,

About the line 16 of the Rabin Karp algorithm:

val = (old_val - out_digit * last_pos + DOMAIN * PRIME) % PRIME

You say in the book that you are adding DOMAIN * PRIME to counter the fact that C++ will return a negative modulo.

Even by doing this you can obtain a negative modulo as (old_val - out_digit * last_pos + DOMAIN * PRIME) can be negative, this happen if the sum produces a number bigger than 2^64.

What do you think?

Also, I am not sure if it is a real issue when the number ends up negative?

Problem with consecutive ones

The consecutive ones instance

0,1,0,0,0,0,0,1,0,0,0
0,0,1,0,0,0,0,0,0,0,1
0,1,1,0,0,0,0,0,0,1,0
0,1,1,0,1,0,0,1,1,1,1

is a yes-instance; the correct order is [0, 3, 5, 6, 4, 8, 7, 1, 9, 2, 10].

from tryalgo import pq_tree as pq
pq.consecutive_ones_property([{1,2,9},{1,7},{2,10},{1,2,9},{1,2,4,7,8,9,10}],{0,1,2,3,4,5,6,7,8,9,10}) 

gives this correct output. However, when permuting the input,

pq.consecutive_ones_property([{1,7},{2,10},{1,2,9},{1,2,4,7,8,9,10}],{0,1,2,3,4,5,6,7,8,9,10}) 

fails with an error message:

 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/martin/.local/lib/python3.6/site-packages/tryalgo/pq_tree.py", line 268, in consecutive_ones_property
    tree.reduce(S)
  File "/home/martin/.local/lib/python3.6/site-packages/tryalgo/pq_tree.py", line 235, in reduce
    z.full_leafs += x.full_leafs                 # cumulate bottom up full leaf numbers
AttributeError: 'NoneType' object has no attribute 'full_leafs'

Typo in a link in the doc

A very interesting data structure is called `PartitionRefinement <tryalgo/tryalgo.html#module-tryalgo.partition_refinement>`. It maintains a partition over the set {0,1,...,n-1}. The main operation is called *refine(S)* which splits each part P of the current partition into elements that are in S and elements that are not in S. The complexity of this operation is linear in the size of S.

this links does not render nicely. See the end of this paragraph:
typo in link

anagrams modifies its input

Is there a specific reason for this? This side-effect feels unnatural to me. Maybe another name could be used instead of w? unique_w = list(set(w))?

Edit: It seems anagrams actually doesn't modify its input, my bad.

Trivial C1P is not validated

array([[0, 1, 0, 0, 0],
       [0, 0, 1, 1, 0],
       [1, 1, 1, 0, 0],
       [1, 1, 1, 1, 1],
       [0, 1, 1, 1, 0],
       [1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1],
       [0, 0, 0, 0, 1],
       [1, 1, 1, 1, 0],
       [0, 0, 1, 0, 0]])

with sets:

[{1},
 {2, 3},
 {0, 1, 2},
 {0, 1, 2, 3, 4},
 {1, 2, 3},
 {0, 1, 2, 3, 4},
 {1, 2, 3, 4},
 {4},
 {0, 1, 2, 3},
 {2}]

gives:

IsNotC1P: state == -1

anagrams.py input ?

In the book you talk about an input :
'le chien marche vers sa niche et trouve une limace de chine nue pleine de malice qui lui fait du charme'
A string, but in your code you talk about a list.
In both cases, you cant turn a set into a list using list(set(w)) it give TypeError: 'set' object is not subscriptable
Am I missing something?

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.