Introduction to Practice Problems

Welcome to the practical segment of our Ruby programming journey! Today, we're applying the knowledge from past lessons to solve two practice problems using advanced Ruby data structures: queues and sorted arrays with custom class keys.

First Practice Problem: Implementing Queues with Arrays

Consider an event-driven system, like a restaurant. Orders arrive, and they must be handled in the order they were received, following the First In, First Out (FIFO) principle. This principle makes it a perfect scenario for a queue implementation in Ruby using arrays.

class Queue
  def initialize
    # Initializing an empty queue
    @buffer = []
  end

  def enqueue(val)
    # Adding (enqueueing) an item to the queue
    @buffer.push(val)
  end

  def dequeue
    # Removing (dequeuing) an item from the queue
    @buffer.shift
  end

  # Checking if the queue is empty
  def is_empty
    @buffer.empty?
  end

  # Checking the size (number of items) in the queue
  def size
    @buffer.size
  end
end

This code demonstrates the creation and operation of a Queue class, which leverages Ruby's array to efficiently implement a queue. The Queue class includes methods to enqueue (add) an item, dequeue (remove) an item, check if the queue is empty, and return the queue's size. Enqueue operations add an item to the end of the array, while dequeue operations remove an item from the beginning, maintaining the First In, First Out (FIFO) principle.

Analyzing the First Problem Solution

In our implementation of a queue using Ruby arrays, the enqueuing of an item is done by using the push method, mimicking the arrival of a new order at the restaurant. The dequeuing serves an order using the shift method, reflecting the preparation and delivery of the order in FIFO order.

Second Practice Problem: Using Sorted Structures with Custom Class as a Key

For the second problem, envision a leaderboard for a video game. Players with their scores can be represented as objects of a custom class, then sorted efficiently. In Ruby, this can be achieved using arrays paired with Ruby's sort method and the Comparable module.

class Player
  include Comparable

  attr_reader :name, :score

  def initialize(name, score)
    @name = name
    @score = score
  end

  # Defining comparator for custom class
  def <=>(other)
    [other.score, name] <=> [@score, other.name]
  end

  # Defining the string representation of Player object
  def to_s
    "(#{name}, #{score})"
  end
end

# Create an array of players
players = [
  Player.new("John", 900),
  Player.new("Doe", 1000)
]

# Sort players
sorted_players = players.sort

# Print sorted players
sorted_players.each do |player|
  puts player  # e.g., (Doe, 1000)
end

This code snippet introduces a Player class for representing players in a video game, incorporating the Comparable module to allow sorting by score (primary) and name (secondary). An array of Player instances is then sorted, ensuring that players are stored in a manner that is sorted first by their scores and then by their names if scores are equal. This is essential for functionalities like leaderboards, where players need to be ranked efficiently according to their performance.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal