Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In ruby or programming in general is the best practice to be as concise as the language allows or to be concise while still readable? For example:
animals = %w{ cat dog bird }
chosen_animal = rand(animals.length)
random_animal = animals[chosen_animal]
Or
animals = %w{ cat dog bird }
random_animal = animals[rand(animals.length)]
I have a feeling the second one is better code. Is there any benefit of using the first one? Which would you use and why?
as concise as the language allows or to be concise while still readable?
Always, always prefer readability. Conciseness is only useful when it makes your code more readable (i.e., less verbosity and clutter to wade through). It saves you nothing if it obscures the code and serves only to slow development down.
Readability/maintainability only takes a back seat to performance concerns and only when absolutely necessary. This is not often the case in a language like Ruby, more common in a language like C when a bottleneck has been shown to exist that can only be remedied by performing some amount of low level optimizations that may make the code slightly more difficult to comprehend. In this case, add comments that explain the behavior of the code thoroughly.
This is a very broad question with plenty of room for subjective bias.
It is generally accepted that readability is always a good thing. However what is readable varies from person to person.
For example, from your example I would actually prefer the second variant.
There are plenty of people that would say, spread your code out, it helps, but then there are those (myself included) who prefer code that is not so spread out (within limits!) because it allows me to more easily get a feel for the "shape" of the code (structure, loops, conditionals etc) at a distance.
In your example, using one or the other variant does little to affect the readability. But suppose you have a formula like:
r1 = $r*(($objcols-i).to_f+j+k)*3/total_objs
That has a lot more terms in it so it's a lot harder to eyeball. You could pull it apart:
t1 = ($objcols-i).to_f
t2 = t1 + j + k
t3 = $r * t2 * 3
r1 = t3 / total_objs
But does that make it any more readable? In reality this particular formula is just a magic formula to produce a nice random-looking rotation.
You can compromise by spreading it out horizontally:
r1 = $r*( ($objcols-i).to_f+j+k ) * 3 / total_objs
Which at least serves to group the major terms.
But at the end of the day, we are talking about the micro. Whether someone takes 3 seconds or 10 to understand that snippet is not what's important. These are more important:
The reason for the expression must be obvious. IF it's not obvious from the code itself then it needs a comment explaning it.
The code should be easily navigable. This means:
eliminating/reducing repeating code
breaking your program into functions that aren't to small or too large. Again, "too small" and "too large" are subjective terms and there are often exceptions.
Explaining the more complex and high-level structures (e.g. large networks of interacting classes) with plenty of comments.
Lastly, did you know you can do this? (At least in ruby 1.9+):
random_animal = animals.sample
which takes a random element from animals.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am doing a Introductory to Computer Science lab once a week. I was hoping to have a quick contest at the end of my next lab. I want to give them a block of code like this:
public class EfficientCode{
public static void main(){
long startTime, endTime, executionTime;
startTime = System.currentTimeMillis();
yourEfficientMethod():
endTime = System.currentTimeMillis();
executionTime = endTime – startTime;
}
public static void doSomething(){
// you do this part.
}
}
They will implement the doSomething method and the person with the fastest code will get a handful of bonus marks.
The problem is that the question needs to be somewhat simple. The students have a good grasp of: loops, if/else, Strings, adding, arrays, etc.
Here are my ideas for what the question could be:
find all the perfect numbers between 1 and 1,000,000. (A perfect number is a number where all of the number's factors add up to the number. Ie: 6 = 3 + 2 + 1)
find all prime numbers between 1 and 1,000,000
I think in order for there to be a measurable difference in performance between methods you must do something many times.
Agreed about 'many times' for short operations, but for longer ones, once might be enough on its own.
I suggest looking into Project Euler, an excellent collection of programming questions. The best part is that the problems are designed with a "one minute rule" in mind, that most problems should take a moderate computer less than one minute to execute an efficient algorithm to find the answers. So an excellent place to start. :)
Two things.
First, efficiency is about more than execution time. It also is about memory usage, memory access, file-system/resource access, etc. There are tons of things that go into efficiency. So please be explicit that you're looking for the routine with the shortest run-time. Otherwise you are sending a mixed message...
Second, I heard this problem about 15 years ago, and I can't forget it:
Produce a list of all 5-digit number pairs that sum to 121212. However, neither of the 2 numbers can repeat a decimal digit. So 1 can only appear once in either number. So an example result pair is 98167 + 23045. There are a fair number, and it's easy to build a brute-force solution, but an efficient solution requires some thought. There are 192 unique pairs...
Because this is an introductory class and your students haven't covered sorting yet I think it's going to be very hard to come up with something simple enough to do, interesting enough to have a few different ways of doing it, and complex enough that there is an appreciable difference in speed between the different implementations on a modern computer. Your real problem, though, is that anything simple enough for them to try already has a canonical implementation only a short Google search away.
My suggestion is to invert the challenge. Have your students compete to come up with the gnarliest, slowest, most memory hogging solution they can think of. I believe it's as educationally valuable to think about all the wrong ways of doing something as it is to think about the right, and it's just as hard to be the worst as it is to be the best. It's easier to see results subjectively as well since bad code will be really slow. No Googling for an answer either. Finally, in my (irrelevant) opinion, this has the added bonus of making the challenge more fun.
Something like finding a string within another string is easier made bad than good. Maybe have them extract all the prime numbers from a 2kb string of random alphanumeric characters. Lots of ways to make a pig's ear of that problem.
Those are good ideas. What about having a sorting question?
Sorting an array of numbers might also be a good idea, since there are a whole bunch of algorithms for it (insertion, selection, quick, heap, etc.) that all have different performance characteristics. This would also give students a chance to learn about big-O notation, etc.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
So there are a lot of posts around here about what are the best ways to teach kids to program. I'm interested in the next step, teaching kids how to debug code that doesn't do what they want, or doesn't always work 100% of the time (I believe these are separate problems, but that could be subjective).
I ask from the point of view of a game developer who already has a working game (ROBLOX) where kids can code up a ton of crazy stuff in our embedded scripting language, which happens to be Lua.
What we are seeing is that as these scripts become more complicated they are suffering from edge cases that the kids didn't consider - ultimately limiting the scope of what they can do. Part of the solution is education and part of the solution is better debugging tools. Thus I ask a two part question:
What high quality, freely available sources of information exist on the internet that we can send aspiring script developers to with any expectation that they would get something valuable out of it? Maybe there aren't any and we need to write some?
What debugging tools do you think would be most useful to kids? I want to hit the payoff vs. complexity sweet spot.
Our target demographic here is motivated kids, mostly 12-15 years old.
IMHO: Never mind tools. Talk them through it. Teach problem-solving skills. And just as importantly, teach testing.
Well for the debugging part, my guess would be three things:
Avoid bugs in the first place by teaching them good programming practice
Test each part with eg. unit-testing (Lunit)
use print() enough for seeing what happens
you might be interested in debugger.lua or Remdebug
Use a decent editor with syntax highlighting, bracket matching, ...
For the general information:
Learning Lua on the Lua-users wiki
The Lua reference manual
Programming in Lua
That's the way I learned using Lua anyway :).
Of course, early start always helps. In the early years, brains aren’t wired to one particular language like in adulthood. http://blog.quib.ly/2012/10/30/can-kids-beat-adults-at-coding/
I don't know about the "sources of information" part. It looks a bit too generic to me. I learned about edge cases with painful experience, and don't know any other means. I'm not sure it is a kind of knowledge that can be taught formally. It's more like an intuitive thing to me. Kind of like swimming: in order to learn, you have to get wet.
But regarding payoff-vs-complexity part, I'd say that nothing beats the good old console + print duet. It might not be as fancy as other debugging means, but its complexity asymptotically approaches 0. And it's something they will be able to use in nearly any environment and any language they encounter in the future (unless something really big happens).
If you have iPad, now there's a nice app that lets you write programs/games/simulations and run it directly from your iPad. The language is Lua.
http://twolivesleft.com/Codea/
I would use Netbeans after stripping it down a bit. It has some very nice code hinting and comprehensible error checking and hinting.
Kids can have restricted access to tools like debuggers as an individual may not be registered as a programmer or (game) software developer in the state or at the national level. Lua can be run in debug or trace mode and there is something to be gained by reading through the program script or code and using a pen and paper with test input values to note the variables and their contents with logic jumps separately noted with any return expectation and assess the output data values created at relevant points. This is sometimes called dryrunning and is used normally prior to first full test in the development process. This can help in coping with sometimes complex logic progress and with stack element contents written from bottom to top or from left to right on the paper.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
What are some simple algorithm or data structure related "white boarding" problems that you find effective during the candidate screening process?
I have some simple ones that I use to validate problem solving skills and that can be simply expressed but have some opportunity for the application of some heuristics.
One of the basics that I use for junior developers is:
Write a C# method that takes a string which contains a set of words (a sentence) and rotates those words X number of places to the right. When a word in the last position of the sentence is rotated it should show up at the front of the resulting string.
When a candidate answers this question I look to see that they available .NET data structures and methods (string.Join, string.Split, List, etc...) to solve the problem. I also look for them to identify special cases for optimization. Like the number of times that the words need to be rotated isn't really X it's X % number of words.
What are some of the white board problems that you use to interview a candidate and what are some of the things you look for in an answer (do not need to post the actual answer).
I enjoy the classic "what's the difference between a LinkedList and an ArrayList (or between a linked list and an array/vector) and why would you choose one or the other?"
The kind of answer I hope for is one that includes discussion of:
insertion performance
iteration performance
memory allocation/reallocation impact
impact of removing elements from the beginning/middle/end
how knowing (or not knowing) the maximum size of the list can affect the decision
Once when I was interviewing for Microsoft in college, the guy asked me how to detect a cycle in a linked list.
Having discussed in class the prior week the optimal solution to the problem, I started to tell him.
He told me, "No, no, everybody gives me that solution. Give me a different one."
I argued that my solution was optimal. He said, "I know it's optimal. Give me a sub-optimal one."
At the same time, it's a pretty good problem.
When interviewing recently, I was often asked to implement a data structure, usually LinkedList or HashMap. Both of these are easy enough to be doable in a short time, and difficult enough to eliminate the clueless.
This doesn't necessarily touch on OOP capabilities but in our last set of interviews we used a selection of buggy code from the Bug of the Month list. Watching the candidates find the bugs shows their analytical capabilities, shows the know how to interpret somebody elses code
Write a method that takes a string, and returns true if that string is a number.(anything with regex as the most effective answer for an interview)
Please write an abstract factory method, that doesn't contain a switch and returns types with the base type of "X". (Looking for patterns, looking for reflection, looking for them to not side step and use an if else if)
Please split the string "every;thing|;|else|;|in|;|he;re" by the token "|;|".(multi character tokens are not allowed at least in .net, so looking for creativity, the best solution is a total hack)
Graphs are tough, because most non-trivial graph problems tend to require a decent amount of actual code to implement, if more than a sketch of an algorithm is required. A lot of it tends to come down to whether or not the candidate knows the shortest path and graph traversal algorithms, is familiar with cycle types and detection, and whether they know the complexity bounds. I think a lot of questions about this stuff comes down to trivia more than on the spot creative thinking ability.
I think problems related to trees tend to cover most of the difficulties of graph questions, but without as much code complexity.
I like the Project Euler problem that asks to find the most expensive path down a tree (16/67); common ancestor is a good warm up, but a lot of people have seen it. Asking somebody to design a tree class, perform traversals, and then figure out from which traversals they could rebuild a tree also gives some insight into data structure and algorithm implementation. The Stern-Brocot programming challenge is also interesting and quick to develop on a board (http://online-judge.uva.es/p/v100/10077.html).
Follow up any question like this with: "How could you improve this code so the developer who maintains it can figure out how it works easily?"
Implement a function that, given a linked list that may be circular, swaps the first two elements, the third with the fourth, etc...
I like to go over a code the person actually wrote and have them explain it to me.
Asking them to write a recursive algorithm for a well known iterative solution (i.e. Fibonacci etc. -- we give them an iterative function, if needed) and then have them compute the run time for it.
Many times the recursive function involves a tree data structure. The number of times the person has failed to recognize that baffles me. It becomes slightly difficult to calculate the run time until you can see that it's a tree structure...
I find that this problem covers many areas. Namely, their code-reading ability (if they are given an iterative function), code-writing ability (since they write a recursive function), algorithm, data-structure (for run-time)...
A trivial one is to ask them to code up a breadth-first search of a tree from scratch. Yeah, if you know what you're doing it is trivial. But a lot of programmers don't know how to tackle it.
One that I find more useful still is as follows. I've given this in a number of languages, here is a Perl version. First I give them the following code sample:
# #a and #b are two arrays which are already populated.
my #int;
OUTER: for my $x (#a) {
for my $y (#b) {
if ($x eq $y) {
push #int, $x;
next OUTER;
}
}
}
Then I ask them the following questions. I ask them slowly, give people time to think, and am willing to give them nudges:
What is in #int when this code is done?
This code is put into production and there is a performance problem that is tracked back to this code. Explain the potential performance problem. (If they are struggling I'll ask how many comparisons it takes if #a and #b each have 100,000 elements. I am not looking for specific terminology, just a back of the envelope estimate.)
Without code, suggest to make this faster. (If they propose a direction that is easy to code, I'll ask them to code it. If they think of a solution that will result in #int being changed in any way (eg commonly order), I'll push to see whether they realize that they shouldn't code the fix before checking whether that matters.)
If they come up with a slightly (or very) wrong solution, the following silly data set will find most mistakes you run across:
#a = qw(
hello
world
hello
goodbye
earthlings
);
#b = qw(
earthlings
say
hello
earthlings
);
I'd guess that about 2/3 of candidates fail this question. I have yet to encounter a competent programmer who had trouble with it. I've found that people with good common sense and very little programming background do better on this than average programmers with a few years of experience.
I would suggest using these questions as filters. Don't hire someone because they can answer these. But if they can't answer these, then don't hire them.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Since Graduating from a very small school in 2006 with a badly shaped & outdated program (I'm a foreigner & didn't know any better school at the time) I've come to realize that I missed a lot of basic concepts from a mathematical & software perspective that are mostly the foundations of other higher concepts.
I.e. I tried to listen/watch the open courseware from MIT on Introduction to Algorithms but quickly realized I was missing several mathematical concepts to better understand the course.
So what are the core mathematical concepts a good software engineer should know? And what are the possible books/sites you will recommend me?
Math for Programmers. A good read.
Boolean algebra is fundamental to understanding control structures and refactoring. For example, I've seen many bugs caused by programmers who didn't know (or couldn't use) deMorgan's law. As another example, how many programmers immediately recognize that
if (condition-1) {
if (condition-2) {
action-1
} else {
action-2
} else {
action-2
}
can be rewritten as
if (condition-1 and condition-2) {
action-1
} else {
action-2
}
Discrete mathematics and combinatorics are tremendously helpful in understanding the performance of various algorithms and data structures.
As mentioned by Baltimark, mathematical induction is very useful in reasoning about loops and recursion.
Set theory is the basis of relational databases and SQL.
By way of analogy, let me point out that carpenters routinely use a variety of rule-of-thumb techniques in constructing things like roofs and stairs. However, a knowledge of geometry allows you to solve problems for which you don't have a "canned" rule of thumb. It's like learning to read via phonetics versus sight-recognition of a basic vocabulary. 90+% of the time there's not much difference. But when you run into an unfamiliar situation, it's VERY nice to have the tools to work out the solution yourself.
Finally, the rigor/precision required by mathematics is very useful preparation for programming, regardless of specific technique. Again, many of the bugs in programming (or even specifications) that I've seen in my career have sloppy thinking at their root cause.
I would go with the fields that Landon stated:
Discrete Math, Linear Algebra,
Combinatorics, Probability and
Statistics, Graph Theory
and add mathematical logic.
This would give you a grip on most fields of CS. If you want to go into special fields, you have to dive into some areas especially:
Computer graphics -> Linear Algebra
Gaming -> Linear Algebra, Physics
Computer Linguistics -> Statistics, Graph Theory
AI -> Statistics, Stochastics, Logic, Graph Theory
In order of importance:
Counting (needed for loops)
Addition, subtraction, multiplication, division.
Algebra (only really required to understand the use of variables).
Boolean algebra, boolean logic and binary.
Exponents and logarithms (i.e. understand O(n) notation).
Anything more advanced than that is usually algorithm-specific or domain-specific. Depending on which areas you are interested in, the following may also be relevant:
Linear algebra and trigonometry (3D visualization)
Discrete mathematics and set theory (database design, algorithm design, compiler design).
Statistics (well, for statistical and/or scientific/economic applications. possibly also useful for algorithm design).
Physics (for simulations).
Understanding functions is also useful (don't remember what the mathematical term is for that area), but if you know how to program you probably already do.
My point being: A ten year old should know enough mathematics to be able to understand programming. There isn't really much math required for the basic understanding of things. It's all about the logic, really.
"Proof by induction" is a core mathematical concept for programmers to know.
Big O notation in general algorithm analysis, and in relation to standard collections (sorting, retrieval insertion and deletion)
For discrete math, here is an awesome set of 20 lectures from Arsdigita University. Each is about an hour and twenty minutes long.
Start with what we CS folks call "discrete math". Calculus and linear algebra can come in quite handy too because they get your foot in the door to a lot of application domains. Once you've mastered those three, go for probability theory. Those 4 will get you to competency in 95% (I made that up) of application domains.
Concrete Mathematics covers most of the major topics. A good book on Discrete Math, like Rosen's Discrete Mathematics and Its Applications, will fill in any gaps.
I think it depends on your focus. A few years ago I purchased the set of Art of Computer Programming by Donald Knuth. After looking at the books I realized pretty much everything is calculus proofs. If you're interested in developing your own generic algorithms and proofs for them, then I recommend being able to understand the above books since its what you'd be dealing with in that world. On the other hand if you only want/need to use various sorting/searching/tree/etc... routines then big O notation at a minimum, boolean math, and general algebra will be fine. If you're dealing with 3D then geometry and trig as well.
I tend to be more on the using side than making proofs, and while I'd like to think I've done some clever things over the years I've never sat down and developed a new sorting routine. The best advice I can give is learn what you need for your field, but expose yourself to higher levels so you know it exists and how much more there is to learn, you won't get much growth otherwise.
I would say boolean logic. AND, OR, XOR, NOT.
I found as programmer we use this more often than the rest of math concepts.
Basic Algebra and Statistics are good starting points, and the foundation for a lot of other fields.
Here is a simple one that baffles me when I see developers that don't understand it:
- Order of Operations
Chapter 1 of "The Art of Computer Programming" aims to provide exactly this.
There was a book that was recommended...the title was something like Concrete Mathematics. It was recommended in a few questions.
Back in school, on of my instructors said for business applications, all you need to know know add, subtract, multiply, and divide. All other formulas the requester will know and inform you what is needed. Now realize that this is for financing reporting and application focused school. To this day, this has held true for me. I have never once needed to know more than that.
Check the book Foundations of Computer Science
This book is authored by: Al Aho and Jeff Ullman and the entire book is available online.
This is what the authors say in their Preface about the goal of this book:
"Foundations of Computer Science covers subjects that are often found split
between a discrete mathematics course and a sophomore-level sequence in computer
science in data structures. It has been our intention to select the mathematical
foundations with an eye toward what the computer user really needs, rather than
what a mathematician might choose."
a site for brushing up on Math:
http://www.khanacademy.org/
My math background is really poor (Geologist by training), but I took a discrete math class in high school and I use the concepts every day as a programmer. It is probably the most valuable class I took in all of my education as it relates to my current profession.
Discrete Math
Linear Algebra
Combinatorics
Probability and Statistics
Graph Theory
Boolean Algebra
Set Theory
Discrete Math
Well, that depends on what you goal is. As someone said, Linear Algebra, Combinatorics, Probability and Statistics and Graph Theory are important if you're into solving hard problems. Asymptotic growth of functions (bit-Oh notation) is very important. You will also need to master summations and series if you need to work on analyzing some more complex algorithms (see the appendix on Cormen&others Intro to Algorithms).
Even if you're into "Java for the enterprise" or "server-side PHP", you will find some Statistics and Algorithm Complexity (hence combinatorics, induction, summations, series, etc) useful when your boss wants you to get the server to work faster, and adding new hardware doesn't seem to help. :-) I've been through that once.
Boolean Algebra
Set Theory
Why is everybody including probability and statistics in the gold list without mentioning calculus? One cannot understand what probability and statistics are about without at least a working knowledge of limits, derivatives, integrals and series. And all in all, calculus (together with linear algebra) is the workhorse of all mathematics.
I think algorithms and theory are of great importance. Being able to come up with a fast, and correct solution is what differentiates good programmers from the rest. Also, being able to prove your algorithm (using standard proof techniques-- induction, contradiction, etc) is equally important.
Yeah, I would say a basic understanding of induction helps so that you understand what n represents in algorithms. Also some Logic and Discrete Structures is helpful.
Probability and Statistics are very helpful if you ever have to do anything resembling machine learning.
I cover the basics in my "Computing Your Skill" blog post where I discuss how Xbox Live's TrueSkill ranking and matchmaking algorithm works.