Alphanumeric base conversion in Ruby - ruby

This is another Codewars Ruby problem that's got me stumped:
Description:
In this kata you have to implement a base converter, which converts between arbitrary bases / alphabets. Here are some pre-defined alphabets:
bin='01'
oct='01234567'
dec='0123456789'
hex='0123456789abcdef'
allow='abcdefghijklmnopqrstuvwxyz'
allup='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alpha='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
alphanum='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
The function convert() should take an input (string), the source alphabet (string) and the target alphabet (string). You can assume that the input value always consists of characters from the source alphabet. You don't need to validate it.
Examples:
convert("15", dec, bin) #should return "1111"
convert("15", dec, oct) #should return "17"
convert("1010", bin, dec) #should return "10"
convert("1010", bin, hex) #should return "a"
convert("0", dec, alpha) #should return "a"
convert("27", dec, allow) #should return "bb"
convert("hello", allow, hex) #should return "320048"
Additional Notes:
The maximum input value can always be encoded in a number without loss of precision in JavaScript. In Haskell, intermediate results will probably be to large for Int.
The function must work for any arbitrary alphabets, not only the pre-defined ones.
You don't have to consider negative numbers.
I've been playing with this for a couple of days and managed to get the numeric-base-conversion portion working. It's the alphabetical part of it that I can't figure out how to approach, and my brain is tired from trying. Here's my code:
def convert(input, source, target)
bases = {
:bin => '01',
:oct => '01234567',
:dec => '0123456789',
:hex => '0123456789abcdef',
:allow => 'abcdefghijklmnopqrstuvwxyz',
:allup => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
:alpha => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
:alphanum => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
}
base_from , base_to = 0
src_num_switch = 1 if source == bases[:bin] || [:oct] || [:dec] || [:hex]
tgt_num_switch = 1 if target == bases[:bin] || [:oct] || [:dec] || [:hex]
src_num_switch = 0 if source == bases[:allow] || [:allup] || [:alpha] || [:alphanum]
tgt_num_switch = 0 if target == bases[:allow] || [:allup] || [:alpha] || [:alphanum]
if source == bases[:bin] then base_from = 2
elsif source == bases[:oct] then base_from = 8
elsif source == bases[:dec] then base_from = 10
elsif source == bases[:hex] then base_from = 16
elsif source == bases[:allow] then base_from = 13
elsif source == bases[:allup] then base_from = 13
elsif source == bases[:alpha] then base_from = 13
elsif source == bases[:alphanum] then base_from = 13
else puts ":( no source match found :("
end
if target == bases[:bin] then puts base_to = 2
elsif target == bases[:oct] then base_to = 8
elsif target == bases[:dec] then base_to = 10
elsif target == bases[:hex] then base_to = 16
elsif target == bases[:allow] then base_to = 13
elsif target == bases[:allup] then base_to = 13
elsif target == bases[:alpha] then base_to = 13
elsif target == bases[:alphanum] then base_to = 13
else puts ":( no target match found :("
end
if base_from == base_to then
return input
elsif src_num_switch == 1 && tgt_num_switch == 1 then
return Integer(input, base_from).to_s(base_to)
elsif src_num_switch == 0 && tgt_num_switch == 0 then
return Integer(input, base_from).to_s(base_to)
# ### # :::::::::::::::::::::::::::::::::::::::::::::
else
puts "ouch, something broke"
end
end
I've got everything down to the "# ### #" portion working for me. Can anyone give me an idea of how to do the alpha-base portion? I've tried the following but had no luck:
if base_from == base_to then return input
elsif src_num_switch == 1 && tgt_num_switch == 1 then
return Integer(input, base_from).to_s(base_to)
elsif src_num_switch == 1 && tgt_num_switch == 0 then
if target == bases[:allup] then return bases[input.index].to_s.upcase
elsif target == bases[:allow] then return bases[input.index].to_s.downcase
end
end
elsif src_num_switch == 0 && tgt_num_switch == 1 then
return input.index.to_s(base_to)
elsif src_num_switch == 0 && tgt_num_switch == 0 then
return Integer(input, base_from).to_s(base_to)
else
puts "ouch, something broke"
end
This one too:
elsif src_num_switch == 1 && tgt_num_switch == 0 then # number-base to alphanumeric-base
if target == bases[:allup] then
return bases[input.index].to_s.upcase
elsif target == bases[:allow] then
return bases[input.index].to_s.downcase
end
elsif src_num_switch == 0 && tgt_num_switch == 1 then # alpha-base to number-base
return input.index.to_s(base_to)

