Leading zeros

Hey!

How can I do this:

number_pool = (00000..99999).to_a

So the first number is 00000, the next is 00001, 00002 etc

Thank you :slight_smile:

路路路

--
Posted via http://www.ruby-forum.com/.

Hey!

How can I do this:

number_pool = (00000..99999).to_a

So the first number is 00000, the next is 00001, 00002 etc.

With strings: '00000'..'99999'.

路路路

On Tue, Nov 26, 2013 at 12:28 PM, Anders Friis <lists@ruby-forum.com> wrote:

That was simple. The best way for my application is to do it with
numbers. But if that is not possible, then I will proceed with strings.

Thank you :slight_smile:

路路路

--
Posted via http://www.ruby-forum.com/.

If you need the leading zeros then it's not strings you're after.

FYI, the above array used 28mb of RAM on my PC, so you might be better
off iterating rather than creating the entire array, unless you're going
to use all of them immediately.

路路路

--
Posted via http://www.ruby-forum.com/.

I need the leading zeros later when I have plus and minus to one of the
numbers in the array.

I'm doing a lottery system for school. Where I have to pick random
numbers between 00000 and 99999. After I picked a number it should be
removed from the array. So I thought it was the only way to do this,
even though it uses an awful lot of ram.

路路路

--
Posted via http://www.ruby-forum.com/.

The reason for why I need the leading zeros is: One of the prizes in the
lottery is when the first 3 numbers is the right one.

If the winner number is 00102

Then you also win if your lottery tickets contains 001**

路路路

--
Posted via http://www.ruby-forum.com/.

In that case, wouldn't you want each number set as an array?
Something like:
[[0, 0, 0], [0, 0, 1], [0, 0, 2]]

Then the index positions would be maintained even if you use 2 digit
numbers.

路路路

--
Posted via http://www.ruby-forum.com/.

Here's a quick idea I threw together:

路路路

___________

class Lottery

聽聽attr_reader :previous_winners

聽聽def self.generate
聽聽聽聽Array.new( 5 ) { rand(9) }
聽聽end

聽聽def initialize
聽聽聽聽@previous_winners = []
聽聽end

聽聽def generate_unique
聽聽聽聽val = []
聽聽聽聽begin
聽聽聽聽聽聽val = self.class.generate
聽聽聽聽end while previous_winners.include? val
聽聽聽聽@previous_winners << val
聽聽聽聽val
聽聽end

end

class Ticket

聽聽attr_reader :value
聽聽attr_accessor :owner

聽聽def initialize( owner )
聽聽聽聽@value = Array.new( 5 ) { rand(9) }
聽聽聽聽self.owner = owner
聽聽end

end

_____________

irb(main):036:0* t1 = Ticket.new 'Bob'
=> #<Ticket:0x2fc4f78 @value=[7, 2, 5, 6, 5], @owner="Bob">
irb(main):037:0> t2 = Ticket.new 'Ruth'
=> #<Ticket:0x2f74698 @value=[2, 7, 4, 6, 0], @owner="Ruth">
irb(main):038:0> l = Lottery.new
=> #<Lottery:0x2ef1280 @previous_winners=[]>

All you'd need to do is throw all your tickets into an Array,
generate_unique from the Lottery object, and use Array#select to find
the winners:

irb(main):064:0> l.generate_unique
=> [7, 2, 5, 6, 5]

irb(main):072:0> players = t1, t2
=> [#<Ticket:0x2fc4f78 @value=[7, 2, 5, 6, 5], @owner="Bob">,
#<Ticket:0x2f74698
@value=[2, 7, 4, 6, 0], @owner="Ruth">]
irb(main):073:0> winning_number = [7, 2, 5, 6, 5]
=> [7, 2, 5, 6, 5]
irb(main):074:0> players.select { |player| player.value ==
winning_number }
=> [#<Ticket:0x2fc4f78 @value=[7, 2, 5, 6, 5], @owner="Bob">]

--
Posted via http://www.ruby-forum.com/.

Thank you, Joel!

I used some of the things in your code.

Another question. How can I generate a list of numbers, from 0 to 99999,
ending with the same number.
For example if my end number is 6, then my list would be:

00006
00016
00026
00036
...
99986
99996
etc.

路路路

--
Posted via http://www.ruby-forum.com/.

The obvious answer would be to append a 6 to each string.

('0000'..'9999').map { |n| n + '6' }

路路路

--
Posted via http://www.ruby-forum.com/.

Anders Friis wrote in post #1128645:

Hey!

How can I do this:

number_pool = (00000..99999).to_a

So the first number is 00000, the next is 00001, 00002 etc

Thank you :slight_smile:

what about ?

n = 67
s_n = n.to_s.rjust(5,"0")
p s_n

路路路

--
Posted via http://www.ruby-forum.com/\.

That was simple. The best way for my application is to do it with
numbers. But if that is not possible, then I will proceed with strings.

Thank you :slight_smile:

Why do you need the leading zeros? If it is for display purposes or putting into a string then you can still use numbers e.g. (0 .. 99_999) and use one of printf, sprintf, or % to display them:

  ratdog:~ mike$ pry --simple-prompt
  >> n = 123
  => 123
  >> printf '%05d', n
  00123=> nil
  >> sprintf '%05d', n
  => "00123"
  >> '%05d' % n
  => "00123"

Hope this helps,

Mike

路路路

On Nov 26, 2013, at 6:45 AM, Anders Friis <lists@ruby-forum.com> wrote:

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

The reason for why I need the leading zeros is: One of the prizes in the
lottery is when the first 3 numbers is the right one.

If the winner number is 00102

Then you also win if your lottery tickets contains 001**

Isn't that just the same as a

if your_number >= 100 and your_number <= 199
  puts "You win!"
end

路路路

On 26 November 2013 12:00, Anders Friis <lists@ruby-forum.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

That was simple. The best way for my application is to do it with

numbers. But if that is not possible, then I will proceed with strings.

A number is a number, it does not have leading zeroes per se. It is an
abstract entity that is represented in base 2 in the computer.

In order to visualize a number in base 10 you need to get *a string* out of
it somehow. The string consists of digits. This is so common that #to_s
does that for you and is a method implicitly called in common use cases.

Now, if you want the range to generate integers, you can get a string with
leading zeroes within a loop this way:

    "%06d" % integer

But the program has to generate strings at some point or another.

路路路

On Tue, Nov 26, 2013 at 12:45 PM, Anders Friis <lists@ruby-forum.com> wrote:

Joel Pearson wrote in post #1128660:

In that case, wouldn't you want each number set as an array?
Something like:
[[0, 0, 0], [0, 0, 1], [0, 0, 2]]

Then the index positions would be maintained even if you use 2 digit
numbers.

I like that idea.

But that will create 99999 arrays. Is that even possible? :slight_smile:

路路路

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson wrote in post #1128774:

The obvious answer would be to append a 6 to each string.

('0000'..'9999').map { |n| n + '6' }

Gotta take a look a mapping.

Thanks a lot! :slight_smile:

路路路

--
Posted via http://www.ruby-forum.com/\.

That might be less obvious to some :slight_smile:

路路路

On Nov 27, 2013, at 3:13 AM, Joel Pearson <lists@ruby-forum.com> wrote:

The obvious answer would be to append a 6 to each string.

('0000'..'9999').map { |n| n + '6' }