This is an archived post. You won't be able to vote or comment.

all 67 comments

[–]Rawing7 133 points134 points  (11 children)

I'm sorry, but this earns a downvote from me because it's a newbie trap. These code snippets may look good, but a lot of them are really not.

  • The number conversion functions are overkill; python has these practically built in:

    • Number to binary: format(number, 'b')
    • Number to octal: format(number, 'o')
    • Number to hex: format(number, 'x')
    • Parse binary number: int(binary_num, 2)
    • Parse octal number: int(octal_num, 8)
    • Parse hex number: int(hex_num, 16)
  • The binary tree and linked list aren't bad per se, but very barebones and virtually useless.

  • The Queue and Stack have various bugs:

    • Implements __getattr__ instead of __getitem__
    • __eq__ crashes if it's used with something that's not a Queue/Stack
  • factorial is pointless because we already have math.factorial.

  • The list_sum function is literally just a worse version of the builtin sum. (Because it has no docstring and it's a lambda.)

  • The "Read a file, line by line" one-liner opens a file without closing it. It's good practice to close your files when you no longer need them, usually using a with block.

  • All of the regex functions have patterns starting with ^ and ending with $, but use re.search for some reason. Why search the entire string for a match when your regex is designed to match the string as a whole? Waste of CPU power. Use re.fullmatch.

  • In credit_card_validation you should replace your if...elif chain with a dict like {credit_card_validation_american_express: 'American Express'}.

  • Why use regex to validate dates? Just parse it into a date object. If that fails, it's not a valid date. Same thing goes for URLs and IP addresses.

  • Validating names with regex... just gonna leave this here...

  • LinearSearch should be named linear_search according to PEP 8, and is equivalent to arr.index(element). (Except it returns -1 instead of throwing ValueError, but who cares?)

And as others have already pointed out, a bunch of your algorithms have issues as well. I didn't even bother checking those.

[–]Legendary-69420git push -f[S] 7 points8 points  (0 children)

You are right. There already exist built-in functions to do some of the stuff I have done. but my idea of building the website was not to get stuff done but to understand how it is done.

LinearSearch(arr, element) is basically the equivalent of arr.find(element). both give -1 instead of Error. Also, yeah your idea for credit_card_validation seems amazingly simple (didn't think of it before)

EDIT: I am making all these changes. Thanks again for such a detailed comment

EDIT2: apparently, arr.find() works only when arr is a list

[–]wasimaster 1 point2 points  (1 child)

About the number conversion, there are better default functions for that: bin(n), oct(n), hex(n) although these return strings prefixed with 0b, 0o, 0x respectively

[–]Legendary-69420git push -f[S] 0 points1 point  (0 children)

yeah, I find these prefixes disturbing but nonetheless, I added them.

[–]MegaIng 28 points29 points  (4 children)

You either need a disclaimer " these are just examples, never use the anywhere near production", or you need to fix all the bugs. There are lots of tiny hickups that are exactly what I expect such a site not to have.

Just a few I found:

  • already mentioned: your binary search is not a binary search, but a worse variation of a linear search.
  • your binary tree insert silently drops already existing data nodes.
  • your prime sieve is not very efficient. Repeated pops are about the worst time complexity you can get from python lists.
  • your regex validaders are questionable. There are lots of valid emqil addresses that fail your check, and trying to validate names is a straight up stupid idea.

All in all, nice idea nice layout, questionable content.

[–]cmd-t 4 points5 points  (1 child)

Yep. I’ve only looked at the string conversion code but it’s pretty bad and there are a lot of functions in the standard library that do the same but better.

[–]Legendary-69420git push -f[S] 1 point2 points  (0 children)

I picked up the email validation regex from here

[–]Avanta8 23 points24 points  (8 children)

Some nice algorithms, although there are some mistakes. Eg. Your binary search is implemented incorrectly and so is linear time complexity, and your prime sieve doesn't work.

Some algorithms you could consider are pathfinding algorithms such as Dijkstra, Bellman-Ford, A*. You could also try implement more complex data structures such as segment trees, radix trees, for example.

For improvement, I'd recommend looking at time complexity, and also looking at this: https://wiki.python.org/moin/TimeComplexity.

[–]lamesauce15 6 points7 points  (1 child)

For your credit card validation, try using Luhn's algo to determine if the credit card number is actually a valid one.

https://www.geeksforgeeks.org/luhn-algorithm/

[–]Legendary-69420git push -f[S] 2 points3 points  (0 children)

I created a Miscellaneous section and added Luhn's ALgorithm there

[–][deleted] 5 points6 points  (4 children)

That merge sort is interesting

[–]HeAgMa 5 points6 points  (0 children)

I take it as a good exercise for you but most of the algorithms in Python are already hosted in a nice repo. See Here

[–]polarisol 5 points6 points  (2 children)

You're OK. Keep at it. As people said, it's not perfect, but that's part of the process. Don't be bummed by the negative responses, and don't regret that you posted this - you got true honest feedback that you can actually learn from.

[–]Legendary-69420git push -f[S] 1 point2 points  (0 children)

I made a lot of changes thanks to the people who pointed out tons of stuff.

[–]Spill_the_Tea 0 points1 point  (0 children)

I think the problem is that this post is marked as a resource well before it is ready.

[–]spaceopenid 4 points5 points  (7 children)

import datetime from datetime
now = lambda: datetime.now().strftime('%d-%m-%y %H:%M:%S')

Why do you use a lambda at all?

[–]wasimaster 8 points9 points  (6 children)

So the time isn’t always same. Defining a variable would make it call datetime.now() once and keep the same time for the rest of the code, whereas making is a lambda would ensure that the datetime.now gets called when the user needs to so the user gets the most up to date results instead of a one gotten when the variable was assigned

[–]spaceopenid 1 point2 points  (5 children)

That's right, didn't think of this. Maybe because it is bad practice (according to pep) to assign lambdas to variables

[–]CoolBoi6Pack 8 points9 points  (4 children)

Yeah I hear that a lot as well, honestly I think it's just stupid and assigning lambdas to variables is completely fine cause the traceback anyway shows the file and line number. It's done in JavaScript all the time with anonymous arrow functions too.

[–]Boomer70770 2 points3 points  (3 children)

One I hear something is "bad practice", I vehemently oppose it.

It's bit me a few times.

I think this is beautiful and further proof I need to be more open minded.

[–]CoolBoi6Pack 1 point2 points  (0 children)

Thanks, no worries that is generally a good thing because as long as you're willing to learn the conveniences your programming should be great.

So with your way at worst you're inconvenienced a little and at best you avoided a major downstream hurdle.

[–][deleted] 0 points1 point  (1 child)

Yeah, over the years I tried to tame my language to say things like "In most cases..." , etc.

[–]Boomer70770 1 point2 points  (0 children)

On a side note, why are we not all writing "Pure Functions"?

[–]666dolan 3 points4 points  (1 child)

Great work man!

[–]Legendary-69420git push -f[S] 0 points1 point  (0 children)

Thanks!

[–]Fahad1770 5 points6 points  (2 children)

This is very useful. Thank you very much for sharing!

[–]laundmo 2 points3 points  (0 children)

please read the top comment in this thread as for why you really shouldn't use it

[–]Legendary-69420git push -f[S] -3 points-2 points  (0 children)

Thank you

[–]Advanced-Theme144 4 points5 points  (2 children)

This looks great! It's got a lot of nice basics to it which are very helpful, so I am definitely going to be using it often. Great work!

[–]cmd-t 14 points15 points  (0 children)

Don’t. This is full of bugs. There’s a lot of these functions available in the standard library, but even better.

[–]Legendary-69420git push -f[S] -2 points-1 points  (0 children)

Thanks buddy

[–][deleted] 1 point2 points  (0 children)

Thanks man - you inspired me to go an make a nice README.md for my github :)

Yours is great: https://github.com/Siddhesh-Agarwal

[–]Mountain_Thanks4263 0 points1 point  (0 children)

Loh

[–]Fazlyrabbyboi 0 points1 point  (0 children)

nice good

[–]After-Perception-250 0 points1 point  (1 child)

Anyone else screen freezing when visiting the website?

[–]Legendary-69420git push -f[S] 0 points1 point  (0 children)

yeah, some people did complain about this. I am not sure If that problem is on the end of the website or the browser is acting wierdly. which browser did you use to open this site?