0

I have a string of digits:

s = "12345678910"

As you can see it is the numbers 1 through 10 listed in increasing order. I want to convert it to an array of those numbers:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

How can I do it?

4
  • 6
    How one should know what is next – 1, 12 or 12345678910? It would be pretty simple if you've wanted only integers in 0..9 range. Otherwise it is pretty...impossible. Commented Jul 22, 2015 at 0:45
  • 1
    Without additional rules, of course.For example, If values in the string are supposed to be sorted (i.e. previous is always less than current or otherwise) and you should write the first value that is greater than current. (1 < 2 < 3 < ... < 8 < 9 < 10) Commented Jul 22, 2015 at 0:51
  • I edited the question for clarity. Is this what you had in mind? Commented Jul 22, 2015 at 19:42
  • This is an interesting question, even though it is not stated precisely. I hope four votes to reopen are suffiicient for resuscitation. If reopened, I will post an answer that contains what I think is a precise statement of the problem. That may help Amritdeep clarify the question (with an edit). Commented Jul 24, 2015 at 19:49

3 Answers 3

5

How about this:

a = ["123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"]
b = a.first.each_char.map {|n| n.to_i }
if b.size > 8
  c = b[0..8]
  c += b[9..b.size].each_slice(2).map(&:join).map(&:to_i)
end

# It would yield as follows:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

For later numbers beyond 99, modify existing predicate accordingly.

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

1 Comment

How would you "modify existing predicate" for an arbitrary string? That is, your code should not make assumptions about the number of digits in the last integer extracted from the string.
1

Assuming a monotonic sequence, here's my run at it.

input = a.first.chars
output = []
previous_int = 0

until input.empty?
  temp = []
  temp << input.shift until temp.join.to_i > previous_int
  previous_int = temp.join.to_i
  output << previous_int
end

puts output.to_s

#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

5 Comments

Note: the desired output asks for ints in the output, not strings.
Monotone sequence is not narrow enough of a condition to disambiguate the question. [1, 2, 34, 567, 8910] would be another solution. It is primarily the OP's fault, but you are also wrong in making it sound like you disambiguated the question.
@sawa - Along with the accompanying code it does. We're not doing rigorous proofs here, we're answering questions as best we can. Nice counterexample by the way.
Nice one. You might consider making the sequence monotonically non-decreasing rather than increasing. You could meet @sawa's objection by stating the assumption that if n is a natural number extracted from the string, the next number m extracted will have as few digits as possible (provided m >= n).
@Cary - Thank you. I'll let you take it from here if it gets reopened.
0

Assumptions

  • the first (natural) number extracted from the string is the first character of the string converted to an integer;
  • if the number n is extracted from the string, the next number extracted, m, satisfies n <= m (i.e., the sequence is monotonically non-decreasing);
  • if n is extracted from the string, the next number extracted will have as few digits as possible (i.e., at most one greater than the number of digits in n); and
  • there is no need to check the validity of the string (e.g., "54632" is invalid).

Code

def split_it(str)
  return [] if str.empty?
  a = [str[0]]
  offset = 1
  while offset < str.size
    sz = a.last.size
    sz +=1 if str[offset,sz] < a.last
    a << str[offset, sz]
    offset += sz
  end
  a.map(&:to_i)
end

Examples

split_it("12345678910")
  #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

split_it("12343636412252891407189118901")
  #=> [1, 2, 3, 4, 36, 36, 41, 225, 289, 1407, 1891, 18901]

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.