Created
March 28, 2014 18:34
-
-
Save psyomn/9839866 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Solution for /r/dailyprogrammer | |
| -- http://www.reddit.com/r/dailyprogrammer/comments/217klv/ | |
| -- | |
| -- Compile and run with: | |
| -- gnatmake brackets.adb && ./brackets | |
| -- @author Simon Symeonidis | |
| with ada.text_io; use ada.text_io; | |
| with ada.exceptions; use ada.exceptions; | |
| with ada.strings.unbounded; use ada.strings.unbounded; | |
| with ada.text_io.unbounded_io; use ada.text_io.unbounded_io; | |
| procedure brackets is | |
| type Character_Array is array (Positive range <>) of Character; | |
| open_brackets : Character_Array := ('[', '(', '{'); | |
| closed_brackets : Character_Array := (']', ')', '}'); | |
| -- if element exists in collection | |
| function is_included(element : Character; collection : Character_Array) | |
| return Boolean is | |
| counter : Positive := 1; | |
| begin | |
| array_has_element : | |
| loop | |
| exit when counter > collection'length; | |
| if collection(counter) = element then | |
| return true; | |
| end if; | |
| counter := counter + 1; | |
| end loop array_has_element; | |
| return false; | |
| end is_included; | |
| -- is open bracket? | |
| function is_obracket(element : Character) | |
| return Boolean is | |
| begin | |
| return is_included(element => element, collection => open_brackets); | |
| end is_obracket; | |
| -- is closed bracket? | |
| function is_cbracket(element : Character) | |
| return Boolean is | |
| begin | |
| return is_included(element => element, collection => closed_brackets); | |
| end is_cbracket; | |
| -- count the number of open brackets | |
| function count_obrackets(input : String) | |
| return Positive is | |
| brack_count : Integer := 0; | |
| counter : Positive := 1; | |
| begin | |
| through_string : | |
| loop | |
| exit when counter > input'length; | |
| if is_obracket(input(counter)) then | |
| brack_count := brack_count + 1; | |
| end if; | |
| counter := counter + 1; | |
| end loop through_string; | |
| return brack_count; | |
| end count_obrackets; | |
| -- count the number of closed brackets | |
| function count_cbrackets(input : String) | |
| return Positive is | |
| brack_count : Integer := 0; | |
| counter : Positive := 1; | |
| begin | |
| through_string : | |
| loop | |
| exit when counter > input'length; | |
| if is_cbracket(input(counter)) then | |
| brack_count := brack_count + 1; | |
| end if; | |
| counter := counter + 1; | |
| end loop through_string; | |
| return brack_count; | |
| end count_cbrackets; | |
| function index_of_obracket(input : String; level : Integer) | |
| return Positive is | |
| counter : Positive := 1; | |
| current_level : Integer := 0; | |
| obracket_not_found : exception; | |
| begin | |
| loop | |
| exit when counter > input'length; | |
| if is_obracket(input(counter)) then | |
| current_level := current_level + 1; | |
| end if; | |
| if current_level = level then | |
| return counter; | |
| end if; | |
| counter := counter + 1; | |
| end loop; | |
| raise_exception(obracket_not_found'identity, "No deeper bracket level"); | |
| end index_of_obracket; | |
| -- given a nesting level, print the contents of that nest | |
| procedure print_nest(input : String; start_index : Integer) is | |
| counter : Positive := 1; | |
| begin | |
| print_until_next_cbracket : | |
| while (not | |
| (is_obracket(input(start_index + counter)) or | |
| is_cbracket(input(start_index + counter)))) | |
| and counter <= input'length loop | |
| put(input(start_index + counter)); | |
| counter := counter + 1; | |
| end loop print_until_next_cbracket; | |
| end print_nest; | |
| test_message : String := "[world[cruel [hello ]]]"; | |
| procedure print_message(input : String) is | |
| number_open_brackets : Integer := count_obrackets(input); | |
| current_index : Integer := 0; | |
| begin | |
| while number_open_brackets /= 0 loop | |
| current_index := index_of_obracket(input, number_open_brackets); | |
| print_nest(input => input, start_index => current_index); | |
| number_open_brackets := number_open_brackets - 1; | |
| end loop; | |
| end print_message; | |
| input_message : Ada.Strings.Unbounded.Unbounded_String; | |
| begin | |
| input_message := get_line; | |
| print_message(to_string(input_message)); | |
| end brackets; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment