Python program to remove invalid parentheses

This is a Leetcode problem –

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note –

The input string may contain letters other than the parentheses ( and ).

def remove_invalid_parentheses(s):      removed = 0     results = {s}     count = {"(": 0, ")": 0}     for i, c in enumerate(s):         if c == ")" and count["("] == count[")"]:             new_results = set()             while results:                 result = results.pop()                 for j in range(i - removed + 1):                     if result[j] == ")":                         new_results.add(result[:j] + result[j + 1:])             results = new_results             removed += 1         else:             if c in count:                 count[c] += 1     count = {"(": 0, ")": 0}     i = len(s)     ll = len(s) - removed     for ii in range(ll - 1, -1, -1):         i-=1         c = s[i]         if c == "(" and count["("] == count[")"]:             new_results = set()             while results:                 result = results.pop()                 for j in range(ii, ll):                     if result[j] == "(":                         new_results.add(result[:j] + result[j + 1:])             results = new_results             ll -= 1         else:             if c in count:                 count[c] += 1     return list(results) 

Explanation –

  • Scan from left to right, make sure count["("] >= count[")"].
  • Then scan from right to left, make sure count["("] <= count[")"].

Here are some inputs/outputs –

print(remove_invalid_parentheses("()())()")) >>> ["()()()", "(())()"]  print(remove_invalid_parentheses("(a)())()")) >>> ["(a)()()", "(a())()"]  print(remove_invalid_parentheses(")(")) >>> [""] 

Here are the times taken for each output –

%timeit remove_invalid_parentheses("()())()") 6.54 µs ± 176 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)  %timeit remove_invalid_parentheses("(a)())()") 8.43 µs ± 1.29 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)  %timeit remove_invalid_parentheses(")(") 3.69 µs ± 67.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 

So, I would like to know whether I could make this program shorter and more efficient.

Any help would be highly appreciated.

Python program for Word Search II

This is a Leetcode problem –

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of a sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Note:

  • All inputs are consist of lowercase letters a-z.
  • The values of words are distinct.

Here is my solution to this challenge –

class Solution:     def __init__(self, board, words):         self.board = board         self.words = words      def find_words(self, board, words):          root = {}         for word in words:             node = root             for c in word:                 node = node.setdefault(c, {})             node[None] = True         board = {i + 1j * j: c                  for i, row in enumerate(board)                  for j, c in enumerate(row)}          found = []         def search(node, z, word):             if node.pop(None, None):                 found.append(word)             c = board.get(z)             if c in node:                 board[z] = None                 for k in range(4):                     search(node[c], z + 1j ** k, word + c)                 board[z] = c         for z in board:             search(root, z, '')          return found 

Program explanation – I first build a tree of words with root root and also represent the board a different way, namely as a one-dimensional dictionary where the keys are complex numbers representing the row/column indexes. That makes further work with it easier. Looping over all board positions is just for z in board, the four neighbors of a board position z are just z + 1j ** k (for k in 0 to 3), and I don’t need to check borders because board.get just returns None if I request an invalid position.

After this preparation, I just take the tree and recursively dive with it into each board position. Similar to how you’d search a single word, but with the tree instead.

Here is an example input/output –

output = Solution([   ['o','a','a','n'],   ['e','t','a','e'],   ['i','h','k','r'],   ['i','f','l','v'] ], ["oath","pea","eat","rain"])  print(output.find_words([   ['o','a','a','n'],   ['e','t','a','e'],   ['i','h','k','r'],   ['i','f','l','v'] ], ["oath","pea","eat","rain"]))  >>> ['oath', 'eat'] 

So, I would like to know whether I could make this program shorter and more efficient.

Any help would be highly appreciated.

How to learn development of data pipeline using python, luigi or airlfow?

I am currently learning building data pipeline for data analysis. I search for online courses for the same but couldn’t find one which matches this. It will be very helpful if community can guide me how to proceed with data pipeline for data analysis. I have already followed Pandas tutorial and know basics of SQL and RDBMS. I found about Luigi and airflow for data pipeline development but not sure which one to learn and use. If there is any course for learning data pipeline then it will be very helpful for me. Thanks in advance.

Python Hangman Problems

I’ve been trying to create Hangman in Python for a school assessment but I don’t understand what is going wrong. My problem here is that after the user guesses the final letter, it keeps on repeating.

