Skip to content

Commit

Permalink
Balanced expressions challenge added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caul58 committed Mar 23, 2024
1 parent 9d5ccff commit a2ae627
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*:
 
[< Previous](@previous)           [Home](Introduction)           [Next >](@next)
# Challenge 10: Balanced Expressions
Create a program to check that parenthesis, braces and brackets in a given expression are balanced.
* Balanced means that these symbols are opened and closed in order and correctly.
* Balanced expression: { [ a * ( c + d ) ] - 5 }
* Wrong balanced expression: { a * ( c + d ) ] - 5 }
*/

import Foundation

func isBalanced(expression: String) -> Bool {
var symbolStack: [Character] = []
let openSym: [Character] = ["{", "[", "("]
let closeSym: [Character] = ["}", "]", ")"]
var exit = false
for symbol in expression {
if openSym.contains(symbol) {
symbolStack.append(symbol)
} else if closeSym.contains(symbol) {
if !symbolStack.isEmpty, symbolStack.last == openSym[closeSym.firstIndex(of: symbol)!] {
symbolStack.popLast()
} else {
return false
}
}
}
return symbolStack.isEmpty
}

print("Expression 1 -> \(isBalanced(expression: "{a + b [c] * (2x2)}}}}"))")
print("Expression 2 -> \(isBalanced(expression: "{ [ a * ( c + d ) ] - 5 }"))")
print("Expression 3 -> \(isBalanced(expression: "{ a * ( c + d ) ] - 5 }"))")
print("Expression 4 -> \(isBalanced(expression: "{a^4 + (((ax4)}"))")
print("Expression 5 -> \(isBalanced(expression: "{ ] a * ( c + d ) + ( 2 - 3 )[ - 5 }"))")
print("Expression 6 -> \(isBalanced(expression: "{{{{{{(}}}}}}"))")
print("Expression 7 -> \(isBalanced(expression: "(a"))")
print("Expression 8 -> \(isBalanced(expression: "{ a * ( b } + 5 )"))")



//: [Next](@next)

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [Challenge #7: Counting words](Challenge#07)
* [Challenge #8: From decimal to binary](Challenge#08)
* [Challenge #9: Morse Code](Challenge#09)
* [Challenge #10: Balanced Expressions](Challenge#10)
* [...]
* [Challenge #43: Special by Halloween](Challenge#43)
* [Challenge #44: Boomerang](Challenge#44)
Expand Down
3 changes: 2 additions & 1 deletion Challenges2022.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios' display-mode='raw' buildActiveScheme='true' importAppTypes='true'>
<playground version='6.0' target-platform='macos' display-mode='raw' buildActiveScheme='true' importAppTypes='true'>
<pages>
<page name='Introduction'/>
<page name='Challenge#00'/>
Expand All @@ -12,6 +12,7 @@
<page name='Challenge#07'/>
<page name='Challenge#08'/>
<page name='Challenge#09'/>
<page name='Challenge#10'/>
<page name='Challenge#43'/>
<page name='Challenge#44'/>
<page name='Challenge#45'/>
Expand Down

0 comments on commit a2ae627

Please sign in to comment.