Questions tagged [formatting]

A generic term meaning the process by which text transformed into a more meaningful or clearer output. A typical example would be the transformation of a decimal into a particular currency format with the correct number of decimal places.

Filter by
Sorted by
Tagged with
66
votes
1answer
2k views

Printing binary trees

After writing this answer, I was inspired to try to design a more elegant solution to the problem of printing binary trees. And what better tool with which to seek elegance than Clojure? Overall ...
44
votes
2answers
906 views

Tool for creating CodeReview questions

To download the tool and for usage instructions, see the follow up to this question Description I have realized that there's a whole bunch of code I want to have reviewed, but copying file by file, ...
43
votes
1answer
2k views

JSON formatter in your least favorite language

Just joking guys, Brainfuck is an awesome, challenging language. I've tested the following code with bf-x86 compiler and rather big JSON file. I believe code is fully functional on a valid (!) JSON ...
40
votes
8answers
17k views

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

I have coded this mess of JavaScript and a bit of jQuery. Everything here works perfect as intended, however it is a real mess. What would be the best way to clean this up with best practices and ...
36
votes
7answers
94k views

Setting a default value if a variable is empty

This is the pattern I've been using for "if the variable is empty, set a default value", and for variables like $muffin, it has all seemed well and good. But in ...
29
votes
6answers
21k views

“Guess a number” game

I began the journey to be a self-taught programmer about a month ago and my biggest fear about teaching myself is developing bad habits and not realizing it. I'm looking for criticism on this "Guess ...
29
votes
3answers
1k views

Code indentation for declaring inputs/outputs in an array

I'm writing a list of inputs and outputs for to be compared in unit tests. ...
27
votes
6answers
2k views

Formatter class

In our production code, we cannot use Boost or C++0x. Formatting strings using sprintf or stringstream is annoying in this case,...
25
votes
4answers
3k views

Formatting 3 integers (hours, mins, secs) to `00:00:00`?

I have this at the moment, wondering if there's a cleaner way to do it or if this is already as compact as it gets. ...
24
votes
1answer
333 views

Follow-up to tool for posting CodeReview questions

Description This is a follow-up question to Tool for creating CodeReview questions. Things that has changed include: Removed replacing four spaces with one tab, all tabs and all spaces in the code ...
20
votes
4answers
29k views

Create a C++ string using printf-style formatting

It's often convenient to use C-style printf format strings when writing C++. I often find the modifiers much simpler to use than C++ I/O manipulators, and if I'm ...
20
votes
3answers
600 views

Formatted print without the need to specify type matching specifiers using _Generic

