In python 2.7, no division, O(n) time. The tradeoff is using a bunch of memory, if the list is very long.
values = map(int,raw_input().split(' ')) def getLeftProducts(rawValues): leftProducts = [] if len(rawValues) > 0: total = 1 for val in rawValues: total *= val leftProducts.append(total) return leftProducts def getRightProducts(rawValues): rightProducts = [] if len(rawValues) > 0: total = 1 index = len(rawValues) - 1 while index >= 0: total *= rawValues[index] rightProducts.insert(0,total) index -= 1 return rightProducts def processList(rawValues): if len(rawValues) < 2: return rawValues resultList = [] leftList = getLeftProducts(rawValues) rightList = getRightProducts(rawValues) resultList.append(rightList[1]) index = 1 while index < (len(rawValues) - 1): resultList.append( leftList[index - 1] * rightList[index + 1] ) index += 1 resultList.append(leftList[index - 1]) return resultList print ' '.join(map(str,processList(values)))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In python 2.7, no division, O(n) time. The tradeoff is using a bunch of memory, if the list is very long.