75 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
  • 33.5k
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
  • 43.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
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
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
  • 33.5k
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
  • 862
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,408
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 - on strike's user avatar
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.4k
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.9k
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
  • 66.4k
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,192
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,757
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
  • 43.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
12 votes
Accepted

Python algorithm that converts array-like data into MathJax

This looks like a handy tool to have around, maybe we can make it even better. Style As per the official Python Style Guide (often known by his nickname PEP8), keyword-arguments of functions should ...
AlexV's user avatar
  • 7,250
11 votes

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

Possible bug The formatter is using an incorrect identifier for the hours. hh means " Hour in am/pm (1-12)", so it works with 12h format and a AM/PM marker. Since ...
Tunaki's user avatar
  • 9,243
11 votes

Format an integer with thousands separator (recursive implementation)

ArtemyVysotsky has provided you with a improved solution, so I am just going to give some general advice: Do not use using namespace std; since it not only lowers ...
Ben Steffan's user avatar
  • 5,228
11 votes

Stopwatch class in C++

Misc.: Modern compilers all support #pragma once. It's much cleaner than old-school include guards. You need a ...
user673679's user avatar
  • 11.7k
11 votes

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

There is indeed a better way. ...
Reinderien's user avatar

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