New answers tagged

3

As pointed out, there is a lot of code repetition and this is a big problem when any changes get made. It's so easy to quickly make a change to one piece of code and miss the 3 other versions of it which do the same thing. Rather than factor out these parts of code into functions though, you can re-factor the code into a recursive function. The idea being ...


2

The first thing I noticed was your indentation. On some lines the indentation is not consistent. Try to keep your indentation consistent as it helps others read your code. You also write a lot of comments in your code but comments are usually not necessary. I only write comments to explain certain design choices which are not clear from the code (for ...


1

it is correctly implemented (?) No. Incorrect result when n <= 10. Goal: Counting the number of digits with a recursion algorithm in c Simply change the condition to handle all int. int countDigits(int n){ // if(n>=0&&n<10){ if(n > -10 && n < 10) { return 1; } else { return 1 + countDigits(n/10); } }


3

I unfortunately cannot comment (not enough reputation), but in terms of performance and generated assembly code both variants are exactly same: int countDigitsSane (int n){ int digits=1; for(int i=n; i>9; i/=10) { digits++; } return digits; } int countDigits(int n){ if(n<10){ return 1; } else{ return 1+countDigits(n/10); ...


2

As mentioned in comments, you should be careful about which integer type you use. int is negative and if your function takes int as parameter, it will therefore be assumed that it can handle negative numbers. So unsigned int might have been a better choice. More importantly, you should be aware that recursion is dangerous, ineffective and often hard to read....


1

A small side note: in create(), you wrote int openspots = 20; // ... while (openspots-- > 0) { // ... } While it looks clean, it's weird to read and should probably be written as a for-loop. This way, you also reduce the scope of openspots to inside the loop and don't clutter up your namespace inside of create(). Also, the twenty seems a bit like a ...


4

Writing down all your basecases takes a lot of space. I would instead use a dictionary, which could even be a global constant. I find using n as an argument for something that is not an integer to be misleading. A generic x or s or roman_numeral would be clearer, IMO. Python has an official style-guide, PEP8. It recommends using four spaces as indentation. ...


4

Object Orientated Programming you don't use object orientated programming! that would clean up the code in a large scale public class Board { //private int[][] board; primitive obsession - use objects instead private Field[][] fields; private Column[] columns; private Row[] rows; private Block[] blocks; public final static int SIZE =...


Top 50 recent answers are included