Code Monkey home page Code Monkey logo

algo-rhythm's Introduction

Algo-rhythm 🎵

Algorithms implementations in Python.

Usage

To run one of the algorithms' CLI, run

./algo-rhythm/<algorithm-name>.py

License

This repository is licensed under the terms of MIT License.

algo-rhythm's People

Contributors

mateusoliveira43 avatar

Watchers

 avatar

algo-rhythm's Issues

Valid bracket problem

Implement this algorithm: Given a string with only brackets ({, }, [, ], (, )), check if it is valid, i.e. all brackets close correctly.

def check_brackets(sequence_of_brackets: str) -> bool:
    if len(sequence_of_brackets) % 2:
        return False
    brackets = {
        "(": ")",
        "[": "]",
        "{": "}",
    }
    stack = []
    for char in sequence_of_brackets:
        if char in brackets:
            stack.append(char)
        else:
            if not stack:
                return False
            open_bracket = stack.pop()
            if char != brackets[open_bracket]:
                return False
    return not stack

Get number of times a word can form another word

Implement: Given two words, check how many times the second word can form the first.
Ex.: "ab", "abba" -> 2; "abc", "abab" -> 0.

import math


def get_number_words_can_form(word1: str, word2: str) -> int:
    return math.floor(
        min([word2.count(letter)/word1.count(letter) for letter in set(word1)])
    )

Add alrorithm to list nth element that is only divisible by a list of primes

Given a position and a list of prime numbers, return the number that is in given position in the sorted list of numbers (1 is always include) only divisible by the given list of primes.

from functools import reduce
from operator import mul


def get_divisor_numbers(divisor: int, divisors: list[int], end: int) -> set[int]:
    rest = {
        index for index in range(2,end)
        if reduce(mul, map(index.__mod__, divisors)) > 0
    }
    return {
        divisor*index for index in range(2, end)
        if reduce(mul, map(index.__mod__, divisors)) == 0
        and reduce(mul, map(index.__mod__, rest), 1) > 0
    }


def get_list_of_numbers(divisors: list[int], end: int) -> list[int]:
    # TODO use cache?
    solution = {1}.union(divisors)
    for divisor in divisors:
        # TODO good limit for end?
        solution.update(get_divisor_numbers(divisor, divisors, end*reduce(mul, divisors)))
    return sorted(list(solution))[:end]

print("3, 7 and 11")
for num in range(1, 12):
    sol = get_list_of_numbers([3,7,11], num)
    print(f"{num:2}:{len(sol):2}", sol)
print("2, 5 and 17")
for num in range(1, 12):
    sol = get_list_of_numbers([2,5,17], num)
    print(f"{num:2}:{len(sol):2}", sol)
print("7 and 13")
for num in range(1, 12):
    sol = get_list_of_numbers([7,13], num)
    print(f"{num:2}:{len(sol):2}", sol)

add anagram algorithm

Given a list of strings, and a word (or phrase, disregarding white spaces), return a list of list of strings that reassembling these strings, we get the word.

Examples:

  1. list of strings = ["bat", "man", "ice"], word = "batman", should return [["bat", "man"]]
  2. list of strings = ["bat", "man", "super", "su", "per"], word = "superman", should return [["super", "man"],["su", "per", "man"]
  3. list of strings = ["bat", "man", "ice"], word = "joker", should return []

code

import itertools


def anagram(word: str, phrases: list[str]) -> list:
    anagrams = []
    for index in range(1, len(phrases)+1):
        for combination in itertools.combinations(phrases, index):
            if sorted("".join(combination)) == sorted("".join(word.split(" "))):
                anagrams.append(list(combination)))
    return anagrams

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.