There may be a very clever built-in Ruby solution, but I would guess based on the custom alphabets describing the number systems that there is not. So, I don't have a direct answer to how to complete your code, but I would suggest a slightly different strategy.
Converting from a decimal
Any number system can be converted from the decimal system like so:
vals_in_system = system.length
output_in_system = []
while (decimal_num != 0)
index_of_next_val = decimal_num % system.length
output_in_system.unshift(system[index_of_next_val])
decimal_num = decimal_num / vals_in_system # truncating is desired here
end
It's a bit tricky. This algorithm first tries to determine what value it has to put in the last position (which has the most granularity in whatever number system you're using). E.g. if you were to represent 12 in decimal (yes, it already is, but using this algorithm), a 2 has to go in the last position - no number you put in the tens place or higher will otherwise help you represent 12. If you were to represent 3 in binary, a 1 has to go in the last position of the binary - nothing you put in the next position will get you to a 3. Once it determines this, it can divide by the base, which will leave you with the number you would use to calculate the remaining positions. For example, if you were to represent 123 in decimal, dividing by 10 (the decimal base) and truncating would give you 12. 12 is the representation of the original number except for the final position (which was chopped off by dividing by the base). (I realize this isn't the clearest explanation so let me know if you have questions.) Some examples:
E.g. the decimal number 15 can be converted to binary:
15 % 2 = 1 # last position
15 / 2 = 7
7 % 2 = 1 # next to last position
7 / 2 = 3
3 % 2 = 1 # 3rd to last position
3 / 2 = 1
1 % 2 = 1 # 4th to last position
1 / 2 = 0 # stop
That's kinda boring, you just get 1111. Try something a little more interesting, like 10:
10 % 2 = 0 # last position
10 / 2 = 5
5 % 2 = 1 # next to last position
5 / 2 = 2
2 % 2 = 0 # 3rd to last position
2 / 2 = 1
1 % 2 = 1 # 4th to last position
1 / 2 = 0 # stop
And you get 1010, which is indeed 10 in binary. You can do this with any of those alphabets.
Converting to a decimal
Similarly, any number system can be converted to a decimal by doing the opposite:
vals_in_system = from.length
output_in_decimal = 0
val.each_char do |next_val|
output_in_decimal *= vals_in_system
output_in_decimal += from.index(next_val)
end
This is easier to understand than the "from decimal" algorithm. Consider if you were to apply this to the decimal number 123. This algorithm is essentially doing this equation
((1 * 10) + 2) * 10) + 3
or, much easier to read:
1 * (10 * 10) + 2 * (10) + 3
Just iteratively. It works for other number systems, by replacing the 10 with the base of the number system (i.e. the number of values the number system contains). The only other magic it does it converts a value in the number system into a decimal number using .index.
E.g. converting "bcdl" to decimal from their "allow" system. Using a 0-index, b = the 1st position, c = 2nd, d = 3rd, l = 11th
Start with 0
Multiply by the number system base, which is 26 (26 letters in the lowercase alphabet) = 0
Add the decimal value of b (1) => 1
1 * 26 = 26
Add decimal value of c (2) => 28
28 * 26 => 728
Add decimal value of d (3) => 731
731 * 26 => 19006
Add decimal value of l (11) => 19017 That's the decimal notation for "bcdl".
Putting it together
Once you have converters to and from decimal, you can write a pretty straightforward wrapper to handle every situation (I put DEC in a constant to make it visible in this method, it's the same as dec):
def convert(val, from, to)
case
when from == to then val
when from == DEC then convert_from_dec(val, to)
when to == DEC then convert_to_dec(val, from)
else
convert_from_dec(convert_to_dec(val, from), to)
end
end
After that, you mostly have to deal with edge cases.
As I said, not a direct answer to your question, but it seems like you'll have to use this general approach for the alpha number systems, at which point you may as well use it for everything :)

I honestly tried not to look at alexcavalli's solution, but in the end came to exact same algorithm with a different code. So for explanation why it works look at his much more explained answer. Here it's only code, written in a way if you save it under base_converter.rb name you can run it as:
$ ruby ../base_converer.rb 123 hex dec #=> 291
bases = {
bin: '01',
oct: '01234567',
dec: '0123456789',
hex: '0123456789abcdef',
allow: 'abcdefghijklmnopqrstuvwxyz',
allup: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
alpha: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
alphanum: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
}
def to_int(num, src)
src_map = src.split('').map.with_index.to_h
num.reverse.each_char.with_index.sum{ |c, i| src_map[c] * (src.size ** i) }
end
def from_int(num, dst)
res = []
while num > 0
res << dst[num % dst.size]
num /= dst.size
end
res.join.reverse
end
def convert(num, src, dst)
from_int(to_int(num, src), dst)
end
if ARGV.size > 2
puts convert(ARGV[0], bases[ARGV[1].to_sym], bases[ARGV[2].to_sym])
end

Related

Count the number of zero digit in an integer number?

I wonder if there is a way to count the number of zero digit in an integer number by using only these operations: +, -, * and /
Others operations such as cast, div, mod, ... are not allowed.
Input: 16085021
Output: 2
It is a major restriction that numbers cannot be compared in a way to know that one is less than the other, and only equality checks can be done.
In my opinion that means there is nothing much else you can do than repeatedly add 1 to a variable until it hits a target (n) and derive from that what the least significant digit is of the original number n. This is of course terribly slow and not scalable, but it works in theory.
Here is a demo in Python using only ==, +, - and /:
n = 16085021
count = 0
while True:
# find out what least significant digit is
digit = 0
i = 0
while True:
if n == i:
break
digit = digit + 1
if digit == 10:
digit = 0
i = i + 1
# count any zero digit
if digit == 0:
count = count + 1
# shift that digit out of n
n = (n - digit) / 10
if n == 0:
break
print(count) # 2
Modulo can be implemented with subtractions: a % b = subtract b from a until you end up with something < b and that's the result. You say we can only use the == comparison operator, but we are only interested in modulo 10, so we can just check equality to 0, 1, ..., 9.
def modulo10WithSubtractions(a):
if a == 0 || a == 1 || ... || a == 9:
return a
while True:
a = a - b
if a == 0 || a == 1 || ... || a == 9:
return a
Integer division by 10 can be implemented in a similar fashion: a // 10 = add 10 to itself as many times as possible without exceeding a.
def div10WithAdditions(a):
if a == 0 || a == 1 || ... || a == 9:
return 0
k = 0
while True:
k += 1
if a - k*10 == 0 || a - k*10 == 1 || ... || a - k*10 == 9:
return k
Then we can basically do the classical algorithm:
count = 0
while True:
lastDigit = modulo10WithSubtractions(a)
if lastDigit == 0:
count += 1
a = div10WithAdditions(a)
if a == 0:
break
Asuuming / means integer division, then this snippet does the job.
int zeros = 0;
while(num > 0){
if(num / 10 == (num + 9) / 10){
zeros++;
}
num /= 10;
}

Ruby not incrementing number

I wrote code to find how many operations a number required under the Collatz Conjecture. However, my operations variable doesn't seem to be incrementing.
My code is:
puts "Please input a number"
number = gets.chomp
number = number.to_i
operations = 0
modulo = number % 2
while number =! 1
if modulo == 0
number = number / 2
operations = operations + 1
elsif modulo =! 0 && number =! 1
number = number * 3
number = number += 1
operations = operations + 2
else
puts "Uh oh, something went wrong."
end
end
puts "It took #{operations} operations!"
I am running this code on https://www.repl.it.
First of all, it's elsif; not elseif (I edited that in your question). And unequal sign is !=; not =!. But that has a somewhat different meaning. (i.e.: number =! 1 means number = !1)
In the 12th line, what is number = number += 1? I think you meant number += 1 or number = number + 1.
Now, the code works. :)
Here's the final version.
puts "Please input a number"
number = gets.chomp
number = number.to_i
operations = 0
modulo = number % 2
while number != 1
if modulo == 0
number = number / 2
operations = operations + 1
elsif modulo != 0 && number != 1
number = number * 3
number = number + 1
operations = operations + 2
else
puts "Uh oh, something went wrong."
end
end
puts "It took #{operations} operations!"
Usage:
Please input a number
256
It took 8 operations!
An optimal solution using functions:
def collatz(n)
if n % 2 == 0
return n / 2
else
return 3*n + 1
end
end
def chainLength(num)
count = 1
while num > 1
count += 1
num = collatz(num)
end
return count
end
puts "Please input a number"
number = gets.chomp
number = number.to_i
operations = chainLength(number)
puts "It took #{operations} operations!"
If you need more performance, read about dynamic programming and memoization techniques.

