Hone logo
Hone
Problems

Custom String Formatting in Go

This challenge focuses on reimplementing a subset of the functionality found in Go's standard fmt package for string formatting. You'll learn how to parse format specifiers and construct custom formatted strings, a fundamental skill for building flexible and user-friendly applications.

Problem Description

Your task is to create a Go function, CustomPrintf, that mimics the behavior of fmt.Printf for a limited set of format verbs. The function should take a format string and a variable number of arguments, and return a formatted string.

Key Requirements:

  • The function must accept a format string and ...interface{} for arguments.

  • It should support the following format verbs:

    • %s: String
    • %d: Base-10 integer
    • %v: Default Go representation of the value
    • %%: Literal percent sign
  • The function should iterate through the format string. When a % is encountered, it should check for a valid verb.

  • If a valid verb is found, it should consume the next argument from the args slice and format it according to the verb.

  • If an argument is provided but there's no corresponding verb, or vice-versa, the behavior is undefined for this challenge (assume valid input for now).

  • Literal characters in the format string should be appended directly to the output.

Expected Behavior:

The CustomPrintf function should return a string where all occurrences of format verbs in the format string are replaced by the string representation of the corresponding argument, formatted according to the verb's specification.

Edge Cases:

  • An empty format string.
  • A format string with no format verbs.
  • A format string with only literal characters.
  • A format string with only percent signs (%%).

Examples

Example 1:

Input:
format: "Hello, %s! You are %d years old."
args: "Alice", 30

Output:
"Hello, Alice! You are 30 years old."

Explanation: "%s" is replaced by "Alice", and "%d" is replaced by "30".

Example 2:

Input:
format: "User ID: %v, Status: %s"
args: 12345, "active"

Output:
"User ID: 12345, Status: active"

Explanation: "%v" is replaced by the default representation of the integer 12345, and "%s" by "active".

Example 3:

Input:
format: "This is a literal percent sign: %% and another: %%."
args:

Output:
"This is a literal percent sign: % and another: %."

Explanation: "%%" is translated into a single literal percent sign.

Example 4:

Input:
format: "No args needed."
args:

Output:
"No args needed."

Explanation: The format string contains no verbs.

Constraints

  • The CustomPrintf function must be implemented in Go.
  • You must not use any functions from the fmt package (e.g., fmt.Sprintf, fmt.Printf, fmt.Fprint, fmt.Errorf, etc.) for the formatting logic itself. You may use fmt.Sprint or similar for basic value conversion if absolutely necessary, but try to implement the core logic yourself.
  • The number of arguments will match the number of format verbs (excluding %%).
  • Input arguments will be of types compatible with the specified format verbs (string for %s, int for %d, interface{} for %v).

Notes

  • You will need to carefully parse the format string character by character.
  • Consider how to iterate through the arguments and keep track of which argument corresponds to which verb.
  • For %d, you'll need to handle integer-to-string conversion.
  • For %v, you can leverage fmt.Sprint as a reasonable approximation for the "default Go representation" for this challenge.
  • Remember to handle the %% case to prevent it from being interpreted as the start of a format verb.
Loading editor...
go