import random import time  name = input("What is your name? ") print(name + ", ay?") time.sleep(1) start = input("Up for a game of Hangman?(y/n) ")  lis = random.choice(["yet"]) dash = [] while len(dash) != len(lis):    dash.append("_")  guess = [] guesscomb = "".join(guess) wrongcount=int(0) alphabet = "abcdefghijklmnopqrstuvwxyz"  if start == "y":     print("One game of Hangman comin' right up,",name)     letter = input("Alright then, Guess a letter: ")     thing = ''.join(dash)      while guesscomb != thing:         if letter == "" or letter == " " or len(letter) != 1:             print("I don't understand. Please only use singular letters.")             letter = input("Guess a letter: ")              elif letter in lis and letter in alphabet:             print("Nice!")              location = lis.find(letter)             dash[location] = letter             guess.append(letter)             alphabet.replace(letter," ")             guesscomb = "".join(guess)              letter = input("Guess a letter: ")          else:             print("Wrong.")             wrongcount = wrongcount + 1              print("Total Mistakes:",wrongcount)             letter = input("Guess a letter: ")        elif start == "n":     input("Shame.")     quit()  print("Good Job!") time.sleep(10) ```  

How to sort dictionary of string by key in numerical order in Python

this is different from that post, as the same method does not work.

here is the code comes from that post.

from collections import OrderedDict  d = {'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40} OrderedDict(sorted(d.items(), key=lambda t: t[0])) 

the items is in a numerical order, which is as expected.

in my case, this does not work

d = {'0': 'index-0',  '1': 'index-1',  '13': 'index-13',  '2': 'index-2',  '20': 'index-20',  '21': 'index-21',  '22': 'index-22',  '23': 'index-23',  '3': 'index-3'} OrderedDict(sorted(d.items(), key=lambda t: t[0])) 

the result should be:

{'0': 'index-0',  '1': 'index-1',  '2': 'index-2',  '3': 'index-3'  '13': 'index-13',  '20': 'index-20',  '21': 'index-21',  '22': 'index-22',  '23': 'index-23',} 

what am i missing?

Python program to get the first word

Given a string the task it to find its first word with some rules:

  • The string can have points and commas
  • A word can start with a letter, a point or space
  • One word can contain one apostrophe and it stills being a valid one

For example:

assert first_word("Hello world") == "Hello" assert first_word(" a word ") == "a" assert first_word("don't touch it") == "don't" assert first_word("greetings, friends") == "greetings" assert first_word("... and so on ...") == "and" assert first_word("hi") == "hi" assert first_word("Hello.world") == "Hello" 

The code:

def first_word(text: str) -> str:     """         returns the first word in a given text.     """     text = re.sub("[^A-Za-z'\s.]",'',text)     words = text.split()     for word in words:         for i in range(len(word)):             if word[i].isalpha() or word[i] == "'":                 if i == len(word) - 1:                     if word.find('.') != -1:                         return word.split('.')[0]                     else:                         return word 

How could we improve it?

Python program to solve the “rod-cutting problem”

An assignment at school required me to write a program for this task:

In the rod-cutting problem, we are given a rod of length n inches and a table of prices p[i] for i = 1, 2, …, n. Here p[i] is the price of a rod of length iinches. We have to find the optimal way of cutting the rod so that maximum revenue can be generated by selling the pieces.

Here is my solution to this task (in Python):

def cut_rod(p, n):     """     Take a list p of prices and the rod length n and return lists r and s.     r[i] is the maximum revenue that you can get and s[i] is the length of the     first piece to cut from a rod of length i.     """     # r[i] is the maximum revenue for rod length i     # r[i] = -1 means that r[i] has not been calculated yet     r = [-1]*(n + 1)      # s[i] is the length of the initial cut needed for rod length i     # s[0] is not needed     s = [-1]*(n + 1)      cut_rod_helper(p, n, r, s)      return r, s  def cut_rod_helper(p, n, r, s):     """     Take a list p of prices, the rod length n, a list r of maximum revenues     and a list s of initial cuts and return the maximum revenue that you can get     from a rod of length n.      Also, populate r and s based on which subproblems need to be solved.     """     if r[n] >= 0:         return r[n]      if n == 0:         q = 0     else:         q = -1         for i in range(1, n + 1):             temp = p[i] + cut_rod_helper(p, n - i, r, s)             if q < temp:                 q = temp                 s[n] = i     r[n] = q      return q   n = int(input('Enter the length of the rod in inches: '))  # p[i] is the price of a rod of length i # p[0] is not needed, so it is set to None p = [None] for i in range(1, n + 1):     price = input('Enter the price of a rod of length {} in: '.format(i))     p.append(int(price))  r, s = cut_rod(p, n) print('The maximum revenue that can be obtained:', r[n]) print('The rod needs to be cut into length(s) of ', end='') while n > 0:     print(s[n], end=' ')     n -= s[n] 

where s[n] gives us the length of the first piece, s[n – s[n]] gives us the length of the second piece and so on.

Here is an example output –

Enter the length of the rod in inches: 3 Enter the price of a rod of length 1 in: 3 Enter the price of a rod of length 2 in: 7 Enter the price of a rod of length 3 in: 2 The maximum revenue that can be obtained: 10 The rod needs to be cut into length(s) of 1 2 

Times taken for each function –

%timeit cut_rod(p, n) 7.65 µs ± 71.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)  %timeit cut_rod_helper(p ,n, r, s) 266 ns ± 3.97 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) 

So, I would like to know whether I could make this program shorter and more efficient.

Any help would be highly appreciated.

python calculator Error

I try to do Calculator but i have error with formatting

this the error:

lblr[‘text’] = (‘Result” $ s $ s $ s = %s’ % (n1,ope,n2,round(r,2))) TypeError: not all arguments converted during string formatting

this my app:

import tkinter from tkinter import ttk     frm = tkinter.Tk() frm.geometry('700x300')  fnt= 'None 30 bold'  lblnum1 = ttk.Label(frm,text='Number 1:',font=fnt) lblnum2 = ttk.Label(frm,text='Number 2:', font=fnt) sv1 = tkinter.StringVar() sv2 = tkinter.StringVar() txtnum1 = ttk.Entry(frm,font=fnt,textvariable=sv1) txtnum2 = ttk.Entry(frm,font=fnt,textvariable=sv2) sv1.set('0') sv2.set('0') lblnum1.grid(row=0,column=0,pady=10,padx=10) txtnum1.grid(row=0,column=1,pady=10,padx=10) lblnum2.grid(row=1,column=0,pady=10,padx=10) txtnum2.grid(row=1,column=1,pady=10,padx=10)   def calc(ope):    strn1 = str(txtnum1.get())    strn2 = str(txtnum2.get())    n1 = int(strn1)    n2 = int(strn2)    r = 0    if ope=='+': r = n1 + n2    elif ope=='-': r = n1 - n2    elif ope=='*': r = n1 * n2    else:        if n2==0: n2=1        r = n1 / n2    lblr['text'] = ('Result" $  s $  s $  s = %s' % (n1,ope,n2,round(r,2)))        btns = ttk.Style()  btns.configure('TButton',font=fnt,padding=10)  frame = tkinter.Frame(frm)  btn_width = 5   btnadd = ttk.Button(frame,text='+',width=btn_width,command=lambda:calc('+')) btnasub = ttk.Button(frame,text='-',width=btn_width,command=lambda:calc('-')) btnmul = ttk.Button(frame,text='*',width=btn_width,command=lambda:calc('*')) btndiv = ttk.Button(frame,text='/',width=btn_width,command=lambda:calc('/'))   frame.grid(row=2, column=0,columnspan=2) btnadd.grid(row=0,column=0) btnasub.grid(row=0,column=1) btnmul.grid(row=0,column=2) btndiv.grid(row=0,column=3)  lblr = ttk.Label(frame,text='Result:',font=fnt) lblr.grid(row=1,column=0,columnspan=4,padx=10)                      frm.mainloop() 

input(“Press enter to Exit..”)

What do I and n stand for in python?

So there was a different question about this, my confusion lies in, can you use any letter to mean i or n? Does it have to be the letters i, and n? or can it be t and q? or anything.

Example (say i defined the range… I’m still a early beginner)


for i in range(10)

or

for q in range(10)

Are they the same thing?

Can get regex to work in python that works in regex101 to capture citations

I am trying to capture brackets of text that have at least one digit in them (think citations). This is my regex now, and it works fine: https://regex101.com/r/oOHPvO/5

\((?=.*\d).+?\) 

So I wanted it to capture (Author 2000) and (2000) but not (Author).

I am trying to use python to capture all these brackets, but in python it also captures the text in the brackets even if they don’t have digits.

import re  with open('text.txt') as f:     f = f.read()  s = "\((?=.*\d).*?\)"  citations = re.findall(s, f)  citations = list(set(citations))  for c in citations:     print (c) 

Any ideas what I am doing wrong?