Skip to content

Instantly share code, notes, and snippets.

@danielborowski
Created June 1, 2017 20:13
Show Gist options
  • Select an option

  • Save danielborowski/ef1edd86045e63006331966151b7ac14 to your computer and use it in GitHub Desktop.

Select an option

Save danielborowski/ef1edd86045e63006331966151b7ac14 to your computer and use it in GitHub Desktop.
public static String QuestionsMarks(String str) {
str = str.replaceAll("[a-z]+","");
Pattern pattern = Pattern.compile("([0-9])([?])([?])([?])([0-9])");
Pattern pattern01 = Pattern.compile("([0-9])([?])([?])([0-9])");
Matcher matcher01 = pattern01.matcher(str);
Pattern pattern02 = Pattern.compile("([0-9])([?])([0-9])");
Matcher matcher02 = pattern02.matcher(str);
Matcher matcher = pattern.matcher(str);
if (matcher01.find() || matcher02.find()) {
return "false";
} else if (matcher.find()) {
return "true";
}
return "false";
}
@dbaber

dbaber commented Feb 14, 2018 •

Copy link
Copy Markdown

Does this really pass the examples? Where's the check that the numbers add up to 10? Wouldn't this fail on any numbers not separated by 3 question marks?

I hacked this together in like 5-10 mins. I noticed that Coderbyte doesn't support Perl :(.

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

sub question_marks {
  my $str = shift;
  $str =~ s/[^\d?]//g;

  my @matches = grep { /(\d)[?]*(\d)/ && $1 + $2 == 10 } ( $str =~ /(?=(\d[?]*\d))/g );
  return "false" unless @matches;

  for my $match (@matches) {
    if ( $match !~ /(\d)[?]{3}(\d)/ ) {
      return "false";
    }
  }

  return "true";
}

say question_marks("arrb6???4xxbl5???eee5");      # true
say question_marks("acc?7??sss?3rr1??????5");     # true
say question_marks("5??aaaaaaaaaaaaaaaaaaa?5?5"); # false
say question_marks("9???1???9???1???9");          # true
say question_marks("aa6?9");                      # false
say question_marks('abcd&$');                     # false
say question_marks("26??476");                    # false
say question_marks("1???9lkjlkj???18???lk2");     # true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment