Convert integer to array?

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

Any help would be greatly appreciated.

On Mon, May 12, 2008 at 6:36 PM, Nadim K. [email protected] wrote:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

irb(main):001:0> x=1234
=> 1234
irb(main):002:0> x = x.to_s.split(‘’)
=> [“1”, “2”, “3”, “4”]

Mikel

1 Like

From: [email protected] [mailto:[email protected]]

Let’s say I have: x=1234

How can I convert that to the follow array: x=[1, 2, 3, 4]

irb is friendly, just play w it…

“1234”.split(//)
#=> [“1”, “2”, “3”, “4”]

your next task is to convert those into integers, then you’re good to
go…

kind regards -botp

On 12.05.2008 10:36, Nadim K. wrote:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

Any help would be greatly appreciated.

x.scan /\d/

robert

Integer to Array

x=1234
y=x.to_s.scan(/d)

or
y=x.to_s.split(//)

or
y=x.to_s.split(’’)

Array to integer

z=y.to_s.to_i

Thanks and Regards,

Lokesh Agrawal
Software Engineer



“Dream is not what you see in sleep, is the the thing which does not let
you
sleep”


Hi –

On Mon, 12 May 2008, Nadim K. wrote:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

require ‘scanf’
“1234”.scanf("%1d" * 4)

=> [1, 2, 3, 4]

David

Hi self –

On Mon, 12 May 2008, David A. Black wrote:

“1234”.scanf("%1d" * 4)

=> [1, 2, 3, 4]

“%1d” * x.size would be better (no need to hard-code the 4).

David

Nadim K. wrote:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

A solution that doesn’t use strings:

result_array = []
while x > 0
result_array.unshift x % 10
x /= 10
end
result_array

This will “destroy” x, though.

HTH,
Sebastian

Hi –

On Mon, 12 May 2008, Mikel L. wrote:

On Mon, May 12, 2008 at 6:36 PM, Nadim K. [email protected] wrote:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

irb(main):001:0> x=1234
=> 1234
irb(main):002:0> x = x.to_s.split(‘’)
=> [“1”, “2”, “3”, “4”]

That’s an array of strings rather than integers, though. See my other
reply, using scanf.

David

2008/5/12 Robert K. [email protected]:

x.scan /\d/
Ah, not really. What I meant was:

x.to_s.scan(/\d/).map {|i| i.to_i}

Cheers

robert

On May 13, 2008, at 7:26 AM, Robert K. wrote:

while x > 0
def int_split(x)

robert


use.inject do |as, often| as.you_can - without end

def int_split(x)
return [0] if x.zero?
r = []
while x > 0
x, b = x.divmod 10
r.unshift b
end
r
end

You don’t need y since Fixnums are immediate. In fact, x is only a
label, doing y=x just adds a new label to the object referred to by
x. (Go ahead, try it with a Bignum.)

It still doesn’t work for negative numbers, but since that behavior
hasn’t been defined, it’s left as an exercise for the OP.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

2008/5/12 Sebastian H. [email protected]:

result_array.unshift x % 10
x /= 10
end
result_array

This will “destroy” x, though.

Well, that’s easily fixed: just work with another variable. You can
also combine division and mod:

def int_split(x)
r = []
y = x
while y > 0
y, b = y.divmod 10
r.unshift b
end
r
end

Kind regards

robert

2008/5/13 Rob B. [email protected]:

x=1234
end
y = x

end
r
end

You don’t need y since Fixnums are immediate.

The reasoning is wrong but comes to the right conclusion: if you want
to retain the original value of x then it does not matter whether
values are mutable or not. It is sufficient to assign to x to loose
the original value.

In my code I don’t need y because x is a method parameter. My remark
was a reaction to Sebastian’s comment and piece of code which modified
x.

It still doesn’t work for negative numbers, but since that behavior hasn’t
been defined, it’s left as an exercise for the OP.

Exactly. :slight_smile:

Kind regards

robert

Hi –

On Tue, 13 May 2008, Robert K. wrote:

Let’s say I have:
x /= 10
r = []
robert
r.unshift b

In my code I don’t need y because x is a method parameter. My remark
was a reaction to Sebastian’s comment and piece of code which modified
x.

I still like scanf :slight_smile:

David

From: David A. Black [mailto:[email protected]]

I still like scanf :slight_smile:

indeed.

i also like the scan+map since i don’t need the formatting (and the
require :wink:

001:0> “1234”.scan(/\d/).map(&:to_i)
=> [1, 2, 3, 4]

kind regards -botp

Mikel L. wrote:

On Mon, May 12, 2008 at 6:36 PM, Nadim K. [email protected] wrote:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

Most of the other ways are better, but none-the-less:

x=1234
array = []

results = x.to_s.split(‘’)
results.each{|x| array << x.to_i}

array #=> [1,2,3,4]

Regards,

  • Mac

Hi,

Rob B. wrote:

def int_split(x)
return [0] if x.zero?
r = []
while x > 0
x, b = x.divmod 10
r.unshift b
end
r
end

Here is a little short version:

def int_split(x)
r=[x];r[0,1]=*r[0].divmod(10)while r[0]>9;r
end

Regards,

Park H.

From: Peña, Botp [mailto:[email protected]]

scan+map since i don’t need the formatting

(and the require :wink:

001:0> “1234”.scan(/\d/).map(&:to_i)

=> [1, 2, 3, 4]

arggh, 1.9 is getting better,

002:0> “1234”.each_char.map(&:to_i)
=> [1, 2, 3, 4]

very readable, imho

kind regards -botp

Nadim K. wrote in post #673293:

Let’s say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

Any help would be greatly appreciated.

This may help…

irb(main):049:0> x = 1234.to_s.split(//)
=> [“1”, “2”, “3”, “4”]
irb(main):050:0> y = x.map{|y| y.to_i}
=> [1, 2, 3, 4]

I am using Ruby 2.2

A solution that doesn’t use strings:

result_array = []
while x > 0
result_array.unshift x % 10
x /= 10
end
result_array

This will “destroy” x, though.

HTH,
Sebastian

Same thing only different :slight_smile:

Positive numbers only

def splitnum(n)
z = Math.log10(n).floor
Array.new(z+1){|i| (n/10**(z-i))%10}
end

#OR

def splitnum2(n)
z = Math.log10(n).floor
(0…z).map{|i| (n/10**(z-i))%10}
end

Harry

Online Karnaugh map solver with circuit