6
\$\begingroup\$

Code Golf Challenge

Given a string of 10 characters: 'abcdefghij'

I want to format it in this pattern and print it in the console in a minimum number of characters: (abc) def-ghij

My solution:

print(f"({s[0:3]}) {s[3:6]}-{s[6::]}")
\$\endgroup\$
14
  • 2
    \$\begingroup\$ This might make a nice little code-golf challenge if you open it up as such. \$\endgroup\$ Commented Nov 7, 2023 at 13:52
  • 2
    \$\begingroup\$ I've cast the final close vote on this, but FWIW I disagree with the close reason most of the voters went with. IMO this is fine as a tips question, it just needs some (not necessarily formal/objective) criteria to optimize for, since pretty code, short code, and fast code can be mutually exclusive and it's currently unclear which you're going for :-) \$\endgroup\$ Commented Nov 7, 2023 at 14:39
  • 2
    \$\begingroup\$ @KarineBauch this is code golf and coding challenges :P. It wasn't clear that you asked for tips to shorten your code, if you edit the challenge to make it more explicit it can probably be reopened. \$\endgroup\$ Commented Nov 7, 2023 at 14:58
  • 1
    \$\begingroup\$ @KarineBauch You can add the code-golf to your question to make it explicit that shortest code is your goal \$\endgroup\$ Commented Nov 7, 2023 at 15:02
  • 1
    \$\begingroup\$ You may want to edit the title as well, as it is currently asking for "better solution" instead of "shortest code". Perhaps you should also remove [tips] as it is no longer relevant with your new question. \$\endgroup\$ Commented Nov 7, 2023 at 16:55

4 Answers 4

6
\$\begingroup\$

Python 3, 35 bytes

print(f"({s:.3}) {s[3:6]}-{s[6:]}")

Try it online!

Uses the solution from the question but saves three bytes:

  1. removes a redundant : in the final slice s[6::], and
  2. uses the precision specifier (.) of the format_spec (the bit after the : in {...:...}) which is overloaded for string types to give a prefix so {s[0:3]} -> {s[:3]} -> {s:.3}
\$\endgroup\$
1
\$\begingroup\$

Python, 36 bytes

slightly improved version of your f-string (see also Jonathan Allan's comment), wrapped in a function

print(f"({s[:3]}) {s[3:6]}-{s[6:]}")

Attempt This Online!


Python, 37 bytes

without format strings

print("("+s[:3]+")",s[3:6]+"-"+s[6:])

Attempt This Online!

uses that print inserts a space between arguments


Python, 39 bytes

C-style format string

print("(%s%s%s) %s%s%s-%s%s%s%s"%(*s,))

Attempt This Online!

printing characters separately is slightly shorter than spiting string in 3 parts

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Why count function wrapper code when the question is explicitly asking for a snippet? \$\endgroup\$ Commented Nov 8, 2023 at 17:35
0
\$\begingroup\$

PowerShell, 46 bytes

"($($a[0..2])) $($a[3..5])-$($a[6..9])"-join''

Try it online!

The output is a little weird, if someone can suggest a fix to that it would be much appreciated!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The question seems to require Python answers. \$\endgroup\$ Commented Nov 10, 2023 at 11:17
0
\$\begingroup\$

Python 2, 35 bytes

print"("+s[:3]+")",s[3:6]+"-"+s[6:]

Simply reduces the second solution of this answer by 2 bytes, since in Python 2 print would be a statement, not a function.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.