-4 prefix python-3.x
嗨,大家好,
我试图在hackerrank的python中解决上述问题,但我收到错误为:从空列表中弹出 input:expression:+15 和 variables:{} 。请在下面检查我的代码:
def is_operand(c):
return c.isdigit()
def max_result_expression(expression:str,variables: Dict[str, Tuple[int, int]]) -> Optional[int]:):
stack = []
for c in expression[::-1]:
if is_operand(c):
stack.append(int(c))
else:
o1 = stack.pop()
o2 = stack.pop()
if c == '+':
stack.append(o1+o2)
elif c == '-':
stack.append(o1-o2)
elif c == '*':
stack.append(o1*o2)
elif c == '/':
stack.append(o1/o2)
return stack.pop()
Run Code Online (Sandbox Code Playgroud)
这是也处理变量元组的代码。
def max_result_expression(expression,variables):
print ()
print (expression,variables)
#request is to find the expression that has the max value
#to help us, let's put the expression into a list
#later we will iterate through this list "expression_list"
#by passing the expression to our earlier code
#the result of each expression will be stored into another list
#let's call that result list as "expression_results" list
expression_list = [expression]
expression_results = []
#Now that we have assigned the two lists, let's review the variables being passsed
#first check if the variables string is a dictionary or a string
#isinstance() allows us to check for it
#isinstance(variable_name, datatype). datatype can be int, str, float, list, tuple, dict
#here we are checking if the variable is a dictionary (dict)
#isinstance() will return True or False. In this case, we will get True if variable is a dictionary
if isinstance(variables,dict):
#since the variable is a dictionary, we need to iterate through it like a dictionary
#use key,value to get the key and value portion
for k,v in variables.items():
#the value portion of the dictionary can be either an integer or a tuple
#check if the value is a tuple. If tuple, then we have to do a range
if isinstance(v,tuple):
#since the value is a tuple, we need to do a range of (lower_limit, upper_limit)
#one expression will now become multiple expressions
#example: x+y with x:(2,5) will become 2+y,3+y,4+y (3 expressions)
#store each expression into a list. To do that, create a temp_exp list
temp_exp = []
#iterate through the original expression_list (which started with one expression)
for exp_temp in expression_list:
#for each expression in the expression_list,
#let's replace the value of variable with range value
for x in range(int(v[0]),int(v[1])):
#we can do that using string replace
temp_exp.append(exp_temp.replace(k,str(x)))
#now that we have replaced all the expressions
#with the range value, let's reassign the temp_exp back to expression_list
#Note: If there are multiple variables with ranges, this happens again
expression_list = temp_exp[:]
#finally, we have a list of expressions that need to be evaluated
#let's iterate through the expression one at a time
#As I mentioned earlier, store the result of the expression
#in the expression_results list
#finally, get the max value from this list and return it back
for expression in expression_list:
stack = []
e_list = [x for x in expression.split(' ')[::-1] if x != '']
#if e_list has only one numeric value, then return numeric value
#if e_list has only one value but not numeric, then return null
if len(e_list) == 1 and e_list[0].isdigit():
expression_results.append(int(e_list[0]))
continue
elif len(e_list) == 1 and not e_list[0].isdigit():
expression_results.append('null')
continue
#if there are less than 3 arguments in an expression
#force the result as null
elif len(e_list) < 3:
expression_results.append('null')
continue
for c in e_list:
if c.isdigit():
stack.append(int(c))
elif c.isalpha():
stack.append(int(variables[c]))
else:
if len(stack) > 1:
o1 = stack.pop()
o2 = stack.pop()
if c == '+':
stack.append(o1+o2)
elif c == '-':
stack.append(o1-o2)
elif c == '*':
stack.append(o1*o2)
elif c == '/':
stack.append(o1/o2)
expression_results.append(stack.pop() if len(stack) == 1 else 'null')
return max(expression_results)
print (max_result_expression('+ 6 * - 4 + 2 3 8',''))
print (max_result_expression('+ 10 20 50 ',''))
print (max_result_expression('* + 1 2 3',''))
print (max_result_expression('* + 2 x y',{'x':1,'y':3}))
print (max_result_expression('* + 2 x y',{'x':(3,7),'y':(1,9)}))
print (max_result_expression('* + 2 x y',{'x':(0,2),'y':(2,4)}))
print (max_result_expression('+ 6 * - x + 2 3 8',{'x':(4,8),'y':(2,4)}))
Run Code Online (Sandbox Code Playgroud)
输出如下:
+ 6 * - 4 + 2 3 8
-2
+ 10 20 50
null
* + 1 2 3
9
* + 2 x y {'x': 1, 'y': 3}
9
* + 2 x y {'x': (3, 7), 'y': (1, 9)}
64
* + 2 x y {'x': (0, 2), 'y': (2, 4)}
9
+ 6 * - x + 2 3 8 {'x': (4, 8), 'y': (2, 4)}
22
Run Code Online (Sandbox Code Playgroud)
让我们一一解决这个问题。
对于初学者,您不需要该is_operand(c)功能。您可以将该检查带入表达式函数本身。
其次,修复表达式中的空间问题。为此,我们可以按空间分割表达式。然后我们将让每个项目进行处理。
现在,我们需要确保至少有 2 个项目可供您弹出。因此,在弹出之前检查堆栈的长度。它必须大于 1。
修改后的代码如下:
def max_result_expression(expression,variables):
stack = []
e_list = [x for x in expression.split(' ')[::-1] if x != '']
#we are splitting the expression by space and also reversing it
#also we can eliminate any unwanted spaces in the expression
#by removing '' from the e_list
for c in e_list: #let's use the e_list instead of the expression
#if number, convert to int and store into stack
if c.isdigit():
stack.append(int(c))
#if variable, lookup value for the variable in the passed dictionary
#convert the lookup value to int and store into stack
elif c.isalpha():
stack.append(int(variables[c]))
#if operator, then process the expression
else:
if len(stack) > 1:
o1 = stack.pop()
o2 = stack.pop()
if c == '+':
stack.append(o1+o2)
elif c == '-':
stack.append(o1-o2)
elif c == '*':
stack.append(o1*o2)
elif c == '/':
stack.append(o1/o2)
return stack.pop() if len(stack) == 1 else 'null'
#return the value only if stack has one item. If there are more
#then the expression was incorrect
print (max_result_expression('+ 6 * - 4 + 2 3 8',''))
Run Code Online (Sandbox Code Playgroud)
这样做的结果是:
-2
Run Code Online (Sandbox Code Playgroud)
这样做的结果:
print (max_result_expression('+ 10 20',''))
Run Code Online (Sandbox Code Playgroud)
是
30
Run Code Online (Sandbox Code Playgroud)
这样做的结果:
print (max_result_expression('+ 10 20 50',''))
Run Code Online (Sandbox Code Playgroud)
是
null
Run Code Online (Sandbox Code Playgroud)
这样做的结果:
print (max_result_expression('* + 2 x y',{'x':1,'y':3}))
Run Code Online (Sandbox Code Playgroud)
是
9
Run Code Online (Sandbox Code Playgroud)
你有它。我们现在已经解决了您在代码中遇到的所有问题。
综上所述,我们现在解决了以下问题:
null如果表达式不完整则返回| 归档时间: |
|
| 查看次数: |
941 次 |
| 最近记录: |