16

Read PEP8, it will give you directions on how to write Python code that look like Python code to other. Other than that, the behaviour you're seeking is already implemented in range: >>> a = range(7, 0, -2) >>> list(a) [7, 5, 3, 1] You just need to reverse it to form the full hourglass: >>> a = range(7, 0, -2) >>> list(...


10

The roll function generates a random sequence of integers. The loop body shows me that you know how to seed a pseudo-RNG with a true source of randomness but you're doing it for every iteration. You should seed the pseudo-RNG once and then use it in the loop. You know how big your rand vector will be by the time you're done with it so you should reserve ...


10

You should generally try to avoid global variables (like tableSize). Since they can be used everywhere ("globally"), at the worst case you need to read all of the code to figure out where they come from and how they're used. In this case, tableSize could become a parameter of multTable. While it will probably work on the compilers you'll come across, void ...


6

If A.ndim is not in 1, 2, 3, your code tries to return a non-existing string s. It would be better to be explicit about what your code supports atm: def ndtotext(A, w=None, h=None): ... else: raise NotImplementedError("Currently only 1 - 3 dimensions are supported") return s While we are at the point of having your code be clear about ...


5

switch vs. if In switch (rpgLevelGrid[i][j]) { case 1: cout << "X"; break; default: cout << "-"; } do not use switch when all you need is a regular if. Instead do if (rpgLevelGrid[i][j]) { cout << "X"; } else { ...


4

Cute project ;-) Do you see where copy-paste coding got the better of you? if !(1 <= c[5] && c[5] <= len(rightArms)) { return s, errors.New("right arm code out of range") } if !(1 <= c[6] && c[6] <= len(torsos)) { return s, errors.New("right arm code out of range") } if !(1 <= c[7] && c[7] <= len(bases)) { ...


4

I took your code and started condensing it, and came up with the following improvements that could be made: Change the inner loops to use the string constructor that takes a character and a count instead of using a loop Give the method and variables more meaningful names Add a size parameter that can be passed to the method Put declarations and incrementing ...


4

Shell programs should start with a shebang, so that when executed, the OS can select the correct interpreter: #!/bin/bash With Bash, we can read prompted input using read -p: read -p "Enter the mininum number of stars: " min We should check that we have a valid positive integer before using $min. The same comments also apply to $max. For both variables,...


3

It looks nice. Can you please make me understand how it works For example function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);} Output * * * * * *********** * * * * Sandbox Let ...


3

Framebuffer() Could end with charBuffer = std::vector<char>(height * width, ' '); textColorBuffer = std::vector<Color>(height * width, {255u, 255u, 255u}); backgroundColorBuffer = std::vector<Color>(height * width); instead of calling clear. void clear() Alternative implementation and let container implementation decide ...


3

Order your includes at least by portable / non-portable. Not a huge fan of omitting private and putting all the private members up top. IMO a class interface should go from public to private which makes for easier reading as a user. The whole thing is a bit hard to read. Some linebreaks and maybe even spaces would make this easier on the eyes. Is there a ...


3

Review The Python style guide [PEP8] advises to use 4 space indentation You don't have to sort the words Counter has some features that allow you to get the most common occurrences, aptly named most_common() String formatting is cleaner vs manual string appending Instead of manually concatenating use str.format() or even f"{string}" with Python3.6+ Code ...


3

Use a bit more spaces. It will make your code more readable. Try to be coherent with your indents You should sanitize input. What happen if user enter a number greater than 9 ? Or 0, 1, or 2? Or negative number? Or not a number? Never trust user, They all try to broke your program. When you just print on char, prefer putchar() instead of printf(). Here's my ...


3

There's a lot that can be improved in your code, especially if you are using C++. The more advanced features of C++ actually help you organize your code better, help you write less code to do what you want, and remove a lot of tedium that you find in C-style code. Use proper names for variables and functions Names should accurately reflect what the purpose ...


3

The recursion you used is very cumbersome, and should be avoided in favour of more expressive solutions in Haskell. You figured out how to write replicate n c — why didn't you just run with that? Add some list comprehensions, and you're done! Instead of using recursion in printTriangle to print one line at a time, you can just write putStr $ unlines. ...


3

Is the graphic representation of a data structure an integral part of that data structure (val gString = myGrid.asText) or separate and independent from the data structure (val gString = asText(myGrid))? I tend to favor the former, but if the Grid API is solid and supplies everything needed for one or more graphic representations, then the latter is ...


