Skip to content

Instantly share code, notes, and snippets.

@tcmacdonald
Created November 9, 2018 20:36
Show Gist options
  • Select an option

  • Save tcmacdonald/ab1f5a237048d8c0e32b0a62a39d3abd to your computer and use it in GitHub Desktop.

Select an option

Save tcmacdonald/ab1f5a237048d8c0e32b0a62a39d3abd to your computer and use it in GitHub Desktop.

Revisions

  1. tcmacdonald renamed this gist Nov 9, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. tcmacdonald created this gist Nov 9, 2018.
    40 changes: 40 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    def question_marks(str)
    # Get any number + chars + number pattern...
    regex = /(\d)[a-zA-Z\?]*(\d)/

    # Iterate over each occurrence and return the MatchData object
    matches = str.to_enum(:scan, regex).map { Regexp.last_match }

    # Iterate over each match and return a boolean meeting the following conditions...
    bools = matches.collect do |match|

    # Do the numbers at the beginning and end of the string add up to 10?
    if match[1].to_i + match[2].to_i == 10
    # Are there exactly 3 question marks within the string?
    match.to_s.scan(/\?/).count == 3
    end

    end.compact # .compact removes any nil entries

    # Evaluate if all items in our array are true and return the result
    !bools.empty? && bools.all?
    end


    # ---------------------------------------- //

    str = "arrb6???4xxbl5???eee5"
    puts "#{str}: #{question_marks(str)}" # true

    str = "aa6?9"
    puts "#{str}: #{question_marks(str)}" # false

    str = "acc?7??sss?3rr1??????5"
    puts "#{str}: #{question_marks(str)}" # true

    str = "dk7???3j??hk8???2jd?k?sj?hfkj"
    puts "#{str}: #{question_marks(str)}" # true

    str = "dk7??3j??hk8???2jd?k?sj?hfkj"
    puts "#{str}: #{question_marks(str)}" # false