Git Product home page Git Product logo

acmsemo / algolib Goto Github PK

View Code? Open in Web Editor NEW
16.0 16.0 56.0 302 KB

A Library of Algorithms!

License: MIT License

Python 9.23% Dart 0.77% C++ 38.71% C# 1.91% Java 17.06% C 10.18% JavaScript 5.03% Nim 0.35% PHP 3.13% Pascal 1.68% Kotlin 2.48% Scala 3.90% Ruby 0.95% TypeScript 3.38% Go 0.34% Rust 0.92%
algorithms beginner-friendly contributions-welcome data-structures hacktoberfest2018

algolib's People

Contributors

akshat157 avatar andrianowinatra avatar dhairyakhale avatar ignaciovellido avatar ikumen avatar imoose avatar jayjeetatgithub avatar joeykanaly avatar jpfonseca avatar juan88 avatar kalebraymond avatar killzdesu avatar marshalx avatar mclmza avatar nattaaek avatar nisaruj avatar niteshjindalxb avatar patres270 avatar salif-04 avatar satyabrat35 avatar saurabh18213 avatar sbis04 avatar smithandrewl avatar stanecobalt avatar swastishreya avatar tdurtschi avatar vaibzz avatar varunvaruns9 avatar vijeshg avatar zanark avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

algolib's Issues

Add gitignore

Add a gitignore for a respective language directory

Documentation

Add some documentation to the readme explaining how an algorithm works

Linked List

class Element(object):
def init(self, value):
self.value = value
self.next = None

class LinkedList(object):
def init(self, head=None):
self.head = head

def append(self, new_element):
    current = self.head
    if self.head:
        while current.next:
            current = current.next
        current.next = new_element
    else:
        self.head = new_element

def get_position(self, position):
    """Get an element from a particular position.
    Assume the first position is "1".
    Return "None" if position is not in the list."""
    first_element = self.head
    next_element = first_element
    if position < 1:
        return None
    for i in range(1, position):
        next_element = next_element.next
        if next_element is None:
            return None
    return next_element

def insert(self, new_element, position):
    """Insert a new node at the given position.
    Assume the first position is "1".
    Inserting at position 3 means between
    the 2nd and 3rd elements."""
    elem_at_pos = self.get_position(position)
    new_element.next = elem_at_pos
    if position == 1:
        self.head = new_element
    elif position > 1:
        self.get_position(position-1).next = new_element

def delete(self, value):
    """Delete the first node with a given value."""
    i = 1
    while self.get_position(i) is not None:
        elem_at_pos = self.get_position(i)
        if elem_at_pos is not None:
            if elem_at_pos.value == value:
                if i == 1:
                    self.head = elem_at_pos.next
                    elem_at_pos.next = None
                elif i > 1:
                    self.get_position(i-1).next = elem_at_pos.next
                    elem_at_pos.next = None
        i += 1

Test cases

Set up some Elements

e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)

Start setting up a LinkedList

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)

Test get_position

Should print 3

print ll.head.next.next.value

Should also print 3

print ll.get_position(3).value

Test insert

ll.insert(e4, 3)

Should print 4 now

print ll.get_position(3).value

Test delete

ll.delete(1)

Should print 2 now

print ll.get_position(1).value

Should print 4 now

print ll.get_position(2).value

Should print 3 now

print ll.get_position(3).value

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.