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:
...
42
votes
Printing a list as "a, b, c." using Python
The best solution is to use str.join(), using ', ' as the joining string.
...
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 ...
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:
...
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 ...
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";
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 ...
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 ...
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,...
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 ...
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.
...
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 ...
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 ...
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
...
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:
...
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 <...
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, ...
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 ...
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 ...
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:
...
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 ...
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 ...
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++...
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:
...
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:
...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
formatting × 583python × 123
strings × 95
datetime × 79
c × 77
java × 72
beginner × 67
javascript × 64
c++ × 64
c# × 54
performance × 49
python-3.x × 49
io × 29
console × 27
integer × 25
ruby × 21
floating-point × 18
parsing × 17
python-2.x × 17
php × 16
comparative-review × 16
regex × 16
ascii-art × 16
json × 15
c++11 × 14