2

I have a string that contains numbers. The string needs to separate the numbers by chunks of 3. If necessary, the last chunk, or the last two chunks can contain 2 numbers (but never 1).

Examples:

"123456789" -> "123-456-789"
"1234567891" -> "123-456-78-91"
"12345678912" -> "123-456-789-12"

Working with chunks of three is straight forward:

'-'.join(S[i:i+3] for i in range(0, len(S),3))

How can I extend this?

2
  • 2
    What defines "the correct way"? No one can read your thoughts... Commented Sep 11, 2018 at 2:06
  • 2
    Sorry about that... updated. Commented Sep 11, 2018 at 2:16

4 Answers 4

1

Another way to describe your condition is: "Break everything up into chunks of three. If the last chunk has one element, grab one off the chunk before". textwrap.wrap provides a nice tool for breaking the strings up:

from textwrap import wrap

pieces = wrap(S, 3)
if len(pieces[-1]) == 1:
    pieces [-2:] = pieces[-2][:-1], pieces[-2][-1] + pieces[-1]
result = '-'.join(pieces)
Sign up to request clarification or add additional context in comments.

Comments

0

I made a simple program that would be easy to understand, to solve this problem. You can do it as:

def SeparateNum(S):

    l=len(S)
    S0=""
    rem=l%3

    if rem==0:
        for i in range(l):
            if i%3==0 and i!=0:
                S0=S0+"-"
            S0=S0+S[i]
    elif rem==1:
        for i in range(l-4):
            if i%3==0 and i!=0:
                S0=S0+"-"
            S0=S0+S[i]
        S0=S0+"-"+S[l-4]+S[l-3]+"-"+S[l-2]+S[l-1]
    elif rem==2:
        for i in range(l-2):
            if i%3==0 and i!=0:
                S0=S0+"-"
            S0=S0+S[i]
        S0=S0+"-"+S[l-2]+S[l-1]
    return S0

print(SeparateNum("12345678987"))
print(SeparateNum("123456789"))
print(SeparateNum("1234567898"))

Comments

0
result = '-'.join(S[i:i+3] for i in range(0, len(S),3)) if len(S)%3 != 1 else '-'.join([S[i:i+3] for i in range(0, len(S)-4, 3)] + [S[-4:-2], S[-2:]])

1 Comment

While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.
0

This problem would be most easily solved by a regular expression. You can use re.findall with the following regex to find either one or two groups of double digits at the end of the string or triple digits elsewhere, and then use join the non-empty groups with '-' using a generator expression:

import re
def format_phone_number(s):
    return '-'.join(i for t in re.findall(r'(\d{2})(\d{2})?$|(\d{3})', s) for i in t if i)

so that:

print(format_phone_number('123456789'))
print(format_phone_number('1234567891'))
print(format_phone_number('12345678912'))

would output:

123-456-789
123-456-78-91
123-456-789-12

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.