`to_i` method with base value as parameter in ruby - ruby

Can anyone explain how base parameter works when calling to_i with the following examples?
'2'.to_i(2) #=> 0
'3'.to_i(2) #=> 0
'12'.to_i(2) #=> 1
'122'.to_i(2) #=> 1
'20'.to_i(2) #=> 0
'21'.to_i(2) #=> 0
I do not understand how it's actually working. Can anyone explain please?

It is the same reason that '54thousand'.to_i is 54: to_i reads until it finds end of string or an invalid digit.
In binary (base 2), the only valid digits are 0 and 1. Thus, because 2 is invalid, '122'.to_i(2) is identical to '1'.to_i(2). Also, '2'.to_i(2) is identical to ''.to_i(2), which is rather intuitively 0.

base, in other word Radix means the number of unique digits in a numeral system.
In Decimal, we have 0 to 9, 10 digits to represent numbers.
You are using 2 as parameter, that means Binary, so there're only 0 and 1 working.
From the Doc of to_i:
Returns the result of interpreting leading characters in str as an
integer base base (between 2 and 36). Extraneous characters past the
end of a valid number are ignored. If there is not a valid number at
the start of str, 0 is returned. This method never raises an
exception when base is valid.
You can use these number representations directly in Ruby:
num_hex = 0x100
#=> 256
num_bin = 0b100
#=> 4
num_oct = 0o100
#=> 64
num_dec = 0d100
#=> 100

Related

Returning the highest and lowest numbers in a string: Ruby

