Topic:   bracket Racket   (Read 3706 times)


0 Members and 1 Guest are viewing this topic.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
bracket Racket
« on: October 24, 2012, 07:43:22 PM »
PROGRAMMING CHALLENGE

so yeah.... we've been a bit lax in our actual programming. I've been drawing for my game, and havent done programming, until today in science class, when I made a temperature converter. I forgot how fun it was, so here is a programming challenge i'm going to undertake, and I hope all of you will make your own/ critique my work. I'll post tomorrow or the day after

Quote
Description:
Write a function, where given a string of arbitrary characters, returns true if all brackets (defined as parentheses, square-brackets, curly-braces, and chevrons) are correctly paired and ordered. This is to say that all brackets, if they enclose other brackets, enclose both the paired opening and closing characters.
Formal Inputs & Outputs:
Input Description:
string data - a given string that may or may not have correctly formed brackets.
Output Description:
Return True or False - true if the given string is correctly bracket formed.
Sample Inputs & Outputs:
"123", "(abc)", "()abc()", and "([<{abc123abc}>])" should return true, but "(abc[123)abc]" (wrong pairing) and "(abc>" (not closed) should return false.
Notes:
This is a very easy problem if you use a specific primitive data-structure.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: bracket Racket
« Reply #1 on: October 24, 2012, 08:16:40 PM »
Someone's caught the formal computer science bug? ;)

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: bracket Racket
« Reply #2 on: October 24, 2012, 09:06:49 PM »
It seems simple enough, I think I'll try to do it in Processing when I don't have an essay to finish. Processing is good for mock-ups and experiments.
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: bracket Racket
« Reply #3 on: October 24, 2012, 10:07:10 PM »
http://www.gamemakersgarage.com/BracketRacket/BracketRacket.html

There's probably a much cleaner way(less line intensive) of doing this. If EQ was active he'd probably have it in under 15 lines.
Here's what I got:
Code: [Select]
var bracketMatches = [{start:"<",close:">"},{start:"{",close:"}"},{start:"(",close:")"}, {start:"[",close:"]"}];
function init() {
var string=prompt("Enter your string to parse: ","<[Your string]>");
var bracketsMatch = true;
var bracketArray = new Array();
for (i = 0; i < string.length; i++) {
if (isStartingBracket(string[i])) {
bracketArray.push(string[i]);
} else if (isClosingBracket(string[i])) {
if (bracketArray.length > 0 && bracketArray[bracketArray.length-1] === getStartingBracket(string[i])) {
//Matches closing bracket so you can pop it from the array.
bracketArray.pop();
} else {
//Does not match closing bracket so abandon ship!
bracketsMatch = false;
i = string.length;
}
}
}
if (bracketArray.length > 0) {
bracketsMatch = false;
}

if (bracketsMatch) {
document.write("The string: <p>"+string+"<p><p> Has matching brackets.");
} else {
document.write("The string: <p>"+string+"<p><p> Does not have matching brackets.");
}
}
function isStartingBracket(string) {
for (i1 = 0; i1 < bracketMatches.length; i1 ++) {
if (string === bracketMatches[i1].start) {return true;}
}
return false;
}
function isClosingBracket(string) {
for (i1 = 0; i1 < bracketMatches.length; i1 ++) {
if (string === bracketMatches[i1].close) {return true;}
}
return false;
}
function getStartingBracket(string) {
for (i1 = 0; i1 < bracketMatches.length; i1 ++) {
if (string === bracketMatches[i1].close) {return bracketMatches[i1].start;}
}
return null;
}

Eugeneepinc


  • GMG Newbie

  • *

  • no avatar

  • Posts: 19

  • Gan is so awesome.
Re: bracket Racket
« Reply #4 on: March 24, 2024, 03:05:28 AM »
I apologise, but, in my opinion, you are mistaken. I can prove it. Write to me in PM.
 
notoriety
https://mmfporn.com/tags/mature-femdom/
@456FgDDY8