This code allows printing in C with a style similar to C++ ostream. By using _Generic() (see ...
18
votes
3answers
2k views

A CSharpish String.Format formatting helper

A while ago I implemented .net's string.Format() method in VB6; it works amazingly well, but I'm sure there has to be a way to make it more efficient. I'll start ...
18
votes
2answers
4k views

Retro Rocket ASCII Art

Inspired by Nested for-loop ASCII art, I looked at building a more complicated ASCII art than just the normal 'diamond' and 'triangle' variations that crop up occasionally here. The challenge: ...
17
votes
5answers
5k views

Printing a list as “a, b, c.” using Python

Is there a better way to do this? Assuming very long lists, is this slow and wasteful? ...
16
votes
1answer
4k views

Pretty printing of the numpy ndarrays

Following this idea for pretty printing of numpy ndarrays, I have developed a very primitive prototype: ...
15
votes
5answers
4k views

Program to print a Python list as “a, b, and c”

I am a beginner (with zero Python experience) working through the book Automate the boring stuff with Python. At the end of Chapter 4, the author provides a review problem. I was supposed to write a ...
15
votes
2answers
1k views

K&R 1-21 write an “entab” program

From K&R exercise 1-21, Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When ...
15
votes
2answers
2k views

Message-formatting code with pluralization, revised to be more functional

I'm a C++ dev, and I've recently started working my way through Clean Code*. Whenever I encounter an example I think I could improve, I try to re-implement it in C++. On pp. 28-29 there is an example ...
15
votes
4answers
5k views

Generating formatted multiplication tables in C++

I am self studying C++ from Robert Lafore's OOP in C++. I am solving the first exercise questions of chapter-3 loop and decisions. I've written three different codes using different approaches. I want ...
15
votes
1answer
436 views

Signed integer-to-ascii x86_64 assembler macro

I have implemented an algorithm which converts a signed integer to its ASCII equivalent string with the corresponding sign as a first byte. I have tested it on several numbers and it worked correctly. ...
14
votes
3answers
28k views

Convert string to multiline text

I made this method that takes any string and transforms it into a multiline text l; each line having the max (character) length specified by the rowLength parameter....
14
votes
2answers
1k views

Python ASCII-Art Table

As a part of a console utilities module, I created a function that takes a table in the form of an array of arrays, and generates an ASCII table with the given contents. I've also added the options ...
13
votes
5answers
6k views

Cleaning up date strings in Python

Working from a CSV of about 14.000 lines, I need to match dates in the CSV against some API. I'm reading the CSV using Pandas (for various other reasons). Most often, the date is just a year, i.e. an ...
13
votes
2answers
2k views

Lightweight rich link Label

After reading the same question on SO about how to bold a word in a label for the nth time today, I decided to write a formatted Label. At first I toyed with the idea of writing a MarkDown clone (...
13
votes
3answers
9k views

Add color to terminal output

I have the following header included in some of my projects so that I can add a little color to the terminal output. Here is how it would be used: ...
12
votes
6answers
3k views

Format RGB Long as hex string in VB6

This is how I convert e.g. RGB(255, 0, 0) (where red is stored in the least significant byte of a Long) to a 6-digit hex string <...
12
votes
4answers
3k views

To understand what recursion is, you must first understand recursion

This is the second assignment for my CS2 course: Write a recursive C++ function writeLine() that writes a character repeatedly to form a line of n ...
12
votes
4answers
1k views

Comma Code - Automate the Boring Stuff with Python

Below is my code for the Comma Code problem from Chapter 4 of Automate the Boring Stuff with Python. Is there anything I can do to make it cleaner? Comma Code Say you have a list value like this: ...
12
votes
4answers
904 views

Cropping a message using array splits

I am trying to write a simple function to crop a given message to a specific length but at the same time not to cut the words in between and no trailing spaces in the end. Example: Input String: ...
12
votes
3answers
2k views

My own C++ Format function

I've never been satisfied with any of the possible sprintf()-like functions in C++: sprintf() is a C function and it's very ...
12
votes
4answers
2k views

Prettify JSON class

It's not much, but I tried employing some of the things I learned yet never really got to use, since that kind of code isn't really needed where I work (for the most part). I tried making it as much C+...
12
votes
2answers
876 views

Python algorithm that converts array-like data into MathJax

I frequently use MathJax, and often I need to write matrices. Since writing matrices using MathJax is very tiresome task, I decided to automate it. Code: ...
12
votes
2answers
15k views

ASCII-fication of playing cards

I am making a small Blackjack game in Python 3. The part of the code I am unsure about is that which allows me to print nice ASCII versions of the card. ...
12
votes
4answers
12k views

Align Strings for Output

I wrote a class which can format input so that it is properly aligned. I'm interested in all suggestions (regarding structure, usability, style, naming, comments, etc). I'm especially wondering ...
12
votes
0answers
401 views

Document formatting markup engine in Postscript

I've been rewriting this same sort of thing off and on over the years, but finally here's the "perfected" monstrosity. Some kruft in the middle with attempting to predict the number of spaces that ...
11
votes
4answers
527 views

Function to format the local time

This is a statement that I use a lot in my code base: (LocalDateTime.now()).format(DateTimeFormatter.ofPattern("ssmmHHddMMMuu")) Should I create a static method ...
11
votes
3answers
1k views

Printing a pyramid

Can you please review this code to determine if it's good or can be improved? ...
11
votes
2answers
2k views

A fraction of the code

Following up on this post, and including some major changes suggested, here's the revised code. Changes include: No longer keeping an IFormatProvider at instance ...
11
votes
2answers
197 views

Ruby format analyser

I have a requirement to validate file names related to architecture after they are uploaded. Once they have been uploaded I must warn the user if the file name is not standards compliant. What's in a ...
11
votes
2answers
8k views

Printing a calendar for a month based on user input

I have a long piece of code that has many repeating points. I am sure that it doesn't take this amount of code to create a simple calendar, but I do not know how to shorten this piece of code. ...
11
votes
1answer
878 views

Poor man's f-strings in Python 2.6

f-strings is a new feature in the upcoming python release which basically lets you do this: >>> s = 'qwerty' >>> f'{s} {1+2}' 'qwerty 3' Not ...
11
votes
1answer
1k views

Formattable Fraction

I have written this small little program, to test how my Fraction type behaves when used with and without a custom ...
11
votes
1answer
338 views

Rust language tag implementation

language-tags is my first crate, it is hosted on GitHub and I uploaded it to Crates.io. It parses language tags like en-US into useful parts, like language and ...
11
votes
1answer
1k views

Stream manipulating indenter

I've always wanted to be able to steer stream indentation, so that I could write code like this: ...
11
votes
1answer
654 views

String interpolation library

I was bored over the last couple of days and wrote up a string interpolation library for JavaScript. I'm very pleased with its functionality, it passes it 79 tests cross browser and the comments and ...
11
votes
3answers
132 views

Simple generic auto __repr__

Preface At some point I was tired of writing & supporting __repr__ methods, so I've decided to write it once and reuse everywhere in my classes. Since I'm ...
10
votes
3answers
20k views

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

My requirement is to convert a time (long) to a String with this date format: yyyy-MM-ddThh:mm:ss.SSS For instance: 2017-03-07T03:52:31.298 My implementation: ...
10
votes
5answers
18k views

Vertically placing the words in a string

Please let me know a better approach to solve below problem: Problem: Vertically arrange the words in a string and print on console. Sample Input: Hello Jack ...
10
votes
2answers
21k views

Pretty-print Pascal's triangle

...

1
2 3 4 5
11