Skip to content

Instantly share code, notes, and snippets.

@IEdiong
Last active May 2, 2021 22:36
Show Gist options
  • Save IEdiong/0c75d03221a1c712ff42e70d8700eeae to your computer and use it in GitHub Desktop.
Save IEdiong/0c75d03221a1c712ff42e70d8700eeae to your computer and use it in GitHub Desktop.
Algorithm Fridays Week 4
def prodOfNums(nums):
if len(nums) == 0:
return "invalid array: input an array of numbers"
else:
prod = 1
ans = []
for i in nums:
prod *= i
for i in range(0,len(nums)):
ans.append(prod // nums[i])
return ans
@meekg33k
Copy link

meekg33k commented May 2, 2021

Hello @IEdiong, thank you for participating in Week 4 of Algorithm Fridays.

Your solution works for most test cases, however it fails for the test case for when one of the elements of the array is 0.

prodOfNums([4, 3, 0]); ❌ // should return [0, 0, 12] but yours returns [0, 0, 0]

Also, do you think we can better optimize this in terms of memory usage? Do we have to create a new ans array?

Kudos all the same for a good attempt! Let me know what you think.

@IEdiong
Copy link
Author

IEdiong commented May 2, 2021

Hello @meekg33k, thank you for the feedback.

I did not consider this particular test case, but I must confess that my mind is being expanded as I participate in this challenge.

I did do some brainstorming for a better way that doesn't involve me creating a new ans array, but I couldn't come up with something good. Please I'm open to corrections and suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment