Sha256: 0b1db3e0363f3b031d42cd4e0f5b7635cea7e5ce11b6e5be53c6d6e41c558a6d

Contents?: true

Size: 501 Bytes

Versions: 396

Compression:

Stored size: 501 Bytes

Contents

module Brackets
  extend self
  MAP = {'}' => '{', ']' => '[', ')' => '('}

  def are_valid?(str : String)
    create_stack(filter_brackets(str)).size == 0
  end

  private def filter_brackets(brackets : String) : Array(Char)
    brackets.chars.select { |c| (MAP.keys + MAP.values).includes?(c) }
  end

  private def create_stack(brackets : Array(Char)) : Array(Char)
    brackets.reduce([] of Char) do |stack, b|
      MAP[b]? && MAP[b]? == stack.last? ? stack[0..-2] : stack << b
    end
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.118 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.117 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.116 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.115 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.114 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.113 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.111 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.110 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.109 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.108 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.107 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.106 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.105 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.104 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.103 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.102 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.101 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.100 tracks/crystal/exercises/bracket-push/src/example.cr
trackler-2.2.1.99 tracks/crystal/exercises/bracket-push/src/example.cr