Not sure what I'm doing incorrect but I seem to be getting it woefully wrong.
The question is, you are given a string of space separated numbers, and have to return the highest and lowest number.
Note:
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
def high_and_low(numbers)
# numbers contains a string of space seperated numbers
#return the highest and lowest number
numbers.minmax { |a, b| a.length <=> b.length }
end
Output:
`high_and_low': undefined method `minmax' for "4 5 29 54 4 0 -214 542 -64 1 -3 6 -6":String
minmax is not implemented for a string. You need to split your string into an array first. But note that split will return an array of strings, not numbers, you will need to translate the strings to integers (to_i) in the next step.
Because minmax returns the values in the opposite order than required, you need to rotate the array with reverse and then just join those numbers with whitespace for the final result.
numbers = "4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"
def high_and_low(numbers)
numbers.split.minmax_by(&:to_i).reverse.join(' ')
end
high_and_low(numbers)
#=> "542 -214"
How about:
numbers_array = numbers.split(' ')
"#{numbers_array.max} #{numbers_array.min}"
If you're starting with a string of numbers you may have to cast the .to_i after the call to split.
In that case:
numbers_array = numbers.split(' ').map { |n| n.to_i }
"#{numbers_array.max} #{numbers_array.min}"
As you're starting with a String, you must turn it into an Array to cast minmax on it.
Also, make sure to compare Integers by casting .map(&:to_i) on the Array; otherwise you'd compare the code-point instead of the numerical value.
def get_maxmin(string)
string.split(' ')
.map(&:to_i)
.minmax
.reverse
.join(' ')
end
There is no need to convert the string to an array.
def high_and_low(str)
str.gsub(/-?\d+/).
reduce([-Float::INFINITY, Float::INFINITY]) do |(mx,mn),s|
n = s.to_i
[[mx,n].max, [mn,n].min]
end
end
high_and_low "4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"
#=> [542, -214]
Demo
This uses the form of String#gsub that has one argument and no block, so it returns an enumerator that I've chained to Enumerable#reduce (a.k.a. inject). gsub therefore merely generates matches of the regular expression /-?\d+/ and performs no substitutions.
My solution to this kata
def high_and_low(numbers)
numbers.split.map(&:to_i).minmax.reverse.join(' ')
end
Test.assert_equals(high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"), "542 -214")
#Test Passed: Value == "542 -214"
Some docs about methods:
String#split Array#map Array#minmax Array#reverse Array#join
More about Symbol#to_proc
numbers.split.map(&:to_i) is same as number.split.map { |p| p.to_i }
But "minmax_by(&:to_i)" looks better, for sure I guess.

Why does `"0a".to_i(16)` return `10`?

I'm confused about the optional argument for to_i.
Specifically, what "base" means, and how it impacts the method in this example:
"0a".to_i(16) #=> 10
I have trouble with the optional argument in regards to the string the method is called on. I thought that the return value would just be an integer value of 0.
Simple answer: It's because 0a or a in Hexadecimal is equal to 10 in Decimal.
And base, in other word Radix means the number of unique digits in a numeral system.
In Decimal, we have 0 to 9, 10 digits to represent numbers.
In Hexadecimal, there're 16 digits instead, apart from 0 to 9, we use a to f to represent the conceptual numbers of 10 to 15.
You can test it like this:
"a".to_i(16)
#=> 10
"b".to_i(16)
#=> 11
"f".to_i(16)
#=> 15
"g".to_i(16)
#=> 0 # Because it's not a correct hexadecimal digit/number.
'2c'.to_i(16)
#=> 44
'2CH2'.to_i(16)
#=> 44 # Extraneous characters past the end of a valid number are ignored, and it's case insensitive.
9.to_s.to_i(16)
#=> 9
10.to_s.to_i(16)
#=> 16
In other words, 10 in Decimal is equal to a in Hexadecimal.
And 10 in Hexadecimal is equal to 16 in Decimal. (Doc for to_i)
Note that usually we use 0x precede to Hexadecimal numbers:
"0xa".to_i(16)
#=> 10
"0x100".to_i(16)
#=> 256
Btw, you can just use these representations in Ruby:
num_hex = 0x100
#=> 256
num_bin = 0b100
#=> 4
num_oct = 0o100
#=> 64
num_dec = 0d100
#=> 100
Hexadecimal, binary, octonary, decimal (this one, 0d is superfluous of course, just use in some cases for clarification.)

Why does Ruby return for `str[-1..1]` what it does?

Suppose we have a string str. If str contains only one character, for example, str = "1", then str[-1..1] returns 1.
But if the size (length) of str is longer than one, like str = "anything else", then str[-1..1] returns "" (empty string).
Why does Ruby interpret string slicing like this?
This behaviour is just how ranges of characters work.
The range start is -1, which is the last character in the string. The range end is 1, which is the second position from the start.
So for a one character string, this is equivalent to 0..1, which is that single character.
For a two character string, this is 1..1, which is the second character.
For a three character string, this is 2..1, which is an empty string. And so on for longer strings.
To get a non-trivial substring, the start position has to represent a position earlier than the end position.
For a single-length string, index -1 is the same as index 0, which is smaller than 1. Thus, [-1..1] gives a non-trivial substring.
For a string longer than a single character, index -1 is larger than index 0. Thus, [-1..1] cannot give a non-trivial substring, and by default, it returns an empty string.
Writing down the indices usually helps me:
# 0 1 2 3 4 5 6 7 8 9 10 11 12
str = 'a' 'n' 'y' 't' 'h' 'i' 'n' 'g' ' ' 'e' 'l' 's' 'e' #=> "anything else"
# -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
You can refer to each character by either its positive or negative index. For example, you can use either 3 or -10 to refer to "t":
str[3] #=> "t"
str[-10] #=> "t"
and either 7 or -6 to refer to "g":
str[7] #=> "g"
str[-6] #=> "g"
Likewise, you can use each of these indices to retrieve "thing" via a range:
str[3..7] #=> "thing"
str[3..-6] #=> "thing"
str[-10..7] #=> "thing"
str[-10..-6] #=> "thing"
str[-1..1] however would return an empty string, because -1 refers to the last character and 1 refers to the second. It would be equivalent to str[12..1].
But if the string consists of a single character, that range becomes valid:
# 0
str = '1'
# -1
str[-1..1] #=> "1"
In fact, 1 refers to an index after the first character, so 0 would be enough:
str[-1..0] #=> "1"

Understanding this snippet 'D' * (num % 1000 / 500)

Can anyone explain how to read this code and what it will do?
'D' * (num % 1000 / 500)
It is from a method for converting integers to Roman numerals. I don't understand how it functions.
It is pretty obfuscated indeed. I guess the idea was to put one or zero Ds depending on if you get a number greater than 500 after you get the remainder of division by 1000.
The order of the operations:
num % 1000
num modulo 1000. Will leave the last three digits.
/ 500
Will see if the last three digits are greater than 500.
String#* repeats a string:
'x' * 5 # => "xxxxx"
The reason that is needed is because D is the letter for 500. You will have only one or zero of these as M is the letter for 1000.
The expression (num % 1000 / 500) means "if you have in your last 3 digits a number greater than 500 then evaluate to 1 otherwise evaluate to 0"
"D" * (0 or 1) is determining whether to put "D" on the roman number or not.
What it Does
The expression is a way of building the Roman numeral five-hundreds digit, which is 'D'.
It takes any number, extracts only the three rightmost digits (values 0 through 999), and returns a 'D' only if the value is 500 or greater. Otherwise it returns an empty string ''
How to Read it
In Ruby, the multiply *, divide /, and modulus % symbols have equal precedence and are processed in order from left to right. Parentheses, however, have a higher precedence than these three operators.
To help visualize the processing order, you can add optional parentheses:
'D' * ( ( num % 1000 ) / 500 )
num % 1000:
extracts the three rightmost digits of a number, resulting in values 0 - 999
{0-999} / 500:
determines if value is 500 or greater, or not.
Returns 1 if so, 0 if not.
In Ruby, integer division does not automatically convert to decimals.
'D' * {1 or 0}:
In Ruby, multiplying a string by 1 returns the string, multiplying by 0 returns an empty string
Examples
For a number 35,045:
35045 % 1000 #=> 45
45 / 500 #=> 0
'D' * 0 #=> ""
For a number 468,987:
468987 % 1000 #=> 987
987 / 500 #=> 1
'D' * 1 #=> "D"
For a number 670:
670 % 1000 #=> 670
670 / 500 #=> 1
'D' * 1 #=> "D"
For a number 7:
7 % 1000 #=> 7
7 / 500 #=> 0
'D' * 0 #=> ""
See this page and scroll down to Ruby Operators Precedence.
Multiplication, division, and modular arithmetic are all together, so precedence is left to right IIRC.
First, num % 1000 is evaluated. Then, that is divided by 500. That's then multiplied by 'D'.
The modulus % and division / operators have the same precedence.
So associativity, which is from left to right for these operators, comes into play.
Therefore the expression is equivalent to 'D' * ((num % 1000) / 500): you are multiplying 'D' by the last 3 digits of num divided by 500.

Ruby's String#hex confusion

I've found it weird that String#hex in Ruby doesn't return the right hex value for a given char. I might be misunderstanding the method, but take the following example:
'a'.hex
=> 10
Whereas the right hex value for 'a' would be 61:
'a'.unpack('H*')
=> 61
Am I missing something? What's hex for? Any hints appreciated!
Thanks
String#hex doesn't give you the ASCII index of a character, it's for transforming a base-16 number (hexadecimal) from a string to an integer:
% ri String\#hex
String#hex
(from ruby site)
------------------------------------------------------------------------------
str.hex -> integer
------------------------------------------------------------------------------
Treats leading characters from str as a string of hexadecimal digits
(with an optional sign and an optional 0x) and returns the
corresponding number. Zero is returned on error.
"0x0a".hex #=> 10
"-1234".hex #=> -4660
"0".hex #=> 0
"wombat".hex #=> 0
So it uses the normal mapping:
'0'.hex #=> 0
'1'.hex #=> 1
...
'9'.hex #=> 9
'a'.hex #=> 10 == 0xA
'b'.hex #=> 11
...
'f'.hex #=> 15 == 0xF == 0x0F
'10'.hex #=> 16 == 0x10
'11'.hex #=> 17 == 0x11
...
'ff'.hex #=> 255 == 0xFF
It's very similar to String#to_i when using base 16:
'0xff'.to_i(16) #=> 255
'FF'.to_i(16) #=> 255
'-FF'.to_i(16) #=> -255
From the docs:
% ri String\#to_i
String#to_i
(from ruby site)
------------------------------------------------------------------------------
str.to_i(base=10) -> integer
------------------------------------------------------------------------------
Returns the result of interpreting leading characters in str as an
integer base base (between 2 and 36). Extraneous characters past the
end of a valid number are ignored. If there is not a valid number at the start
of str, 0 is returned. This method never raises an exception
when base is valid.
"12345".to_i #=> 12345
"99 red balloons".to_i #=> 99
"0a".to_i #=> 0
"0a".to_i(16) #=> 10
"hello".to_i #=> 0
"1100101".to_i(2) #=> 101
"1100101".to_i(8) #=> 294977
"1100101".to_i(10) #=> 1100101
"1100101".to_i(16) #=> 17826049
One more advantage over hex method. '10-0' to 256.
Consider you want to compare'100' > '20'. Should return true but return false. Use '100'.hex >'20'.hex. Returns true. Which is more accurate.

Resources