Algorithms implementations in Python.
To run one of the algorithms' CLI, run
./algo-rhythm/<algorithm-name>.py
This repository is licensed under the terms of MIT License.
:musical_note: In the rhythm of the algorithms | No ritmo dos algoritmos
License: MIT License
Algorithms implementations in Python.
To run one of the algorithms' CLI, run
./algo-rhythm/<algorithm-name>.py
This repository is licensed under the terms of MIT License.
Use closure and nonlocal
instead of using global variable.
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
Implement Closest pair of points problem.
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)])
)
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)
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:
["bat", "man", "ice"]
, word = "batman"
, should return [["bat", "man"]]
["bat", "man", "super", "su", "per"]
, word = "superman"
, should return [["super", "man"],["su", "per", "man"]
["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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.