geekelo / geekelo/dsa_practice
DSA RUBY
- Dominant language
- No language data
- Stars
- 1
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
If you want to use **Ruby on Rails** to solve **Data Structures and Algorithms (DSA) challenges**, you mainly need **Ruby syntax and core features** rather than the full Rails framework. However, if you're solving these challenges within a Rails environment (e.g., in a Rails console or as part of a web application), here’s what you need to focus on:
---
### 1. **Ruby Syntax & Core Concepts**
Since Rails is built on Ruby, mastering Ruby’s syntax is essential for solving DSA challenges.
#### **Basic Data Types**
- **Numbers**: `Integer`, `Float`
- **Strings**: `"hello".reverse`, `"world".upcase`
- **Arrays**: `[1, 2, 3].map { |n| n * 2 }`
- **Hashes**: `{ a: 1, b: 2 }[:a] #=> 1`
- **Symbols**: `:name`
- **Ranges**: `(1..5).to_a #=> [1, 2, 3, 4, 5]`
- **Booleans & Nil**: `true`, `false`, `nil`
#### **Conditionals & Loops**
- `if`, `elsif`, `else`, `unless`
- `while`, `until`, `loop`, `for`
- `each`, `map`, `select`, `reduce`
#### **Methods & Blocks**
```ruby
def add(a, b)
a + b
end
add(2, 3) #=> 5
```
Blocks and Lambdas:
```ruby
[1, 2, 3].map { |n| n * 2 } #=> [2, 4, 6]
square = ->(n) { n**2 }
square.call(4) #=> 16
```
#### **Classes & Modules**
```ruby
class Node
attr_accessor :value, :next
def initialize(value)
@value = value
@next = nil
end
end
```
#### **Error Handling**
```ruby
begin
1 / 0
rescue ZeroDivisionError
puts "Cannot divide by zero"
end
```
---
### 2. **Data Structures**
- **Arrays (`Array`)** – `push`, `pop`, `shift`, `unshift`
- **Hashes (`Hash`)** – `{ key => value }`, `merge`, `keys`, `values`
- **Sets (`Set`)** – `require 'set'`, `Set.new([1, 2, 3])`
- **Stacks (Using Arrays)** – `push`, `pop`
- **Queues (Using Arrays or Deques)** – `shift`, `push`
- **Linked Lists** – Implement with `Struct` or `Class`
- **Trees** – `BinaryTree`, `BST`
- **Graphs** – Adjacency list or matrix
---
### 3. **Algorithms**
You should be comfortable implementing:
- **Sorting**: `bubble_sort`, `quick_sort`, `merge_sort`
- **Searching**: `binary_search`, `linear_search`
- **Recursion**: `factorial`, `fibonacci`
- **Dynamic Programming**: Memoization techniques
- **Graph Algorithms**: BFS, DFS, Dijkstra’s Algorithm
- **String Manipulation**: Palindrome check, Anagram check
- **Bit Manipulation**: XOR swaps, setting/clearing bits
Example:
```ruby
def binary_search(arr, target)
left, right = 0, arr.length - 1
while left <= right
mid = (left + right) / 2
return mid if arr[mid] == target
if arr[mid] < target
left = mid + 1
else
right = mid - 1
end
end
-1
end
```
---
### 4. **Rails-Specific Considerations**
Though Rails itself isn't needed for DSA, you can use:
- **Rails Console (`rails c`)** – Run Ruby code interactively.
- **Rake Tasks (`lib/tasks/*.rake`)** – Run scripts for DSA challenges.
- **Controllers & Models** – For structuring problems in MVC.
- **Background Jobs** – For async problem-solving.
---
### 5. **Testing Your Solutions**
Use **RSpec** or **Minitest** to write test cases.
```ruby
require 'rspec'
def factorial(n)
return 1 if n == 0
n * factorial(n - 1)
end
RSpec.describe 'Factorial' do
it 'calculates factorial of 5' do
expect(factorial(5)).to eq(120)
end
end
```
Run with:
```sh
rspec filename.rb
```
---
### 6. **Where to Practice?**
- **LeetCode** (Ruby support)
- **HackerRank**
- **CodeWars**
- **Project Euler** (Math-heavy)
- **Rails Console for Quick Testing**
---
### Final Tip: **Do You Need Rails for DSA?**
Not really. Rails is for web development, and pure Ruby is enough for DSA challenges. However, if you're comfortable in a Rails environment, you can integrate DSA problems into Rake tasks or use `rails console` to test algorithms.
Would you like me to generate some sample Rails-based DSA problems? 🚀
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.