trying to learn code
Thanks for the backup on Javascript. Much of the erroneous code snippets out there are simply trying to force an asynchronous event-based language to be synchronous and more linearly predictable, which is an instant red-flag of a bad example. However once you get used to the "callback" nature of things you will wonder why anyone uses synchronous languages. It's more like delegating tasks to workers and letting them get back to you with the result (things might happen out of order, but you just set up a function to receive the result(s) from source(s) and have it continue processing once the "packages are all delivered"), than micro-managing them through the entire process (terse, rigid, ultimately predictable flow). Of course a multi-lane highway of processing is tougher to "wrap your head around" and control than a single lane one-way road, but which one gets there first? Always, the highway. The "exit" or final result would be at the bottom (top? if you consider it as "bubbling up" instead of "funneling down") of the nest of callbacks, somewhere in the middle of the code, rather than at the very end. If you poison yourself with sequential/synchronous languages now, it will only be tougher to grasp the asynchronous stuff later (and since browsers use it... you'll never avoid it).
what am I doing wrong? Anyone can help me understand this better?
Python, or C, or PERL are languages. They are no substitute for learning the basic underlying logic and theme of this or that particular algorithm.
Without resorting to a programming language tell me how to sort a deck of cards first by suit and then by rank within suite, King high Ace low.
ruveyn
C is a pain in the ass to use without classes and objects, though.

This is really no big deal, if you mean sorting them by value. In non-object oriented languages, you create a struct with the desired attributes. You then add them to an array (or a datastructure like an ArrayList or a LinkedList) and sort them with qsort(). In an object oriented language, swap the struct for a class instead.
In a non-object oriented language, it's time consuming compared to an object-oriented language, though.
aaronzx
Yellow-bellied Woodpecker

