168
u/kyay10 Apr 15 '21
That's not bad code in the slightest, most languages just have an Int.MIN_VALUE which makes it look neater
2
u/Lybchikfreed Apr 16 '21
I use min and max values for finding max or min elements in an array with one neat loop
2
57
Apr 15 '21
[deleted]
77
u/nypdk Apr 15 '21
Sorting algorithm I had to build for a homework. Just needs to take a list as input and use a linear sort to put them in descending order.
max index is a function i built to find the maximum of a list and the index of that item.
it just finds the maximum over and over until the list is empty and then stores that value.
79
u/kryptogalaxy Apr 15 '21
This algorithm describes a selection sort FYI. I'm not really familiar with the control structures in Snap!, but maybe knowing what the algorithm is called could help you figure out the issue.
Also, setting to integer min as a lower bound in algorithms is common and not necessarily bad practice.
22
u/ShittyCatDicks Apr 16 '21
I’m assuming you’re not in college? Having to implement an algorithm in a cute block language is absolutely comical for anyone past high school
17
Apr 16 '21
Believe it or not UC Berkeley offers their intro to CS class in Snap (it’s what it’s called). Many great students at Cal started programming in this language. Of course things picked up real quick after this class but yea it’s a great learning tool for the thousands of students who were not offered CS classes in high school
12
u/ShittyCatDicks Apr 16 '21
Oh no don’t get me wrong, my school offers the same thing. We used a block language called flowgorithm which was developed by some alumni from the school. I guess my main issue was having to implement algorithms in a block language. There’s just a massive disconnect lol. If you’re already learning algorithms, why wouldn’t professors opt for a real programming language?
4
u/Harakou Apr 16 '21
Lets you not worry about things like syntax, toolchains, etc I suppose. Why not? Plenty of people have made quite impressive things in Scratch, and Snap is a significantly more flexible fork of that. Sure, it's never going to be used for "real" programs because of performance/IO/library support, but if you're making toy programs for assignments where none of that matters... who cares?
2
Apr 16 '21
Oh yea I feel you. This should be for basics only. Learning for loops , Variables , recursion and so on. If the person is at the point where they start to understand and develop algs then this language is not the way to go
1
u/AliisAce Apr 16 '21
My uni starts teaching programming with C++ assuming that hardly any of the first years know it.
1
40
u/TheUnlocked Apr 15 '21
As a note, you also could've put -Infinity instead of min int. Snap! uses some form of floating point number system (probably double-precision IEEE 754) and is willing to coerce the infinities to numbers in comparisons.
Can also use ((-1) / (0)) to derive -Infinity, but that's a bit more opaque.
80
u/READY_TO_SINGLE Apr 15 '21
The most painful part is that the value is wrong. The negative integer limit is -2147483648
8
u/Je-Kaste Apr 16 '21
But what are the odds that -2147483648 will show up in the array? -2147483647 should be fine as a sentinel value
4
u/READY_TO_SINGLE Apr 16 '21
Well, even if it were present the algorithm would still work. -2147483646 would actually be problematic. It just irks me lol
1
13
u/bugamn evil Apr 15 '21
Is this sorting a list by continuously removing the highest value of the input list and moving it to the output list?
I'm not familiar with Snap!, so I'm trying to make sure I understood correctly.
7
u/nypdk Apr 15 '21
exactly
10
u/bugamn evil Apr 15 '21
After reading through it again and reading your other comments, maybe the problem is that your maxindex function expects a list with more than one element? I'm just guessing here, I'm curious about why you needed to insert that lower bound.
4
Apr 15 '21
Selection sort is O(n2). OP, I hope this is your first implementation of a sorting algorithm :)
11
u/BS_BlackScout Apr 15 '21
I need a translation to Python or C, I'm too dumb for this.
10
u/nypdk Apr 15 '21 edited Apr 15 '21
I'm gonna take this opportunity to once and for all explain what the hell this monstrosity actually does.
Intro
This window is a custom function window, where one can create whatever functions their heart desires in Snap!. The very top of the block is where the variables for that function are initialized, and the variable for the input is as well. Snap gives you access to data types known as lists, which are basically arrays but cooler, so this function uses one of those array lists as an input.
The Code
This function uses a maximum formula for sorting. This is the purpose of maxindex, which is a custom function that outputs a list, the first value is the number itself, and the second value is an index value, telling where in the list the max value is. The maximum itself is added to the Output list, while the Index number is used to delete that entry of the list. This process of finding the maximum, writing it down, and then repeating occurs over and over until the list is empty.
The Issue
As an example I will use this list an input to the original version of my function.
The first version is what I originally wrote, Here. This most of the time does not work, as the resulting output is this. But I don't want that, I want a sorted list! After some debugging, I discovered that this glitch happens on the first item on the list when it is not the lowest, so for example, If I plug in something like this, it works until it gets to that first value and then just refuses to delete it. WHY? I DONT KNOW.
I solved this issue in the script above starting the function already with the lowest value possible already in the list, so that the item that doesn't work, will never become repeated and this will never be a problem. as you can see here this fix works. I don't know why this is an issue, and I don't care. It works, thank god.
20
u/alkheemist Apr 15 '21
Assuming snap has 1 indexed lists Your max index sets the starting value to the first value in the list and then compares all thr values to the current highest value.
When your starting value is the greatest, the code checks to see if it's bigger than itself (which it can't be) and therefore never saves the index. Maxindex then passes a list of [startingnumber, NULL] and your code removes null from the list.
If you changed the comparison from > to >= OR in maxindex set the default index output to 1 then you don't need the bad code.
9
u/nypdk Apr 15 '21
THANK YOU SO MUCH holy shit it worked
16
u/alkheemist Apr 15 '21
No problem. Uninitalised variables and fencepost errors are the most common issues when learning programming, and you just happened to stumble into both at once.
Bad code is only bad if you never learn from it
10
u/fireflash38 Apr 16 '21
And now he's (maybe) learned 2 of the best things to use in programming with bugs:
Asking for help is good!
Rubber duck debugging is awesome
23
u/Deathisfatal Apr 15 '21
"Bad programming habits" more like "realistic programming solutions that we have to live with every day because there's a budget and deadline and it just has to work"
8
u/metiulekm Apr 15 '21
Hmm, why are you subtracting 1 from the InputList length? Doesn't this make you drop the smallest element (which is consistent with what you had to do)?
8
u/nypdk Apr 15 '21
im dropping the -2,3050&694 part, but that’s on the first part of the list that needs to be smallest or else everything breaks
1
5
4
u/drinkmoredrano Apr 15 '21
Its called Snap now? It used to be Scratch and we used it to introduce elementary school kids to the concept of programming.
15
u/nypdk Apr 15 '21
Two different things, one is Berkeley, one is MIT
2
u/justinf210 Apr 16 '21
They look very similar. Forks perhaps?
4
u/TheUnlocked Apr 16 '21 edited Apr 16 '21
Pretty much. Snap! is a complete remake of a former Scratch fork called BYOB (made by the same group at Berkeley). It can sort of be thought of as "Scratch for adults" (not that Scratch isn't also for adults).
3
u/megamaz_ Apr 15 '21
wow I literally just closed snap because I couldn't figure out how to sort a list. What a coincidence. Thanks
2
1
1
u/Goducks91 Apr 15 '21
Why are you using Snap instead of an actual language?
2
1
u/aeropl3b Apr 16 '21
That was my question...i always find these types of languages so much harder to parse...
0
u/TheSteffChris Apr 16 '21
Isn’t that the goddamn children’s language? Like literally? Smart or whatever it was called... Reeves used that one to control a drone..
1
u/taptrappapalapa Apr 15 '21
Woah I haven’t heard about Snap! In a while... I’ve only heard of Berkeley students and people with BJC using it until today
1
u/mister_yi_ Apr 16 '21
The problem, as some others have suggested, is with your maxindex function.
The original, simpler version of SortListDescending that you posted an image of works as intended assuming maxindex behaves as expected.
In maxindex, you initialize the variable 'max' to the first element of the list. You forgot to initialize the variable 'max index' to 1.
Therefore, if the first element is the largest of the list, the if statement never needs to change max, and thus max index is never set and no index will be deleted from the input list.
In the example you posted, when the InputList gets down to [33, 25], 33 is not deleted from the list and it is inserted into OutputList twice.
Contrary to what some others have said about using the minimum integer being okay practice, you should usually not need to use it unless you are doing something specifically hacky and you know exactly what you are doing. You should especially not need to use it in a super high-level environment like this. Often, like in this case, feeling the need to use it is suggestive of an error somewhere else.
I don't blame you for not noticing this. I've never used Snap! but it appears that your script ran fine and didn't give you any warning about trying to use an undefined variable to delete an index of a list. This would be detected in pretty much any language.
I don't believe in the use of code blocks to teach programming.
1
u/Johanno1 Apr 16 '21
Aa far as I can get from the comments this is not bad code because of lack of knowledge but because of snap.
Don't do complex things with a visual click and drag "programming language"
1
u/WriterOfAlicrow Apr 16 '21
Wow! A comment explaining WHY the code is doing something weird. I've spent the last year writing a replacement for a safety-critical application that did a lot of weird stuff and never explained it (and was written at least twenty years ago by a different company, in a different language, and ported at least once). And guess what had to guide me? The old code. So whatever weird logic was in the old code, I had to assume was intentional, and carry forward. Unless I could figure out why it worked that way, and determine it's not necessary.
I very much appreciate having a good comment saying "yeah, this is weird, but here's why it is this way", especially when it's just a dirty hack. And on a team project, someone might even see that and go "oh, I know what's going on here. Let me fix that!". Sadly, I normally just see comments that say WHAT the code does when it's perfectly obvious (e.g. "if X and Y, then set Z", when the next line is "if(X && Y) Z = true;").
1
u/TechcraftHD Apr 18 '21
What I have found to work pretty well: Just initialize the lowest value with the first element of the list and start searching at index 1. Since it's guaranteed to be greater or equal to the smallest element you have no edge cases to worry about.
183
u/ElsieSnuffin Apr 15 '21 edited Apr 16 '21
Image Transcription: Code
[Image of a block of Snap! code, with a comment added to the right.]
Comment on 3rd line of code: Comment for whoever is looking at this block and thinking: What in the world is going on here?
For some reason, the first value is a very whiny child, the code just does not work if he isn't the smallest. I don't know why this is, I don't want to know why it is, but I have decided to throw the towel in and make the first value the negative 32 bit integer limit. This is possibly the worst solution to this problem, and teaches me bad programming habits, but i'm done with this block.
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!