As of May 31, 2023, we have updated our Code of Conduct.
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: ...
AJNeufeld's user avatar
  • 33k
42 votes

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

The best solution is to use str.join(), using ', ' as the joining string. ...
Peilonrayz's user avatar
  • 42.3k
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 ...
jonrsharpe's user avatar
  • 13.9k
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: ...
Simon Forsberg's 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 ...
Attilio's user avatar
  • 1,645
17 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";
amisax's user avatar
  • 271
17 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 ...
Ludisposed's user avatar
  • 11.5k
17 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 ...
Oh My Goodness's user avatar
16 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,...
Lando's user avatar
  • 257
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 ...
Jan Korous's user avatar
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. ...
OrangeDog's 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 ...
AJNeufeld's user avatar
  • 33k
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 ...
Edward's user avatar
  • 65k
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 ...
MiniMax's 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: ...
Cornholio's user avatar
  • 911
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 <...
Sep Roland's user avatar
  • 4,278
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, ...
Cody Gray's user avatar
  • 4,509
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 ...
Caridorc's user avatar
  • 27.4k
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 ...
Heslacher's user avatar
  • 50.1k
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: ...
Graipher's user avatar
  • 40.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 ...
Edward's user avatar
  • 65k
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 ...
Comintern's user avatar
  • 4,212
12 votes

Create a C++ string using printf-style formatting

I agree with you that 'printf-style' formatting was by many aspects better than C++'s manipulators: more concise, more varied, etc. That said, I feel like it's a step backwards if we port them into C++...
papagaga's user avatar
  • 5,717
12 votes
Accepted

Checking some rules before a Telegram bot replies to a message

If you want to get rid of your nested code then you should make a function and use guard statements. If we simplify your code a bit, we can have: ...
Peilonrayz's user avatar
  • 42.3k
12 votes
Accepted

Cleaning up date strings in Python

For starter, congratulations, the code is clean and uses rather good constructs. I just have two nitpicks about the layout: I prefer to indent lists before the first row: ...
301_Moved_Permanently's user avatar
12 votes
Accepted

Reformatting a date from YYYY-MM-DD to MM/DD/YYYY

Simpler approach Another option is to construct a Date object (this can be achieved by appending a time at midnight to the date) and pass that to ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
12 votes
Accepted

Printing a multiplication table in C

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 ...
Curtis F's user avatar
  • 334

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