4
\$\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\$
    – Adám
    Nov 7 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\$ Nov 7 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\$
    – math scat
    Nov 7 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\$
    – mousetail
    Nov 7 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\$ Nov 7 at 16:55

3 Answers 3

4
\$\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\$ Nov 8 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\$ Nov 10 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.