**Implement** a valid parentheses checker in Python

Keep your skills sharp and start learning Python by creating a valid parentheses checker. There is no better way than to make projects.

To create a valid parentheses checker you will learn.

**Syntax and semantics of Python language**. Creating a parentheses checker requires a good understanding of Python’s syntax and semantics. You will learn how to use conditional statements, loops, and string manipulation functions to check if parentheses are balanced or not.**Problem-solving skills**. Building a parentheses checker requires problem-solving skills. You will need to think through the logic of how to check if parentheses are balanced or not and how to handle different scenarios such as nested parentheses.**Algorithmic thinking**. You will need to develop an algorithm to solve the problem of checking for balanced parentheses. This involves breaking down the problem into smaller steps and designing a sequence of instructions to solve the problem.**Debugging skills**. As with any programming project, debugging skills are crucial. You will learn how to identify and fix errors in your code, which is an essential skill for any programmer.**Data structures**. You will likely use data structures such as stacks or queues to implement the parentheses checker algorithm. By building the checker, you will gain a deeper understanding of these data structures and how to use them effectively in your code.**Code organization**. Writing a valid parentheses checker involves creating functions and modularizing your code. This will help you understand how to organize your code in a way that makes it easy to read, maintain, and reuse.

**Project** Description

A valid parentheses string satisfies the following:

- Every opening bracket must have a matching closing bracket of the same type.
- The brackets should be closed in the correct order.

Examples of valid

`()`

`(())`

`()()`

`(()())`

Examples of invalid

`(()`

`)(`

`())(`

`((())`

### Advanced

Can you solve it if the string contains simple, curly, and square braces: `()[]{}`

Examples of valid

`()[]`

`([])`

`(){}`

`([]{})`

Examples of invalid

`([)]`

`(}(]`

`()(}`

`({][})`

The advanced version is a classical problem to solve in job interviews.

But first, start with the simple version. It can help you get started.

**Simple** valid parentheses checker

If you think about it, all you need is to keep track of the following.

- How many parentheses are being opened?
- How many are being closed?

While this is not the whole story you need to add a bit more.

- Have a variable of how many opening parentheses are not being closed yet.
- Then if you see one opening parentheses, then add one.
- If you see closing parentheses, subtract one, if the value becomes negative, then it is not a valid string of parentheses.
- After parsing the full string of parentheses, the value of the variable should be zero.

This can be implemented as follows using variables, conditional, and loops.

```
def matching_parentheses(s):
no_open = 0
for c in s:
if c == ')':
no_open -= 1
elif c == '(':
no_open += 1
if no_open < 0:
return False
if no_open != 0:
return False
return True
```

**Advanced** valid parentheses checker

The complexity suddenly becomes bigger.

Because now you need to keep track of the order of opening and closing parentheses.

A closing parenthesis needs to match the opening.

To accomplish that you can use a stack. A stack can be implemented by using a Python list.

```
def matching_parentheses(s):
stack = []
for c in s:
if c in '([{':
stack.append(c)
if c in ')]}':
if len(stack) == 0:
return False
top = stack.pop()
if top == '(' and c != ')':
return False
if top == '[' and c != ']':
return False
if top == '{' and c != '}':
return False
if len(stack) != 0:
return False
return True
```

Now, that is great.

## Want more Python projects?

This is part of 19 Python Projects and you can create a four in a row game with Python and get 3 core programming skills.

## Python for Finance: Unlock Financial Freedom and Build Your Dream Life

Discover the key to financial freedom and secure your dream life with Python for Finance!

Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

Python for Finance a 21 hours course that teaches investing with Python.

Learn **pandas**, **NumPy**, **Matplotlib** for Financial Analysis & learn how to Automate Value Investing.

*“Excellent course for anyone trying to learn coding and investing.”* – **Lorenzo B.**