This repository contains a collection of Ruby scripts showcasing various algorithms and problem-solving techniques. Each project is implemented as a standalone .rb
file, demonstrating different computational approaches and data structures.
Description: This script computes all possible decompositions of a given number into three distinct parts whose sum equals the number. It’s useful for mathematical exploration and combinatorial problems.
Features:
- Accepts any positive integer as input.
- Outputs all valid decompositions in a readable format.
Usage Example:
ruby decomposition_of_three.rb 9
# Output:
# [1, 2, 6]
# [1, 3, 5]
# [2, 3, 4]
Description: A Ruby implementation of Conway’s Game of Life. This program simulates the evolution of a grid-based system of cells according to simple rules, demonstrating the emergence of complex behavior from simple rules.
Features:
- Configurable grid size and initial state.
- Outputs the grid after each generation.
Usage Example:
ruby game_of_life.rb
# Outputs grid states for each generation.
Description: Implementation of k
stacks using a single array, showcasing an efficient way to manage multiple stacks with minimal memory overhead.
Features:
- Push and pop operations for multiple stacks.
- Handles overflow and underflow conditions gracefully.
Usage Example:
ruby k_stacks.rb
# Outputs stack operations and current state.
Description: Sorts an array such that adjacent elements, when concatenated, form a palindrome. This is a unique take on sorting that ensures special properties in the output array.
Features:
- Takes an array of strings as input.
- Outputs a palindrome-compatible sorted array.
Usage Example:
ruby pal_sort.rb
# Outputs sorted array with palindrome properties.
Description: Computes and displays the prime factorization of a given number. This script breaks down any positive integer into its prime factors.
Features:
- Accepts any positive integer as input.
- Outputs prime factors in a formatted manner.
Usage Example:
ruby prime_factorization.rb 84
# Output:
# 2^2 * 3^1 * 7^1
Description: Implements shortest path algorithms (like Dijkstra's) for finding the shortest path between nodes in a graph. The script reads graph data and computes the shortest path for given nodes.
Features:
- Input flexibility for custom graphs.
- Outputs the shortest path and its cost.
Usage Example:
ruby shortest_path.rb
# Outputs shortest path and total distance for given nodes.
-
Clone the Repository:
git clone https://github.com/yourusername/ruby-algorithms.git cd ruby-algorithms
-
Install Ruby: Ensure you have Ruby installed. You can check your Ruby version with:
ruby -v
-
Install Dependencies (if any): Some scripts may require additional gems. Run
bundle install
if aGemfile
is present, or manually install necessary gems using:gem install gem_name
- Navigate to the project directory.
- Run the desired script by passing any required arguments. For example:
ruby prime_factorization.rb 84
- Follow the on-screen prompts or enter data as required by the script.
Contributions are welcome! If you have enhancements or new algorithms to add, feel free to fork the repository, create a new branch, and submit a pull request.
- Fork the repository.
- Create your feature branch:
git checkout -b feature/your-feature-name
- Commit your changes:
git commit -m 'Add your feature'
- Push to the branch:
git push origin feature/your-feature-name
- Open a pull request.