6 kyu

Simple string indices

249 of 2,304KenKamau

Description:

In this Kata, you will be given a string with brackets and an index of an opening bracket and your task will be to return the index of the matching closing bracket. Both the input and returned index are 0-based except in Fortran where it is 1-based. An opening brace will always have a closing brace. Return -1 if there is no answer (in Haskell, return Nothing; in Fortran, return 0; in Go, return an error)

Examples

solve("((1)23(45))(aB)", 0) = 10 // the opening brace at index 0 matches the closing brace at index 10
solve("((1)23(45))(aB)", 1) = 3 
solve("((1)23(45))(aB)", 2) = -1 // there is no opening bracket at index 2, so return -1
solve("((1)23(45))(aB)", 6) = 9
solve("((1)23(45))(aB)", 11) = 14
solve("((>)|?(*'))(yZ)", 11) = 14
solve("((1)23(45))(aB)", 0) = 10 -- the opening brace at index 0 matches the closing brace at index 10
solve("((1)23(45))(aB)", 1) = 3 
solve("((1)23(45))(aB)", 2) = -1 -- there is no opening bracket at index 2, so return -1
solve("((1)23(45))(aB)", 6) = 9
solve("((1)23(45))(aB)", 11) = 14
solve("((>)|?(*'))(yZ)", 11) = 14
solve("((1)23(45))(aB)", 0) = Just 10 -- the opening brace at index 0 matches the closing brace at index 10
solve("((1)23(45))(aB)", 2) = Nothing -- there is no opening bracket at index 2, so return "Nothing" instead of -1
solve("((1)23(45))(aB)", 1) ! => 11 (the opening brace at index 1 matches the closing brace at index 11)
solve("((1)23(45))(aB)", 2) ! => 4
solve("((1)23(45))(aB)", 3) ! => 0 (there is no opening bracket at index 3, so return 0)
solve("((1)23(45))(aB)", 7) ! => 10
solve("((1)23(45))(aB)", 12) ! => 15
solve("((>)|?(*'))(yZ)", 12) ! => 15

Input will consist of letters, numbers and special characters, but no spaces. The only brackets will be ( and ).

More examples in the test cases.

Good luck!

Strings
Algorithms

Stats:

CreatedDec 3, 2017
PublishedDec 4, 2017
Warriors Trained3811
Total Skips252
Total Code Submissions8226
Total Times Completed2304
Python Completions689
JavaScript Completions787
Ruby Completions91
Haskell Completions52
C++ Completions235
Crystal Completions11
Fortran Completions14
Java Completions249
NASM Completions35
Go Completions127
C Completions150
Clojure Completions9
Total Stars57
% of votes with a positive feedback rating93% of 522
Total "Very Satisfied" Votes456
Total "Somewhat Satisfied" Votes59
Total "Not Satisfied" Votes7
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • KenKamau Avatar
  • jhoffner Avatar
  • GiacomoSorbi Avatar
  • donaldsebleung Avatar
  • Voile Avatar
  • achtlos Avatar
  • Bubbler Avatar
  • rsa Avatar
  • FArekkusu Avatar
  • uniapi Avatar
  • siliconbrain Avatar
  • user8436785 Avatar
  • saudiGuy Avatar
Ad