3

My Bash complains about sum=`expr $sum + 1`, and it ain't pretty: *expr: syntax error What to make of that? Good ol' Bash won't treat you nicely if you don't treat it nicely! If I use set -u, things become clearer: *script.sh: line 13: sum: unbound variable Aha! Don't forget to initialize variables, sum=0 in this case. Speaking of sum=`...`, use sum=...


3

First of all, please avoid using namespace std;. It is considered bad practice and will cause many problems. See Why is using namespace std; considered bad practice? for more information. Now let's try to simplify the logic. The following loop: for (int j = 1; j <= spaces_before; j++) { cout << ' '; } is a very verbose way to write std::...


2

Another way to fill the array is to first ensure it is initialized outside any for-loop to contain only blank tiles, and then to just add the border. For example: int rpgLevelGrid[dim][dim] = {}; // Initialize with all-zeroes for (int i = 0; i < dim; i++) { rpgLevelGrid[i][0] = 1; rpgLevelGrid[i][dim - 1] = 1; rpgLevelGrid[0][i] = 1; ...


2

A view more suggestions with explanation to the current source code (first BlackRedBST). I deleted public static final NodeRB NILL = null; because null can be used instead of NILL for less code. I also deleted public static final boolean RED = true; and public static final boolean BLACK = false; because color is never used (in the given code). There is no ...


2

Minor ideas: Repetitive calls This is an alternative idea, not a recommendation. A way to avoid repeated calls to puts(), and still maintain code "art", use string literal concatenation. puts( "┏━━━╤━\n" "┃┃\n" "┃┃\n" "┃┃\n" "┃┃\n" "┻┻━━━━━━━"); Note an optimizing compiler ...


2

This code is not bad at all, and I like the new ASCII art. Here are some ideas on how to improve it further: Don't confuse the reader By grouping things together in an enum it's true that it eliminates "magic numbers" but it also tends to mislead the reader into thinking that these items are related. In this case, there are really three independent ...


2

It's strange that you're using vectors instead of std::complex to represent your complex numbers. I think you'll find that using the standard complex-number class will simplify your code. It's much more efficient and accurate to square a number by simply multiplying rather than going via the much more general std::pow(). Of course, with std::complex, you ...


2

Good work! I would only like to give you one piece of advice. I see this line in you code: typedef std::vector<double> tuple; I don't know if you are aware of this or not, but there is actually a data structure in the C++ standard library called std::tuple. Click on the text for more information about it. I see that you did not include the std::tuple,...


2

The drawings may be arbitrarily complex. As you grow the program, you might want to add color, animation, who knows what. Thus, your drawings are resources, and should be treated as such. This answer provides code, links, and explanations. In short: create one or more (your call) text files store the drawings in some format (your call) in the text files ...


2

You can effectively compress the data by defining two strings, representing a format and a mask. private static final String IMG_FMT = " ________%n" + " | \\|%n" + " o |%n" + " /|\\ |%n" + " | |%n" + " / \\ |%n" + " ___________|___%n" + " | %2d/10 |%n" + " | R.I.P |%...


2

Here's my array-functional spin on the task. It is not likely to be faster than @ArtisticPhoenix's solution, but it provides the desired output without loop constructs or conditions. ...just different for the sake of being different. The process generates a full-sized array of strings that "looks" like a vertical stroke of asterisks, then replaces the ...


2

Rather than send to the console as you build. Build a string then send to the console. This avoids some of the overhead associated with writing to IO. In terms of performance you can use a System.Text.StringBuilder and avoid some of the allocation overhead of new String. StringBuilder will allocate new memory as the string grows, doubling the buffer each ...


2

Good start mate. Few items for you to consider: Things tend to get complicated by law of entropy all by them self - your help is not needed (aka: KISS) Use natural names so your code reads as close to English as possible. Learn to be a good listener - if an experienced user tells you huge files, like pictures are not appreciated needlessly, take pain and ...


2

First I will propose only some simple changes to make the code more readable. 1. add #!/bin/bash as first line then it is guaranteed that bash is used 2. use consistent indentation 3. when you do arithmetic evaluation you don't need the $ character to evaluate the variables, 4. use consistent spacing to structure expressions, e.g. (( j=max; j>=i; j-- )) 5....


Only top voted, non community-wiki answers of a minimum length are eligible