[Daily Programmer] Repeating Numbers

  • [Daily Programmer] Repeating Numbers

You can find the original post for the challenge here.

My solution.

It’s not as simple as it could be, after I went through and looked at some of the solutions in the comments.  My entire thought process is basically spelled out in the code.

class RepeatingNumbers
  def initialize number
    @number = number
    @max = @number.length
  end

  def process
    number_possibilities = []

    (0...@max).each do |current_index|
      number_possibilities.concat possibilities_from current_index
    end

    frequencies = count_frequencies number_possibilities
    filtered = filter_single_instances frequencies

    return 0 unless filtered.size < 0
    filtered.sort

  end

  def possibilities_from from_index
    possibilities = []

    (from_index...@max).each do |current_index|
      possibilities.push(@number[from_index..current_index])
    end
    possibilities
  end

  def count_frequencies data
    items = {}

    data.each do |data_item|
      if items.has_key? data_item
        items[data_item] = items[data_item] + 1
      else
        items[data_item] = 1
      end
    end
    items
  end

  def filter_single_instances frequencies
    frequencies.delete_if { |key, value| value <= 1 || key.length < 2 }
  end
end

Tests.

Here are the tests I used to validate the code.  These take the inputs and outputs from the challenge.


require "repeating_numbers"

describe RepeatingNumbers do

  describe "with the input of 0" do
    it "returns 0" do
      processor = RepeatingNumbers.new("0")
      expect(processor.process).to eq(0)
    end
  end

  describe "with the input of 124489903108444899" do
    it "returns a the associated challenge output" do
      processor = RepeatingNumbers.new("124489903108444899")
      expect(processor.process).to eq({
          '44899' => 2, '4489' => 2, '4899' => 2,
          '448' => 2, '489' => 2, '899' => 2, '44' => 3, '48' => 2, '89' => 2,
          '99' => 2
      }.sort)
    end
  end

  describe "with the input of 82156821568221" do
    it "returns a the associated challenge output" do
      processor = RepeatingNumbers.new("82156821568221")
      expect(processor.process).to eq({
        '8215682' => 2, '821568' => 2, '215682' => 2, '82156' => 2,
        '21568' => 2, '15682' => 2, '8215' => 2, '2156' => 2, '1568' => 2,
        '5682' => 2, '821' => 2, '215' => 2, '156' => 2, '568' => 2,
        '682' => 2, '82' => 3, '21' => 3, '15' => 2, '56' => 2, '68' => 2
      }.sort)
    end
  end

  describe "with the input of 11111011110111011" do
    it "returns a the associated challenge output" do
      processor = RepeatingNumbers.new("11111011110111011")
      expect(processor.process).to eq({
        '11110111' => 2, '1111011' => 2, '1110111' => 2, '111101' => 2, '111011' => 3,
        '110111' => 2, '11110' => 2, '11101' => 3, '11011' => 3, '10111' => 2, '1111' => 3,
        '1110' => 3, '1101' => 3, '1011' => 3, '0111' => 2, '111' => 6, '110' => 3,
        '101' => 3, '011' => 3, '11' => 10, '10' => 3, '01' => 3
      }.sort)
    end
  end

  describe "with the input of 98778912332145" do
    it "returns a the associated challenge output" do
      processor = RepeatingNumbers.new("98778912332145")
      expect(processor.process).to eq(0)
    end
  end
end

 

 

781 Comments