I want to make 26 alphabetic numbers with Ruby.

Asked 2 years ago, Updated 2 years ago, 48 views

What should I do if I want Ruby to make only 26 alphabetic characters?

integer.to_s(26)
It will be a combination of numbers and alphabets.

ruby

2022-09-30 20:33

5 Answers

How about tr("0-9a-p", "a-z") in String#tr.

0.to_s(26).tr("0-9a-p", "a-z")
# = > "a"
25.to_s(26).tr("0-9a-p", "a-z")
# = > "z"
26.to_s(26).tr("0-9a-p", "a-z")
# = > "ba"

Personally, I feel very uncomfortable when 0 becomes a^^;


2022-09-30 20:33

I heard that this is the Excel column name conversion test site.

class Fixnum
  def bijective_s(base=nil, offset=nil)
    offset||='A'.ord#65
    base||=26
    value = self
    result = [ ]
    while value > 0
      value - = 1
      result<<(value%base+offset).chr
      value/=base
    end
    result.reverse.join
  end
end

0.biotic_s#=>"
1. bijective_s#=>"A"
26.biotic_s#=>"Z"
27.biotic_s#=>"AA"
53.bijective_s#=>"BA"
702.bijective_s#=>"ZZ"
703.bijective_s#=>"AAA"

No. I'm just trying to make a comeback.


2022-09-30 20:33

When implemented as a function:

defdec26(num)
  table=('a'..'z').to_a
  num>25?dec26(num/26)+table [num%26]:table [num%26]
end

However, with the above method, the tables are stacked each time you recursion, so the following is less stressful:

defdec26(num)
  (num>25?dec26(num/26):'')+('a'.ord+num%26).chr
end


2022-09-30 20:33

I would appreciate it if you could give me an example of INPUT and OUTPUT.
In particular, if the specifications are not clear when the digits are moved forward, you may not be able to get the answer you want.

For the time being, I wrote a program a long time ago called Excel column name conversion problem, which is similar to Excel column counting (?)
Here's an example:

1=>A
26 = > Z
27 = > AA
16384 = > XFD

I think there is a lot of room for refactoring because I wrote this code a long time ago, but I will post it for your reference.

class ExcelColConv
  AZ_LENGTH='Z'.ord-'A'.ord+1#26
  OFFSET_NUM='A'.ord-1#64

  # Numbers to Alphabet
  def to_col_string(col_num)
    convert_num',col_num 
  end

  def convert_num(ret,n)
    if n == 0
      ret
    else
      quo,rem=excel_divmod n
      convert_num(rem+OFSET_NUM).chr+ret,quo#recursive           
    end
  end    

  def excel_divmod(n)
    quo,rem=n.divmod AZ_LENGTH
    rem==0?[quo-1,AZ_LENGTH]:[quo,rem]
  end
end

conv = ExcelColConv.new
[1,26,27,16384] .each do|n|
  str=conv.to_col_string(n)
  puts"#{n}=>#{str}"
end

I have also written a blog about this issue, so please read it if you like.

Tried writing the Excel column name conversion problem in Ruby, Perl, C#, and F#


2022-09-30 20:33

I wrote a simple hexadecimal conversion.

class Integer
    def to_26_a
        n = self
        if n == 0
            "a"
        else
            table=[*'a'..'z']
            result=""
            while > 0
                result+=table [n%26]
                n / = 26
            end
            result.reverse!
        end
    end
end

puts(0.to_26_a)#a
puts(1.to_26_a)#b
puts(25.to_26_a)#z
puts(26.to_26_a)#ba


2022-09-30 20:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.