-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Balanced expressions challenge added.
- Loading branch information
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Challenges2022.playground/Pages/Challenge#10.xcplaygroundpage/Contents.swift
This file contains 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
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) | ||
|
This file contains 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
This file contains 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