2022 Developer Survey is open! Take survey.
74 votes
Accepted

Printing a list as "a, b, c." using Python

TL;DR: Despite this being the currently accepted & highest voted answer, these methods from this answer and variations (1, 2) of it: ...
user avatar
  • 30.4k
42 votes
Accepted

Rendering a person's time clock record in HTML, with many color-coded conditions

There are a few good practices that will help this code out. Close the HTML tags that you open - generate valid HTML Newer browsers will probably render your content properly, but older browsers may ...
user avatar
42 votes

Printing a list as "a, b, c." using Python

The best solution is to use str.join(), using ', ' as the joining string. ...
user avatar
  • 41.6k
23 votes
Accepted

Program to print a Python list as "a, b, and c"

# defining possible lists Don't write comments that describe what the code does; we can see the code, we know what it's doing. If you ever need comments, it's ...
user avatar
  • 13.8k
19 votes

Function to format the local time

First of all, the parenthesis around LocalDateTime.now() are useless. Now, let's see what we're really doing here: ...
user avatar
18 votes

Rendering a person's time clock record in HTML, with many color-coded conditions

That's a big mess of conditionals. Hopefully they all match the comments near them, though given typical code that's likely not the case. You are repeating a lot of HTML code that could be addressed ...
user avatar
17 votes
Accepted

Printing binary trees

First of all, in my opinion, both the algorithm is nice and interesting, and the code is really well-written, broken down into easily understandable functions! Well done! The only addition that I can ...
user avatar
  • 1,635
16 votes
Accepted

Prettify JSON class

Thanks for editing the question. I have few ideas how you might want to polish your code. std::string base class It caught my eye that JSONPretify is derived ...
user avatar
16 votes

Show time in format yyyy-MM-ddThh:mm:ss.SSS

You can single-quote the 'T' to have it accepted as a String literal private final static String DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.SSS";
user avatar
  • 261
16 votes
Accepted

Show time in format yyyy-MM-ddThh:mm:ss.SSS

Don't reinvent the wheel, this is java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME. ...
user avatar
  • 831
16 votes
Accepted

Comma Code - Automate the Boring Stuff with Python

You've got an odd bug with your code. Given the 8th menu item from the greasy spoon café: spam = ["Spam", "Spam", "Spam", "egg", "Spam"] You will get: and ...
user avatar
  • 30.4k
15 votes

Setting a default value if a variable is empty

Lo and behold, the power of PHP 7's null coalesce operator! It is also called the isset ternary operator, for obvious reasons. Here's a passage from php.net's wiki page about it Proposal The coalesce,...
user avatar
  • 251
15 votes
Accepted

Format a timespan with up to two fields

What you want to do, is use early-returns (FTW). Also, let the formatter do the string work for you. Right tool for the job. Consider your code, rewritten in Ideone...: ...
user avatar
  • 96.7k
15 votes
Accepted

K&R 1-21 write an "entab" program

I see some things that could help you improve your program. Learn a recent version of C I still have my K&R first edition from 1978, however it would be a very poor choice to use for learning C ...
user avatar
  • 64.1k
15 votes

Cropping a message using array splits

This is much slower than necessary. It takes time to construct the array, and more to shorten the array word-by-word. It's easy to imagine how this would go if ...
user avatar
15 votes
Accepted

Fantasy game inventory — Ch. 5 Automate the Boring Stuff

I am suggesting to use fstrings and the dictionary items() method. The print(f'{value} {key}') instead of ...
user avatar
  • 872
15 votes
Accepted

Converting time in number of minutes past midnight to HH:MM format

Following @Martin R's comment, I'll make my comment above a solution: printf already supports what you're trying to achieve with your if-then-else jungle: ...
user avatar
  • 871
15 votes
Accepted

Minimal `printf` for integer types in x86 assembly

If, for the sake of this exercise, codesize is the only thing that you care about, then I would dare suggest the following: For divq10, not having to reload the constant will save 5 bytes, and using <...
user avatar
  • 4,058
14 votes

Rendering a person's time clock record in HTML, with many color-coded conditions

The first if block can be narrowed by using a color variable; something like this: ...
user avatar
  • 251
14 votes
Accepted

Format RGB Long as hex string in VB6

The code you have works, and is very nicely-formatted. There are only two things I would change, stylistically speaking: Explicitly pass input parameters by value. This should have been the default, ...
user avatar
  • 4,429
14 votes
Accepted

Python class initialize with dict

Python and the power of unpacking may help you in this one, As it is unclear how your Class is used, I will give an example of how to initialize the dictionary with unpacking. This will work on any ...
user avatar
  • 11.4k
13 votes
Accepted

Pretty printing degrees input to radians

Your code looks more complicated than necessary and some of your special cases are really not needed. I will use an incremental development to avoid unnecessary code: The first version is just based ...
user avatar
  • 27k
13 votes

BigInteger formatting for endless tower defense game

Naming Interfaces are usually named starting with a capital I in .NET. ScientificFormatter Here you have ...
user avatar
  • 49.9k
13 votes

Program to print a Python list as "a, b, and c"

To expand on jonrsharpe's excellent answer, here is how you can make the function that does the actual work, printing the list comma separated with an "and" before the last item, more pythonic: ...
user avatar
  • 40.5k
12 votes

A CSharpish String.Format formatting helper

Two small things I noticed at a glance, both involve this code and it looks like it made it into your refactored version as well. ...
user avatar
  • 30.7k
12 votes

Generating formatted multiplication tables in C++

I see a number of things that could help you improve your program. Don't abuse using namespace std Putting using namespace std ...
user avatar
  • 64.1k
12 votes

Format RGB Long as hex string in VB6

I'll just focus on performance, because that's the fun part. You're correct to be worried about the string manipulations, so you should obviously try to minimize them. The other thing you should be ...
user avatar
  • 4,202

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