Why are these loops in Ruby not outputting the same answer?

I am currently doing Project Euler problem 1. I have no idea why these two loops are not the same.
total = 0
for i in 0..1000
if (i % 3 == 0 || i % 5 == 0)
total += i
end
end
and
total = 0
(0...1000).each do |i|
total += i if (i % 3 == 0 || i % 5 == 0)
end
puts total
When you use three dots in range (0...1000), the end value is not part of the range - it is equivalent to (0..999)
So, in first case 1000 is part of the loop, but in second case it is not.

How do I describe all integers in Ruby

I'm trying to write a basic program that spits out the English version of a number when the user inputs a numeral:
input = 44
output = fourty four
Is there a way to describe all integers?
Basically I want the execution to look something like:
number = gets.chomp
if number != (whatever the nomenclature is for integer)
puts 'Please enter a positive number'
or something to that effect.
You can do that with the numbers_and_words gem:
https://github.com/kslazarev/numbers_and_words
It supports languages other than english as well.
For example:
21.to_words
=> "twenty-one"
44.to_words
=> "forty-four"
I modified the Fixnum class and added a method in_words. What I did is I broke each number up into groups of three, so 100000 turns into [100, 000] and 123456789 turns into [123, 456, 789] or 1543 turns into [1, 453] then I went element by element and named every number in the element and added the appropriate word, like hundred and thousand. If you have any questions I am happy to explain!
class Fixnum
LOW = %w(zero one two three four five six seven
eight nine ten eleven twelve thirteen fourteen
fifteen sixteen seventeen eighteen nineteen)
TWO_DIGIT = %w(ten twenty thirty forty fifty sixty seventy eighty ninety)
BIG_NUMS = %w(hundred thousand million billion trillion)
def in_words
# Break up number into bytes with three bits each
# Then turn each byte into words
# Break into bytes
number = self.to_s.reverse
bytes = []
num_bytes = (number.length.to_f / 3.0).ceil()
num_bytes.times { |x| bytes << number[(x*3)..(x*3)+2].reverse }
#puts bytes.reverse.join(",")
# Turn bytes into words bit by bit
word = []
text = ""
bytes.each_with_index do |byte, i|
text = ""
# First bit
text = LOW[byte[0].to_i] if (byte.length == 3 && byte[0].to_i != 0) || byte.length == 1
# Add hundred if 3 bits
text += " hundred" if byte.length == 3 && byte[0].to_i != 0
# Second bit
if byte.length == 3 # Three bits
if byte[1].to_i > 1 # Second bit greater than teens
text += " " + TWO_DIGIT[byte[1].to_i + (-1)]
elsif byte[1].to_i != 0 # Second bit not zero
text += " " + LOW[byte[1..2].to_i]
end
elsif byte.length == 2 # Two bits
if byte[0].to_i > 1 # Greater than teens
text += " " + TWO_DIGIT[byte[0].to_i + (-1)]
text += " " + LOW[byte[1].to_i] if byte[1].to_i != 0
else # Less than twenty
text += LOW[byte[0..1].to_i]
end
end
# Third bit if three bytes and second bit > teens and third bit nonzero
text += " " + LOW[byte[2].to_i] if byte[1].to_i != 1 && byte[2].to_i > 0 && byte.length > 2
# Add trillion/billion/million/thousand
text += " " + BIG_NUMS[i] if i != 0 && byte.to_i != 0
word << text.strip if text.strip != ""
end
word.reverse.join(" ")
end
end
Because I modified the Fixnum object, you can call this from any Fixnum e.g. 44.in_words
EDIT: It looks like you might be trying to check input for integers. I would recommend making a function to handle that:
def check_input(i)
if !(i =~ /^[0-9]+$/)
puts "Sorry, that is an invalid input! Please try again"
i = check_input(gets.chomp)
end
i.to_i
end
I think the best way to handle that is with regex (pattern matching). Basically your function checks if the input isn't a number, then it asks for input again. If it is a number, then the function returns the number. /^[0-9]+$/ is the regex. ^ means start of the line and $ means end of the line. [0-9] matches any digit zero through nine (as the Tin Man commented, you can also use \d to represent any digit and it is equivalent), and + means match the previous thing (any digit) at least once.

