sergio
BAN USER- 1of 1 vote
AnswersWrite a function that receives a string an returns a list of all the substrings (of length >= 2) composed by consecutive characters. E.g input : "BCCDE" , output: ["BC","CD","CDE","DE"]
- sergio in United Kingdom for Bing| Report Duplicate | Flag | PURGE
Microsoft Software Developer Algorithm
import sys
def smallestLargerNoArrays(stopToken):
print('Enter one number per line\n')
maximun, secondMaximun, minimun, secondMinimun = None,None,None,None
while (True):
userInput = input()
if (userInput == stopToken):
print ('Maximun:{0} Second Maximun:{1} Minimun:{2} Second Minimun:{3}').format(maximun,
secondMaximun if secondMaximun != None else maximun,
minimun,
secondMinimun if secondMinimun != None else minimun)
return
secondMaximun = max(secondMaximun,min(userInput,maximun))
maximun = max(userInput,maximun)
if (secondMinimun == None and minimun != None): secondMinimun = max(userInput,minimun)
elif(secondMinimun != None): secondMinimun = min(secondMinimun, max(userInput,minimun))
minimun = userInput if minimun == None else min(userInput,minimun)
what about given {3,6} and N=9. According to your solution the final list will contain {1,2,3,4,6,8} while the optimal solution is {1,2,3,6}
The representation of a number as the sum of powers of two is optimal but is not the only one. If the initial list is empty or has only powers of two your approach will work otherwise is not guarantied.
The substrings should be composed of strictly increasing characters, so BCC is not valid
- sergio March 05, 2017