Faced an interview question in Python that was as follow?
ex:
input = ('192.168.15.1', '.', -1) ===> output = (192, 168, 15, 1)
input = ('192.168.15.1', '.', 2) ===> output = ( 192, 168, 15.1 )
Solve such that input(string,char,integer) will give the output such that the string is splited by the character and if integer is 0 or less than that else string is splited the number of by character where character split only occur for the value of integer.
I have written the code although it works fine within boundary and no error condition. Is there a better code out there plese share.
Thanks
def split_string(string,ch,inte):
q = string
c = ch
i = inte
s =list(q)
#Check if integer value is negative or positive
if i <= 0:
# split the string one time
r1 = split_once(s,c)
print r1
else:
#split according to the integear value
r2 = split_acc_intger(s,c,i)
print r2
def split_once(s,c):
y = 0
d = []
for x in range(len(s)):
if s[x] == c:
p=s[y:x]
d.append(''.join(p))
y = x + 1
elif x == len(s)-1:
p=s[y:]
d.append(''.join(p))
return d
def split_acc_intger(s,c,i):
y = 0
d =[]
count = 1
for x in range(len(s)):
# the leat number will 1
if s[x] == c:
p=s[y:x]
d.append(''.join(p))
y = x + 1
count += 1
elif count == i :
p=s[y:]
d.append(''.join(p))
break
return d
Simple and recursive.
def split_str(s,c,i):
if i == 0:
return [s]
else:
head, _, rest = s.partition(c)
if rest:
return [head] + split_str(rest, c, i - 1)
return [head]
def split(text, sep, maxsplit=-1):
parts = []
end = -1
while True:
start = end + 1
end = text.find(sep, start)
if (end == -1) or (maxsplit == 0):
parts.append(text[start:])
break
else:
parts.append(text[start:end])
if maxsplit != 0: maxsplit -= 1
return parts
print(split('192.168.15.1', '.', -1)) # ['192', '168', '15', '1']
print(split('192.168.15.1', '.', 2)) # ['192', '168', '15.1']
Something like this would work:
def splitter(string_, splitchar, maxsplits):
# if maxsplits is positive, split string into maxsplits of parts, on splitchar.
# Otherwise perform as many splits as the string allows.
out = []
sub = []
for ch in string_:
if ch != splitchar:
sub.append(ch)
else:
if maxsplits < 1 or (maxsplits > 0 and (len(out) < maxsplits)):
out.append(''.join(sub))
sub = []
else:
sub.append(ch)
out.append(''.join(sub))
return tuple(out)
>>> splitter.splitter('192.168.15.1', '.', -1)
('192', '168', '15', '1')
>>> splitter.splitter('192.168.15.1', '.', 2)
('192', '168', '15.1')
>>> splitter.splitter('192.168.15.1', '.', 0)
('192', '168', '15', '1')
>>> splitter.splitter('192.168.15.1', '.', 1)
('192', '168.15.1')
another simpler and short approach is you can use regular expressions like this:
import re
def split_without_using_split_fn(s, c, i):
if i<=0:
print(re.findall("[^"+c+"]+(?=\\"+c+"|$)", s))
else:
l=re.findall("[^"+c+"]+(?=\\"+c+"|$)", s)
print(l[0:-i]+[c.join(l[i:])])
split_without_using_split_fn(*('192.168.15.1', '.', -1))
split_without_using_split_fn(*('192.168.15.1', '.', 2))
Output:
$ python3 p.py
['192', '168', '15', '1']
['192', '168', '15.1']
$
You could solve this task with following function
def split_string(istr,ich,inte):
res = []
prev = 0
for i,ch in enumerate(istr):
if ch == ich:
res.append(istr[prev:i])
prev = i+1
inte = inte-1
if inte == 0:
break
if prev < len(istr):
res.append(istr[prev:])
return res
or another solution
def split_string(istr,ich,inte):
res = []
h,_,r = istr.partition(ich)
while r:
res.append(h)
inte = inte-1
if inte == 0:
h = r
break
h,_,r = r.partition(ich)
if h:
res.append(h)
return res
For following code
print split_string('192.168.15.1', '.', -1)
print split_string('192.168.15.1', '.', 2)
output will be
['192', '168', '15', '1']
['192', '168', '15.1']
Related
I am writing a program in python that take some string and test it through some condition
the first 2 characters in the sting must be letters
2 . the steer must be maximum of 6 characters and at least 2 characters
3 . Numbers cannot be used in the middle of the string; they must come at the end. For example, AAA222 would be an acceptable … ; AAA22A would not be acceptable
4 . The first number used cannot be a ‘0’.”
5 . [' ', ',', ';', '-', '_'] thos characters are not allowed
this is my code so far
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
total = is_N_1(s) + is_N_2(s) + is_N_3(s) + is_N_4(s) + is_N_5(s)
if total == 5:
#print (total)
return True
else:
#print(total)
return False
def is_N_1(s):
if len(s)<7 and len(s)>3:
return 1
else:
return 0
def is_N_2(s):
if s[0:2].isalpha():
return 1
else:
return 0
def is_N_3(s):
for i in s:
if s[-1].isalpha() and i.isnumeric():
return 0
else:
return 1
def is_N_4(s):
t = []
for i in s:
if i.isdigit():
t.append(i)
if len(t)<=0:
return 1
else:
if t[0] == '0':
return 0
else:
return 1
def is_N_5(s):
not_allow =[' ', ',', ';', '-', '_']
for i in s :
for _ in not_allow :
if i == _:
return 1
else :
return 0
main()
this is the input that give an error in the output
1 . input of "CS50"
expected "Valid", not "Invalid\n"
2 . input of "ECTO88"
expected "Valid", not "Invalid\n"
3 . input of "NRVOUS"
expected "Valid", not "Invalid\n"
can any one take a look at this and tell me what I did wrong, I stack for 2 hours and I did not find the solution?
Below is how to check the string in s one by one for not allowed characters within not_allow. I think that just fixing this part will solve it.
def is_N_5(s):
not_allow =[' ', ',', ';', '-', '_']
for i in s :
if i in not_allow:
return 0
return 1
In the 5th condition, you should reverse the return values. Now you return 1 when one of not allowed chars is used.
def is_N_5(s):
not_allowed = [' ', ',', ';', '-', '_']
for i in s:
if i in not_allowed:
return 0
return 1
Your function is_N_5 has an invalid reference.
not_allwo should be updated to not_allow
def is_N_5(s):
not_allow =[' ', ',', ';', '-', '_']
for i in s :
for _ in not_allow :
if i == _:
return 1
else :
return 0
you can use this method srt.isalum() in function
is_N_5
if s.isalum(): return
els
To draw a set of objects in a breadth first search manner
To get this out of the way in the beginning, I'm quite a noob at programming in general so please pardon the obviously noobish things I would've done in the following project.
The goal of this project is to get a graph structure if you give it a table of nodes, their descriptions and the nodes that it is connected to. The hurdle though is that I cannot use any external libraries to do this apart from matplotlib.
The input would look something like this:
inputs as a table
The expected output should be something along the lines of this: (note this diagram ignores the dimensions)
Expected output
The logic which I developed is:
Convert the raw list data into an class object which has all the attributes from the list.
2)Proceed to searching the nodes in a breadth first manner
3)Put the sequence of spaces that the BFS searched into a list
4)Then draw out the spaces as they appeared in the list (note the distance between them is necessary for what I need it for)
How far I got:
I figured out everything till step no 3. The problem I have is that I have to set a x and y position for each space now.
To break down what I have in mind is that :
If the first node is an entry type node then it draws it out in the middle of the maximum breadth of the graph
Count the number of external connections it has and then draw them out as (maximum breadth /number of connections , incrementing through them. So, the living room has none so it will draw it out in the middle. The living room has 5 connections ( out of which one has already been drawn i.e. the entry so we iterate through the next set of four spaces only moving in the x axis) Then we step up the y and draw again.
To execute this I intended to have a loop which goes though the ordered list of spaces and have a counter. The process:
1)if the node encountered is one of the type of entry node then we draw out that node and set the counter to number of connections.
2)if the counter is 1 then we move the Y axis up. and draw out that space
3)if the counter is > 1 then we divide the the counter value by the maximum depth value
The problem I have:
The example shown here only takes into account that we have one entry.
2)The ordering of spaces is wrong when using the counter method.
3) The counter method doesnt draw out the termination nodes properly.
And when I actually write it down in python I get this, which means I've clearly failed:
The output which I got
The script as it stands now:
Pastebin link
And at this point I'm out of ideas as how I should actually fix this problem. Any help is much appreciated :)
#imports
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from math import pi , sin
#raw data
connections = ['1', '0,2,5,7,8', '1,3', '2,4', '3', '1,6', '5', '1', '1']
indices = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
spaceTags = ['entry ', 'living area', 'dining area', 'Kitchen', 'Utility',
'Bedroom1', 'Toilet attached', 'Toilet common', 'Bedroom2']
area = ['1', '40', '20', '15', '6', '20', '6', '6', '20']
minimumDimension = ['1', '5', '4', '3', '2', '4', '2', '2', '4']
#define the space class
class Space(object):
def __init__(self,index,spaceName,connections,area,minimumDimension):
self.index = index
self.spaceName = spaceName
self.connections = connections
self.area = area
self.ydim = minimumDimension
self.xdim = (self.area / self.ydim)
self.x = 0
self.y = 0
self.isExplored = False
self.polarAngle = 0
self.spaceType = 0
# 0= not assigned ; 1 = entry; 2 = intermediate; 3 = termination
def ObjectAttributes(self):
return (self.index,
self.spaceName,
self.connections,
self.area,
self.y,self.x,
self.xdim,self.ydim,
self.isExplored,
self.spaceType)
#definations
#process integers
def convert_to_int_vals (input_list):
output_list = []
i = 0
while i < len(input_list):
output_list.append(int(input_list[i]))
i += 1
return output_list
#process floats
def convert_to_float_vals (input_list):
output_list = []
i = 0
while i < len(input_list):
output_list.append(float(input_list[i]))
i += 1
return output_list
#process 2D lists for connections
def process_2d_connections (input_list):
output_list = []
for item in input_list:
if len(item) <= 1:
lst = []
lst.append(int(item))
output_list.append(lst)
else:
var = item
var = (var.split(','))
var = convert_to_int_vals(var)
output_list.append(var)
return output_list
#make data into objects i.e. spaces
def convertDataToSpaces(index,spaceTag,connections,area,minimumDimension):
print('Processing data...')
if (len(index)==len(spaceTag)==len(connections)==len(area)==len(minimumDimension)):
i = 0
output_list = []
while i < len(spaceTag):
space = Space(index[i],spaceTag[i],connections[i],area[i],minimumDimension[i])
output_list.append(space)
i += 1
print('Done.')
return output_list
else:
print('Number of lists dont match')
#find first node
def FindFirstNode(spaceList):
output = 'null'
for item in spaceList:
if item.spaceName == 'entry ' or item.spaceName =='entry':
item.spaceType = 1
output = item
if output == 'null':
print('No entry defined. Please define entry!')
return output
#Calculate hypotenuse
def calculate_hypotenuse(arg1,arg2):
val = ((arg1**2)+(arg2**2))**(0.5)
return val
#Calculate max hypotenuse
def calculate_max_hypotenuse (spaceList):
outputval = 0
for item in spaceList:
var = calculate_hypotenuse(item.xdim,item.ydim)
if var > outputval:
outputval = var
else:
outputval
return outputval
# Note this is a FIFO list
def FindAndQueueNextNode(spaceList,searchNode,queue):
searchNode.isExplored = True
if len(searchNode.connections) == 1:
if searchNode.spaceName == 'entry ' or searchNode.spaceName =='entry':
searchNode.spaceType = 1
else:
searchNode.spaceType = 3
elif len(searchNode.connections) > 1:
searchNode.spaceType = 2
else:
searchNode.spaceType = 0
for item in spaceList:
if ( item.index in searchNode.connections) and (item.isExplored == False) :
queue.append(item)
# Calculate the position based on the dimension (inputs are the object dimensions and current floating dim)
def CalculatePosition(currentx, currenty, space):
spaceXdimadjusted = (space.xdim / 2)* -1
spaceYdimadjusted = (space.ydim / 2)* -1
adjustedx = currentx + spaceXdimadjusted
adjustedy = currenty + spaceYdimadjusted
return (adjustedx,adjustedy)
#core algorithm
def coreAlgorithm(spacesList):
## variable holding max hypotenuse distance
grid_dimension = int((calculate_max_hypotenuse(spacesList))*(1.5))
print('Grid dimensions are : ' + str(grid_dimension) + (' units'))
## create empty processing variables
processingQueue = []
orderedListOfSpacesInBFS = []
maxTreeWidth = 0
## find the first space
firstSpace = FindFirstNode(spacesList)
orderedListOfSpacesInBFS.append(firstSpace)
print('The first node is : ' + str(firstSpace.spaceName) +
'; Index being : ' + str(firstSpace.index))
## queue the next space
FindAndQueueNextNode(spacesList,firstSpace,processingQueue)
##start while loop (while queue length loop > 0)
while len(processingQueue) > 0 :
if len(processingQueue) > maxTreeWidth:
maxTreeWidth = len(processingQueue)
else:
maxTreeWidth = maxTreeWidth
item = processingQueue.pop(0)
orderedListOfSpacesInBFS.append(item)
FindAndQueueNextNode(spacesList,item,processingQueue)
## second loop to loop through spaces and draw them
maxXDepthDimension = grid_dimension * maxTreeWidth
ypos = grid_dimension
counter = 0
while len(orderedListOfSpacesInBFS) > 0:
item = orderedListOfSpacesInBFS.pop(0)
if item.spaceType == 1:
xpos = maxXDepthDimension / 2
(item.x , item.y) = CalculatePosition(xpos,ypos, item)
ypos += grid_dimension
counter = len(item.connections)
elif counter == 1:
xpos = maxXDepthDimension / 2
(item.x , item.y) = CalculatePosition(xpos,ypos, item)
ypos += grid_dimension
counter = len(item.connections) - 1
elif counter > 1:
xpos = (maxXDepthDimension / counter)
(item.x, item.y) = CalculatePosition(xpos, ypos, item)
counter -= 1
#draw lines as a separete method
#core algorithm preprocess
def coreAlgorithmLoop (spaces_list):
#check object feasibility and if the algorithm can run.
print('Starting algorithm...')
startrun = False
floatingvartoggle = 1
for item in spaces_list:
if type(item) == Space:
floatingvartoggle = floatingvartoggle * 1
else:
floatingvartoggle = floatingvartoggle * 0
if floatingvartoggle == 1:
startrun = True
else:
print('Objects should be spaces.')
#start of core-algorithm.
if startrun == True:
coreAlgorithm(spaces_list)
#implementation
#pre-process data
indices = convert_to_int_vals(indices)
spaceTags = spaceTags
connections = process_2d_connections(connections)
area = convert_to_float_vals(area)
minimumDimension = convert_to_float_vals(minimumDimension)
#initialize processing
listOfSpaces = convertDataToSpaces(indices,
spaceTags,
connections,
area,
minimumDimension)
coreAlgorithmLoop(listOfSpaces)
#matplotlibtester - start
fig, ax = plt.subplots()
ax.set_xlim((0, 100))
ax.set_ylim((0, 70))
for space in listOfSpaces:
var = space.area
print(space.ObjectAttributes())
rect = patches.Rectangle((space.x,space.y),
space.xdim,space.ydim,0,
linewidth=1,
edgecolor='r',
facecolor='none')
ax.add_patch(rect)
plt.show()
#matplotlibtester - end
So this is awkward but just in-case it helps someone, The method to solve it is:
Create the attributes explored from and depth
Then in the queue keep track of how the node was explored and what it's depth is (the current searchnode is assigned to the items getting queued, the searchnode's depth is equal to the depth of the node that it was explored from +1
Using this create a layered chart to understand where each object goes, (depth is the row/ tier/ level number) and the instance that the object is in that level is the column number
This is easier to do with another class.Here is the modified version of the code:
(note it draws an asymmetric chart which is okay for my purposes and I've still not solved the issue of more than one starting points which is okay for now)
#imports
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from math import pi , sin
#raw data
connections = ['1', '0,2,5,7,8', '1,3', '2,4', '3', '1,6', '5', '1', '1']
indices = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
spaceTags = ['entry ', 'living area', 'dining area', 'Kitchen', 'Utility',
'Bedroom1', 'Toilet attached', 'Toilet common', 'Bedroom2']
area = ['1', '40', '20', '15', '6', '20', '6', '6', '20']
minimumDimension = ['1', '5', '4', '3', '2', '4', '2', '2', '4']
#define the space class
class Space(object):
def __init__(self,index,spaceName,connections,area,minimumDimension):
self.index = index
self.spaceName = spaceName
self.connections = connections
self.area = area
self.ydim = minimumDimension
self.xdim = (self.area / self.ydim)
self.x = 0
self.y = 0
self.isExplored = False
self.isExploredFrom = 0
self.Depth = 0
self.spaceType = 0
# 0= not assigned ; 1 = entry; 2 = intermediate; 3 = termination
self.CentroidX = 0
self.CentroidY = 0
def ObjectAttributes(self):
return (self.index,
self.spaceName,
self.y,self.x,
self.Depth)
class DrawingStructure(object):
def __init__(self,Depth):
self.Depth = Depth
self.numberOfInstances = 0
self.count = 0
#definations
#process integers
def convert_to_int_vals (input_list):
output_list = []
i = 0
while i < len(input_list):
output_list.append(int(input_list[i]))
i += 1
return output_list
#process floats
def convert_to_float_vals (input_list):
output_list = []
i = 0
while i < len(input_list):
output_list.append(float(input_list[i]))
i += 1
return output_list
#process 2D lists for connections
def process_2d_connections (input_list):
output_list = []
for item in input_list:
if len(item) <= 1:
lst = []
lst.append(int(item))
output_list.append(lst)
else:
var = item
var = (var.split(','))
var = convert_to_int_vals(var)
output_list.append(var)
return output_list
#make data into objects i.e. spaces
def convertDataToSpaces(index,spaceTag,connections,area,minimumDimension):
print('Processing data...')
if (len(index)==len(spaceTag)==len(connections)==len(area)==len(minimumDimension)):
i = 0
output_list = []
while i < len(spaceTag):
space = Space(index[i],spaceTag[i],connections[i],area[i],minimumDimension[i])
output_list.append(space)
i += 1
print('Done.')
return output_list
else:
print('Number of lists dont match')
#find first node
def FindFirstNode(spaceList):
output = 'null'
for item in spaceList:
if item.spaceName == 'entry ' or item.spaceName =='entry':
item.spaceType = 1
output = item
if output == 'null':
print('No entry defined. Please define entry!')
return output
#Calculate hypotenuse
def calculate_hypotenuse(arg1,arg2):
val = ((arg1**2)+(arg2**2))**(0.5)
return val
#Calculate max hypotenuse
def calculate_max_hypotenuse (spaceList):
outputval = 0
for item in spaceList:
var = calculate_hypotenuse(item.xdim,item.ydim)
if var > outputval:
outputval = var
else:
outputval
return outputval
# Note this is a FIFO list
def FindAndQueueNextNode(spaceList,searchNode,queue):
searchNode.isExplored = True
if len(searchNode.connections) == 1:
if searchNode.spaceName == 'entry ' or searchNode.spaceName =='entry':
searchNode.spaceType = 1
else:
searchNode.spaceType = 3
elif len(searchNode.connections) > 1:
searchNode.spaceType = 2
else:
searchNode.spaceType = 0
for item in spaceList:
if ( item.index in searchNode.connections) and (item.isExplored == False) :
item.isExploredFrom = searchNode.index
item.Depth = searchNode.Depth + 1
queue.append(item)
# Calculate the position based on the dimension (inputs are the object dimensions and current floating dim)
def CalculatePosition(currentx, currenty, space):
spaceXdimadjusted = (space.xdim / 2)* -1
spaceYdimadjusted = (space.ydim / 2)* -1
adjustedx = currentx + spaceXdimadjusted
adjustedy = currenty + spaceYdimadjusted
return (adjustedx,adjustedy)
#def return only unique values in a list
def ReturnUniqueValues(input_list):
output_list = []
for i in input_list:
if i not in output_list:
output_list.append(i)
return output_list
#core algorithm
def coreAlgorithm(spacesList):
## variable holding max hypotenuse distance
grid_dimension = int((calculate_max_hypotenuse(spacesList))*(1.5))
print('Grid dimensions are : ' + str(grid_dimension) + (' units'))
## create empty processing variables
processingQueue = []
orderedListOfSpacesInBFS = []
maxTreeWidth = 0
## find the first space
firstSpace = FindFirstNode(spacesList)
orderedListOfSpacesInBFS.append(firstSpace)
print('The first node is : ' + str(firstSpace.spaceName) +
'; Index being : ' + str(firstSpace.index))
##initialize first space
firstSpace.Depth = 0
firstSpace.isExploredFrom = 0
FindAndQueueNextNode(spacesList,firstSpace,processingQueue)
##start while loop (while queue length loop > 0)
while len(processingQueue) > 0 :
if len(processingQueue) > maxTreeWidth:
maxTreeWidth = len(processingQueue)
else:
maxTreeWidth = maxTreeWidth
item = processingQueue.pop(0)
orderedListOfSpacesInBFS.append(item)
FindAndQueueNextNode(spacesList,item,processingQueue)
#second loop for figuring out the depth and column number to draw
DepthList = []
uniquelist = []
DrawingList = []
for item in orderedListOfSpacesInBFS:
DepthList.append(item.Depth)
uniquelist = ReturnUniqueValues(DepthList)
for item in uniquelist:
var = DrawingStructure(item)
DrawingList.append(var)
copyList = orderedListOfSpacesInBFS
while len(copyList) > 0:
space = copyList.pop(0)
for thing in DrawingList:
if int(thing.Depth) == int(space.Depth):
thing.numberOfInstances += 1
##actually setting the values to later draw
for item in spacesList:
rowNum = item.Depth
rowData = 'null'
for entry in DrawingList:
if rowNum == entry.Depth:
rowData = entry
colNum = rowData.count
rowData.count += 1
xpos = rowNum * grid_dimension
ypos = colNum * grid_dimension
item.CentroidY = xpos
item.CentroidX = ypos
(item.x, item.y) = CalculatePosition(ypos,xpos,item)
#draw lines as a separete method
def DrawConnectionLines(spacesList):
for item in spacesList:
centroid = [item]
#core algorithm preprocess
def coreAlgorithmLoop (spaces_list):
#check object feasibility and if the algorithm can run.
print('Starting algorithm...')
startrun = False
floatingvartoggle = 1
for item in spaces_list:
if type(item) == Space:
floatingvartoggle = floatingvartoggle * 1
else:
floatingvartoggle = floatingvartoggle * 0
if floatingvartoggle == 1:
startrun = True
else:
print('Objects should be spaces.')
#start of core-algorithm.
if startrun == True:
coreAlgorithm(spaces_list)
DrawConnectionLines(spaces_list)
#implementation
#pre-process data
indices = convert_to_int_vals(indices)
spaceTags = spaceTags
connections = process_2d_connections(connections)
area = convert_to_float_vals(area)
minimumDimension = convert_to_float_vals(minimumDimension)
#initialize processing
listOfSpaces = convertDataToSpaces(indices,
spaceTags,
connections,
area,
minimumDimension)
coreAlgorithmLoop(listOfSpaces)
#matplotlibtester - start
fig, ax = plt.subplots()
ax.set_xlim((-10, 60))
ax.set_ylim((0, 70))
xvals = []
yvals = []
for space in listOfSpaces:
print(space.ObjectAttributes())
rect = patches.Rectangle((space.x,space.y),
space.xdim,space.ydim,0,
linewidth=1,
edgecolor='r',
facecolor='none')
ax.add_patch(rect)
xvals.append(space.CentroidX)
yvals.append(space.CentroidY)
plt.scatter(xvals,yvals)
plt.show()
#matplotlibtester - end
I've implemented the following 'tree sizer' but it fails under certain conditions, the example below returns size 2 when it should return size 4, can anyone help me out. I've written this several times, to no avail, it keeps failing.
def getRPNdepth(expression):
treesize=0
maxtreesize=treesize
mintreesize=treesize
tmpexp=expression
tmpfmla = [1 if n[0] == 'x' else n for n in tmpexp]
print(tmpfmla)
try:
stack = []
for val in tmpfmla:
if val in ['-', '+', '*', '/']:
op1 = stack.pop()
op2 = stack.pop()
if val == '-': result = op2 - op1
if val == '+': result = op2 + op1
if val == '*': result = op2 * op1
if val == '/':
if op1 == 0:
result = 1
else:
result = op2 / op1
stack.append(result)
treesize=treesize+1
else:
stack.append(float(val))
treesize = treesize - 1
if treesize>maxtreesize:
maxtreesize=treesize
if treesize<mintreesize:
mintreesize=treesize
return abs(mintreesize)
except:
print('error validate rpn>' + str(expression))
return 0
xxxx = ['x6', 'x7', '+', 'x7', '+', 'x7', '+', 'x7', '+']
print(getRPNdepth(xxxx))
a couple of examples :
['1','1' ,'+','1','1' ,'+' ,'+']
['1','1','1' ,'+' ,'+']
both give the result of 3, which is correct, but.
['1','1' ,'+','1','1' ,'+' ,'+']
returns 3 when it should be 4
All in all, I need to know the depth of the RPN from its string representation.
Calculating the tree depth is similar to evaluating the expression, but the operators calculate resulting depths instead of resulting values:
def getRPNdepth(expression):
stack = []
for val in expression:
if val in ['-', '+', '*', '/']:
stack.append(max(stack.pop(),stack.pop())+1)
else:
stack.append(1)
return stack.pop()
Well, just did a little of 'cheating' and used my rpn to infix converter to achieve the same goal, I post it here if anyone need it.
def getRPNdepth(expression):
tmpexp = expression
tmpfmla = [1 if n[0] == 'x' else n for n in tmpexp]
stack = []
for val in tmpfmla:
if val!=' ':
if val in ['-', '+', '*', '/']:
op1 = stack.pop()
op2 = stack.pop()
stack.append('(' + str(op1) + str(val) + str(op2) + ')')
else:
stack.append(str(val))
openparentesiscount=0
maxopenparentesiscount = 0
onlyparentesis=''
for c in stack[0]:
if c in ['(', ')']:
onlyparentesis=onlyparentesis+c
if c=='(':
openparentesiscount=openparentesiscount+1
else:
openparentesiscount = openparentesiscount - 1
if openparentesiscount>maxopenparentesiscount:
maxopenparentesiscount=openparentesiscount
return maxopenparentesiscount
Thanks all !
I'm trying to solve a maze recursively and I can get it to go where I want it to (check to make sure it can go a way and then to mark it as been there) but for some reason when it runs into a dead end, it does not recursively go back to an open place to check another path. Is there anything wrong with the way I'm using recursion?
class maze(object):
def __init__(self, maze):
self.maze = maze
self.maze2d = []
n = 0
i = 0
for row in self.maze:
self.maze2d.append([])
print(row)
for col in row:
self.maze2d[i].append(col)
i += 1
print(self.maze2d)
def find_start(self):
x = 0
y = 0
for row in self.maze2d:
for index in row:
if index == "S":
self.maze2d[x][y] = index
return x,y
y += 1
x += 1
y = 0
return -1
def mazeSolver(self,x,y,path):
if self.maze2d[x][y] == "E":
return True
else:
if self.maze2d[x] != 0:
if self.maze2d[x+1][y] == ".":
self.maze2d[x+1][y] = "_"
self.mazeSolver(x+1,y,path + "S")
if self.maze2d[x] < len(self.maze2d):
if self.maze2d[x-1][y] == ".":
self.maze2d[x-1][y] = "_"
self.mazeSolver(x-1,y,path + "N")
if y < len(self.maze2d[x]):
if self.maze2d[x][y+1] == ".":
self.maze2d[x][y+1] = "_"
self.mazeSolver(x,y+1,path + "E")
if self.maze2d[y] != 0:
if self.maze2d[x][y-y] == ".":
self.maze2d[x][y-1] = "_"
self.mazeSolver(x,y-1,path + "W")
and where I'm calling the function and the maze itself:
from eachstep import *
maze1 = []
maze1.append("S..*..")
maze1.append("*...*.")
maze1.append("..*..E")
var1 = maze(maze1)
x,y = var1.find_start()
var1.mazeSolver(x,y,"")
I changed your mazeSolver function with this. And I print the path at the end:
def mazeSolver(self,x,y,path):
if self.maze2d[x][y] == '.':
self.maze2d[x][y] = "_"
if self.maze2d[x][y] == "E":
print path
return True
else:
if x < len(self.maze2d)-1:
if self.maze2d[x+1][y] in ['.','E']:
self.mazeSolver(x+1,y,path + "S")
if x > 0:
if self.maze2d[x-1][y] in ['.','E']:
self.mazeSolver(x-1,y,path + "N")
if y < len(var1.maze2d[x])-1:
if self.maze2d[x][y+1] in ['.','E']:
self.mazeSolver(x,y+1,path + "E")
if y > 0:
if self.maze2d[x][y-y] in ['.','E']:
self.mazeSolver(x,y-1,path + "W")
>>> var1.mazeSolver(x,y,"")
ESEESEE
>>>> var1.maze2d
[['S', '_', '_', '*', '.', '.'],
['*', '_', '_', '_', '*', '.'],
['_', '_', '*', '_', '_', 'E']]
Your code will never reach the end, since it only visits locations with the value ., and the end has the value E.
I want to be able to pair up all parentheses in a string, if they aren't paired then then they get their index number and False. It seems like it is repeating some values over and over, i.e cl == pop[1]. I have tried to see where the problem is but I can't see it no matter how hard I try. So I'm asking if anyone help me to locate the error and maybe even improve my code ;)
def check_parentheses(string):
pending = 0
brackets = []
'''Checks if parens are paired, otherwise they are bad.'''
parenstack = collections.deque()
for ch in string:
if ch in lrmap:
try:
cl = string.index(ch, pending)
pending = cl + 1
except:
cl = False
if ch in lparens:
parenstack.append([ch, cl])
print parenstack
elif ch in rparens:
try:
pop = parenstack.pop()
if lrmap[pop[0]] != ch:
print 'wrong type of parenthesis popped from stack',\
pop[0], ch, pop[1], cl
brackets.append([pop[1], False])
brackets.append([cl, False])
else:
brackets.append([pop[1], cl])
except IndexError:
print 'no opening parenthesis left in stack'
brackets.append([cl, False])
# if we are not out of opening parentheses, we have a mismatch
for p in parenstack:
brackets.append([p[1],False])
return brackets
You can adapt my code to a similar question:
def Evaluate(str):
stack = []
pushChars, popChars = "<({[", ">)}]"
for c in str :
if c in pushChars :
stack.append(c)
elif c in popChars :
if not len(stack) :
return False
else :
stackTop = stack.pop()
balancingBracket = pushChars[popChars.index(c)]
if stackTop != balancingBracket :
return False
else :
return False
return not len(stack)
iparens = iter('(){}[]<>')
parens = dict(zip(iparens, iparens))
closing = parens.values()
def balanced(astr):
stack = []
for c in astr:
d = parens.get(c, None)
if d:
stack.append(d)
elif c in closing:
if not stack or c != stack.pop():
return False
return not stack
Example:
>>> balanced('[1<2>(3)]')
True
>>> balanced('[1<2(>3)]')
False
BRACES = { '(': ')', '[': ']', '{': '}' }
def group_check(s):
stack = []
for b in s:
c = BRACES.get(b)
if c:
stack.append(c)
elif not stack or stack.pop() != b:
return False
return not stack
Thanks hughdbrown your code was a breeze to get working and it's really short! You've just saved me a headache :D
converted it to pep8 if thats ok :)
Edit
Added support for comments and strings, it will not match inside them.
Added support for easy language brace checking, modify the charset dict.
Correctly paires up, i.e right to left
HTML
charset = dict(opening='{[(<',\
closing='}])>',\
string = ('"', "'"),\
comment=(('<!--', '-->')))
Python
charset = dict(opening='{[(<',\
closing='}])>',\
string = ('"', "'"),\
comment=(("'''", "'''"), ('"""', '"""'), ('#', '\n')))
C++
charset = dict(opening='{[(<',\
closing='}])>',\
string = ('"', "'"),\
comment=(('/*', '*/'), ('//', '\n')))
you get the point? :)
charset = dict(opening='{[(<',\
closing='}])>',\
string = ('"', "'"),\
comment=(('<!--', '-->'), ('"""', '"""'), ('#', '\n')))
allowed = ''.join([x[0][0] + x[1][0] for x in charset['comment']])
allowed += ''.join(charset['string'])
allowed += charset['opening']
allowed += charset['closing']
def brace_check(text):
o = []
c = []
notr = []
found = []
busy = False
last_pos = None
for i in xrange(len(text)):
ch = text[i]
if not busy:
cont = True
for comment in charset['comment']:
if ch == comment[0][0]:
como = text[i:len(comment[0])]
if como == comment[0]:
busy = comment[1]
if ch in charset['opening']:
last_pos = i
cont = False
break
if cont:
if ch in charset['string']:
busy = ch
elif ch in charset['opening']:
o.append((ch, i))
elif ch in charset['closing']:
c.append((ch, i))
else:
if ch == busy[0]:
if len(busy) == 1:
comc = ch
else:
comc = text[i:i + len(busy)]
if comc == busy:
if last_pos is not None:
if busy[-1] in charset['closing']:
found.append((last_pos, i))
last_pos = None
text = text[:i] + '\n' * len(comc) +\
text[i + len(comc):]
busy = not busy
elif busy in charset['string']:
if ch == '\n':
busy = not busy
for t, e in reversed(o):
try:
n = next((b, v) for b, v in c\
if b == charset['closing'][\
charset['opening'].find(t)] and v > e)
c.remove(n)
n = n[1]
if found != []:
if e < found[-1][0] and n > found[-1][0] and n < found[-1][1]\
or e < found[-1][1] and n > found[-1][1] and e > found[-1][0]:
found.append((n, False))
n = False
except StopIteration:
n = False
found.append((e, n))
for t, e in c:
found.append((e, False))
return found
An understandable solution in Python 3:
def check_balanced_string(str):
stack = []
dicc = {'(': ')', '[': ']', '{': '}'}
for char in str:
if char in dicc.keys(): # opening char
stack.append(char)
elif char in dicc.values(): # closing char
if dicc[stack[-1]] == char: # check if closing char corresponds to last opening char
stack.pop()
else:
return False
return not len(stack) # returns True when len == 0
eq = '{1+[3*5+(2+1)]}'
print(check_balanced_string(eq))
Try this:
def matched(s):
stack=[]
open,close="(",")"
for i in s:
if i in open:
stack.append(i)
if i in close:
if len(stack)==0:
return(False)
else:
stack.pop()
if len(stack):
return(False)
else:
return(True)
The below code will display the missing parentheses and the no of times missing in the given string.
from collections import Counter
def find_missing(str):
stack1 = []
stack2 = []
result = []
res_dict = {}
open_set = '<[{('
closed_set = '>]})'
a = list(str)
for i in a:
if i in open_set:
stack1.append(i)
elif i in closed_set:
stack2.append(i)
dict1 = Counter(stack1)
dict2 = Counter(stack2)
print(dict1)
print(dict2)
for i in open_set:
if dict1[i] > dict2[closed_set[open_set.index(i)]]:
res_dict[closed_set[open_set.index(i)]] = dict1[i] - dict2[closed_set[open_set.index(i)]]
result.append(closed_set[open_set.index(i)])
for i in closed_set:
if dict2[i] > dict1[open_set[closed_set.index(i)]]:
res_dict[open_set[closed_set.index(i)]] = dict2[i] - dict1[open_set[closed_set.index(i)]]
result.append(open_set[closed_set.index(i)])
return res_dict
# return result
if __name__ == '__main__':
str1 = '{This ((()bracket {[function]} <<going> crazy}'
x = find_missing(str1)
if len(x) > 0:
print("Imbalanced")
print(x)
else:
print("Balanced")
First we will scan the string from left to right, and every time we see an opening parenthesis we push it to a stack, because we want the last opening parenthesis to be closed first. (Remember the FILO structure of a stack!)
Then, when we see a closing parenthesis we check whether the last opened one is the corresponding closing match, by popping an element from the stack. If it’s a valid match, then we proceed forward, if not return false.
Code:
https://gist.github.com/i143code/51962bfb1bd5925f75007d4dcbcf7f55
I needed something for a recent project and figured I could build on the OP's solution a bit. It allows for comment patterns, quotes and brackets to be checked, whilst ignoring the surrounding text. I've purposefully made it more generic than it needs to be so that others can take what they want and cut out what they don't.
"""
This module is for testing bracket pairings within a given string
Tested with Python 3.5.4
>>> regexp = getRegexFromList(opening + closing)
>>> print(regexp)
(\\<\\-\\-|\\-\\-\\>|\\/\\*|\\/\\/|\\*\\/|\\#|\\"|\\'|\\(|\\[|\\{|\\<|\\\n|\\\n|\\"|\\'|\\)|\\]|\\}|\\>)
>>> test_string = 'l<--([0])-->1/*{<2>}*/3//<--4 &-->\\n5#"6"\\n7"/*(8)*/"9\'"10"\'11({12\ta})13[<14>]'
>>> patterns = re.findall(regexp, test_string)
>>> print(patterns)
['<--', '(', '[', ']', ')', '-->', '/*', '{', '<', '>', '}', '*/', '//', '<--', '-->', '\\n', '#', '"', '"', '\\n', '"', '/*', '(', ')', '*/', '"', '(', '{', '}', ')', '[', '<', '>', ']']
>>> doBracketsMatch(patterns)
True
>>> doBracketsMatch(['"', ')', '"', '[', ']', '\\''])
False
"""
# Dependencies
import re
# Global Variables
# Provide opening and closing patterns, along with their priorities & whether a priority is nestable
opening = ['<--', '/*', '//', '#', '"', '\'', '(', '[', '{', '<']
closing = ['-->', '*/', '\n', '\n', '"', '\'', ')', ']', '}', '>']
priority = [ 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
nestable = {0: True, 1: False}
bracket_pairs = dict(zip(opening + closing, \
[[(closing + opening)[i], (priority + priority)[i]] \
for i in range(0, opening.__len__() * 2)]))
def getRegexFromList(listOfPatterns):
"""
Generate the search term for the regular expression
:param listOfPatterns:
:return:
>>> getRegexFromList(['"', '<--', '##', 'test'])
'(\\\\t\\\\e\\\\s\\\\t|\\\\<\\\\-\\\\-|\\\\#\\\\#|\\\\")'
"""
# Longer patterns first to prevent false negatives
search_terms = sorted(listOfPatterns, key=len, reverse=True)
regex = ""
for term in search_terms:
for char in str(term):
regex = regex + '\\' + char # Search for all characters literally
regex = regex + '|' # Search pattern = (a|b|c)
return '(' + regex[:-1] + ')' # Remove excess '|' and add brackets
def doBracketsMatch(list_of_brackets):
"""
Determine if brackets match up
:param list_of_brackets:
:return:
"""
stack = []
for bracket in list_of_brackets:
# Check empty stack conditions
if stack.__len__() is 0:
# Check for openings first to catch quotes
if bracket in opening:
stack.append(bracket)
elif bracket in closing:
return False
else:
continue
# Check for a matching bracket
elif bracket == bracket_pairs[stack[-1]][0]:
stack.pop()
# Ignore cases:
# - False positives
# - Lower priority brackets
# - Equal priority brackets if nesting is not allowed
elif bracket not in bracket_pairs or \
bracket_pairs[bracket][1] < bracket_pairs[stack[-1]][1] or \
(bracket_pairs[bracket][1] == bracket_pairs[stack[-1]][1] and \
not nestable[bracket_pairs[bracket][1]]):
continue
# New open bracket
elif bracket in opening:
stack.append(bracket)
# Otherwise, unpaired close bracket
else:
return False
# If stack isn't empty, then there is an unpaired open bracket
return not bool(stack)
if __name__ == '__main__':
import doctest
doctest.testmod()