I have a numeric value like 30.6355 that represents money, how to round to 2 decimal places?

I have a numeric value like 30.6355 that represents money, how to round to 2 decimal places?
You should not use double or float types when dealing with currency: they have both too many decimal places and occasional rounding errors. Money can fall through those holes and it'll be tough to track down the errors after it happens.
When dealing with money, use a fixed decimal type. In Ruby (and Java), use BigDecimal.
Ruby 1.8:
class Numeric
def round_to( places )
power = 10.0**places
(self * power).round / power
end
end
(30.6355).round_to(2)
Ruby 1.9:
(30.6355).round(2)
In 1.9, round can round to a specified number of digits.
This will round for some useful cases - not well written but it works! Feel free to edit.
def round(numberString)
numberString = numberString.to_s
decimalLocation = numberString.index(".")
numbersAfterDecimal = numberString.slice(decimalLocation+1,numberString.length-1)
numbersBeforeAndIncludingDeciaml = numberString.slice(0,decimalLocation+1)
if numbersAfterDecimal.length <= 2
return numberString.to_f
end
thingArray = numberString.split("")
thingArray.pop
prior = numbersAfterDecimal[-1].to_i
idx = numbersAfterDecimal.length-2
thingArray.reverse_each do |numStr|
if prior >= 5
numbersAfterDecimal[idx] = (numStr.to_i + 1).to_s unless (idx == 1 && numStr.to_i == 9)
prior = (numStr.to_i + 1)
else
prior = numStr.to_i
end
break if (idx == 1)
idx -= 1
end
resp = numbersBeforeAndIncludingDeciaml + numbersAfterDecimal[0..1]
resp.to_f
end
round(18.00) == 18.0
round(18.99) == 18.99
round(17.9555555555) == 17.96
round(17.944444444445) == 17.95
round(15.545) == 15.55
round(15.55) == 15.55
round(15.555) == 15.56
round(1.18) == 1.18
round(1.189) == 1.19

Resources