3

I am doing the algorithm challenges from HackerRank and one of the problems needs me to accept input in the form of strings of numbers formatted as follows:

3 4
12 14 16
1 2
3 4
5 6

Now, I know how to iterate through the lines and assign them where they need to go, but my issue is with the second line. The others are two two digit numbers so I've been extracting them by just referencing their index in the string. For example, the first line of numbers would be collected with string[0] and string[-1].

The second line, however is of indeterminate length, and may include numbers shorter or longer than three digits. How would I pull those out and assign them to variables? I'm sure there is probably a way to do it with RegEx, but I don't know how to assign multiple matches in one string to multiple variables.

3
  • Why don't you just split(" ") on spaces and then iterate through the result? Using regex here seems like overkill Commented Aug 21, 2014 at 14:41
  • 1
    Yep. Duh. It's been one of those days today. I just spent like 45 minutes on other more complicated solutions...Thanks! Commented Aug 21, 2014 at 14:48
  • Is there any meaning to the lines? ie, do you want [3,4,12,14, 16,...] or [[3, 4], [12, 14, 16], ...]? Commented Aug 21, 2014 at 15:32

4 Answers 4

3
  import re
  print(re.findall(r"(\d+)",x))

"x" being your line.This will return a list with all the number.

Sign up to request clarification or add additional context in comments.

Comments

0

You mean this?

>>> import re
>>> s = """3 4
... 12 14 16
... 1 2
... 3 4
... 5 6"""
>>> m = re.findall(r'\b\d+\b', s, re.M)
>>> m
['3', '4', '12', '14', '16', '1', '2', '3', '4', '5', '6']

Just pickup each value in the final list and assign it to variables.

Comments

0

So if s is your string,

map(int, s.split())

yields a list of integers:

[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6]

That's basically what skamazin suggested.

Comments

0

Given:

>>> txt='''\
... 3 4
... 12 14 16
... 1 2
... 3 4
... 5 6'''

If the lines have meaning, you can do:

>>> [map(int, line.split()) for line in txt.splitlines()]
[[3, 4], [12, 14, 16], [1, 2], [3, 4], [5, 6]]

If the lines have no meaning, you just want all the digits, you can do:

>>> map(int, txt.split())
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6]

If your source text has the possibility of strings that will not convert to integers:

>>> txt='''\
... 3 4
... 12 14 16
... 1 2
... 3 4
... 5 6
... text that won't be integers
... 99 100 101'''

You can use a conversion function:

>>> def conv(s):
...    try:
...       return int(s)
...    except ValueError:
...       return s
... 
>>> [[conv(s) for s in line.split()] for line in txt.splitlines()]
[[3, 4], [12, 14, 16], [1, 2], [3, 4], [5, 6], ['text', 'that', "won't", 'be', 'integers'], [99, 100, 101]]

Or filter out the things that are not digits:

>>> map(int, filter(lambda s: s.isdigit(), txt.split()))
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6, 99, 100, 101]

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.