Joined: 21 Nov 2013
Age: 33
Gender: Male
Posts: 66
Location: Australia
try Khan academy. They also have Python tutorials now.
https://www.khanacademy.org/science/com ... er-science
I haven't tried these videos yet but for other topics I really find Khan's explanations to the point.
The Pragmatic Programmers have published a book called "Learn To Program", which teaches the basic ideas of programming using Ruby. It doesn't really matter that it's Ruby -- once you understand the basic idea, it applies to a lot of languages, including Perl, Python, Java, PHP, C#, C++, Go, JavaScript, etc. Each language has quirks and their strengths lie in different areas, and there will be other concepts that you'll need to learn, but understanding the building blocks will take you a long way.
You can buy the book anywhere, but there's also an earlier version available on for free on the author's website (google chris pine learn to program).
I really like exercism.io, because it gives you an automated test suite that tells you when your code works, and then people help you improve the code by discussing other ways of doing things, or how to make your code more idiomatic in the language you're working in. They have Python, Ruby, JavaScript, and several other languages available. I learn a lot from the discussions there, and it seems like it caters both to beginners (because people help you figure out how to get better and learn stuff) and experienced programmers (because they get to discuss best practices and trade-offs).
I've heard really good things about teamtreehouse but I've never used it myself.
I would also suggest something like Javascript since you don't need to buy a compiler, but you do need to buy a book--looking up every new topic on line gets be be old in a hurry.
You might be able to learn from other websites or other coders/programmers. If you are interested in how round robins work, you can go to Round Robin Scheduling and press Cntl/U (most browsers) to see the code behind the Cyclic algorithm demonstrator, but if you are interested in White and Black (rather than shades of pink/red) or Home and Away or in "seating" the players so they will be meet their opponents in order, it is much easier to modify existing code than to start from scratch. There are also forums to answer questions such as Javascript Forum, but lurk before posting.
As for sorting a deck of cards:
In an outer loop, vary subscript i from 1 to 51 and
in the inner loop, vary subscript j from i + 1 to 52
if deck[j] < deck[i]
move deck[i] to temp-area
move deck[j] to deck[i]
move temp-area to deck[j]
end-if
end-inner-loop
end-outer-loop
Of course, the syntax would depend on the language and subscripts may start at zero instead of one.
Oops, I just did a print preview and my indentions were blown away. Indent everything from-to-and including word "inner" by two. Indent everything from-to-and including word "if" by two more. Finally, the "move" statements go in two more characters.
You might be able to learn from other websites or other coders/programmers. If you are interested in how round robins work, you can go to Round Robin Scheduling and press Cntl/U (most browsers) to see the code behind the Cyclic algorithm demonstrator, but if you are interested in White and Black (rather than shades of pink/red) or Home and Away or in "seating" the players so they will be meet their opponents in order, it is much easier to modify existing code than to start from scratch. There are also forums to answer questions such as Javascript Forum, but lurk before posting.
As for sorting a deck of cards:
In an outer loop, vary subscript i from 1 to 51 and
in the inner loop, vary subscript j from i + 1 to 52
if deck[j] < deck[i]
move deck[i] to temp-area
move deck[j] to deck[i]
move temp-area to deck[j]
end-if
end-inner-loop
end-outer-loop
Of course, the syntax would depend on the language and subscripts may start at zero instead of one.
Oops, I just did a print preview and my indentions were blown away. Indent everything from-to-and including word "inner" by two. Indent everything from-to-and including word "if" by two more. Finally, the "move" statements go in two more characters.
You algorithm has a running time of O(n^2). By comparison, this has a running time of O (n log n):
public static void quicksort(Comparable[] a) {
quicksort(a, 0, a.length - 1);
}
private static void quicksort(Comparable[] a, int low, int high) {
if (low + CUTOFF > high)
insertionSort(a, low, high);
else {
int middle = (low + high) / 2;
if (a[middle].compareTo(a[low]) < 0)
swapReferences(a, low, middle);
if (a[high].compareTo(a[low]) < 0)
swapReferences(a, low, high);
if (a[high].compareTo(a[middle]) < 0)
swapReferences(a, middle, high);
swapReferences(a, middle, high - 1);
Comparable pivot = a[high - 1];
int i, j;
for (i = low, j = high - 1;;) {
while (a[++i].compareTo(pivot) < 0);
while (pivot.compareTo(a[--j]) < 0);
if (i >= j)
break;
swapReferences(a, i, j);
}
swapReferences(a, i, high - 1);
quicksort(a, low, i - 1); // Sort small elements
quicksort(a, i + 1, high); // Sort large elements
}
}
public static final void swapReferences(Object[] a, int i, int j) {
Object tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
(Just don't place the pivot at the edges, in which case this is an O(n^2) operation as well.

Most of the languages you will come across these days are free to use, even the ones that require a compiler. C, C++, JavaScript, Java, PHP, Ruby, Python, Go, Haskell, Scala, Clojure, Elixir, Erlang, Rust, are all free. Even the C# compiler is free, though the programming environment (Visual Studio) is not.
Last edited by kx on 02 Dec 2013, 7:57 am, edited 1 time in total.
You might be able to learn from other websites or other coders/programmers. ..snip...
As for sorting a deck of cards:
In an outer loop, vary subscript i from 1 to 51 and
in the inner loop, vary subscript j from i + 1 to 52
..snip for brevity...
You algorithm has a running time of O(n^2). By comparison, this has a running time of O (n log n):
[b]
public static void quicksort(Comparable[] a) {
quicksort(a, 0, a.length - 1);
.. snip whatever ...
Yea, Yea. My career was as a programmer and I know a number of ways to sort. My example was aimed at beginners or the OP, "straight from Intro to Computers 101". Pay attention to your audience and who is in it.
Most of the languages you will come across these days are free to use, even the ones that require a compiler. C, C++, JavaScript, Java, PHP, Ruby, Python, Go, Haskell, Scala, Clojure, Elixir, Erlang, Rust, are all free. Even the C# compiler is free, though the programming environment (Visual Studio) is not.
If you're a student, Microsoft gives it away for free. I got both Visual Studio 2013, Windows 7 and Windows Server 2008 for free via DreamSpark.
Similar Topics | |
---|---|
How to learn writing? |
Today, 4:32 am |
How to learn keep secrets and talk only when necessary? |
05 Jul 2025, 1:48 am |
Learn Skills by playing - Tapspire |
04 Jul 2025, 8:28 pm |