Skip to content

Instantly share code, notes, and snippets.

@VeldaKiara
Created April 10, 2021 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VeldaKiara/c5d98ebca8a71e7889f1aa6c9ad916a9 to your computer and use it in GitHub Desktop.
Save VeldaKiara/c5d98ebca8a71e7889f1aa6c9ad916a9 to your computer and use it in GitHub Desktop.
This is a list of the algorithms I will be solving.
nums = [0, 0, 1, 1, 2, 2, 3, 3, 4]
#using list comprehension to remove duplicates
non_dup = []
[non_dup.append(x) for x in nums if x not in non_dup]
print(str(non_dup))
print(len(non_dup))
@meekg33k
Copy link

Hello @veldakarimi, thanks for participating in Week 1 of Algorithm Fridays. I like that you used Python's list comprehension for your solution.

A few things to note:

  • The challenge required that you write a function. So the ideal thing to do would have been to put this logic inside a function, except you had a reason why and I'll be interested in knowing.
  • The other thing to note is that your solution assumes that the input array cannot have a null value. If I pass None as input to your code, it breaks. Ideally, you want to write code that is robust and doesn't fail on edge cases.

I have posted my solution here, if you would like to take a look. Let me know what you think.

@VeldaKiara
Copy link
Author

Hello @meekg33k, I am happy to participate.
I totally missed that I was supposed to exclusively use a function. I used a list comprehension because of time, I thought it would be faster and compact as compared to using a regular loop.
And yes my solution assumed that there will be no case where the result is null.
Something I have learned from doing this challenge is to ask the right questions and not to base the solution on my assumptions and instructions or requirements are as important as solving the problem.
And as much as I want to make the solution compact I need to account for edge cases.

I will write another solution that accounts for the edge cases and uses a function.

Thank you for the feedback as well as the challenge.
I cannot wait for the next challenge.

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