Programming Questions

• Popular Tags
• Off Line Minimum Python
It says that when you give the following input: "1","2","E","E","3" you should get the following output: "1,2" Shouldn't you just get "1" as the output, since that is the smallest integer in the set up to the E's?
franko posted this question on 10/28/16 |
• +
• 1
• -
• I took a look to your code. Let me tell you that you can reduce your piece of code: resStr = "" i = 0 while i < len(res): resStr += res[i] if i < len(res) - 1: resStr += ',' i += 1 return resStr to this: return ','.join(res) ;-)
• +
• 1
• -
• Hi franko, Actually the correct output is "1,2". Explanation: E's "take out" (sic) numbers, so before the first E, the set is {1;2} but just after the E, the set becomes {2} since the smallest number is taken out. Therefore the second E outputs "2", which is the smallest remaining number. Is it clear to you now?
• +
• 0
• -
• The full question is that: I had to solve this problem but I don't know any practical application for it. Any tip on why this problem exists. thank you. Have the function OffLineMinimum(strArr) take the strArr parameter being passed which will be an array of integers ranging from 1...n and the letter "E" and return the correct subset based on the following rules. The input will be in the following format: ["I","I","E","I",...,"E",...,"I"] where the I's stand for integers and the E means take out the smallest integer currently in the whole set. When finished, your program should return that new set with integers separated by commas. For example: if strArr is ["5","4","6","E","1","7","E","E","3","2"] then your program should return 4,1,5. python code: #! /usr/bin/python # -*- coding: utf-8 -*- import pdb import copy def OffLineMinimum(): inputStr = (raw_input("inputStr: ")).split(',') inputStr = [itr for istr in inputStr for itr in list(istr.strip()) if (itr != '"' and itr != "'") or (itr == 'E')] ans_tmp = copy.copy(inputStr) ans = [] count = 0 for i in range(0,len(inputStr)): #pdb.set_trace() if i > len(inputStr) - 1: break if(inputStr[i] == 'E'): if len(ans_tmp) == len(inputStr): addr = i else: for j in ans_tmp: if j is 'E': addr = ans_tmp.index(j) break ans_tmp.remove(ans_tmp[addr]) ans_tmp1 = [ans_tmp[an] for an in range(0,addr) ] if len(ans_tmp1): ans_tmp1.sort() ans.append(ans_tmp1[0]) ans_tmp.remove(ans_tmp1[0]) return ','.join(ans) #'1', '2', 'E', 'E', '3' #"5","4","6","E","1","7","E","E","3","2","9" print OffLineMinimum()
• +
• 0
• -
• Yes, thank you