Converting int to escaped octal character - ruby

Given an integer - say, x=20, I'd like to convert this into a string containing its escaped octal character. I.e. ...
x=20
# y = ... Some magic
p y # => "\024"
The closest I've managed to get is by using:
x.to_s(8) # => "24"
However, I'm completely stumpted on how to convert this string into an escaped octal character! Any ideas, internet?

Just use Kernel#sprintf to format the number.
Like this
x = 20
y = sprintf('\%03o', x)
puts y
output
\024
Update
Maybe I misunderstood you. If you just want a character with the given code point then just use Integer#chr.
Like this
x = 20
y = x.chr
p y
output
"\x14"

You could use Array#pack:
[20].pack("C") #=> "\x14"

x = 20
y = '\%03o' % x
puts y
If you use p to display y you would see 2 backslashes as p outputs ruby parsable strings.

Related

Don't understand leetcode question about isomorphic strings

In the leetcode problem, isomorphic strings the input outputs to false.
Here is the question...
Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: true
Example 2:
Input: s = "foo", t = "bar"
Output: false
Example 3:
Input: s = "paper", t = "title"
Output: true
"bbbaaaba"
"aaabbbba"
This is not clear to me because if we follow the instructions I believe we should have true as the answer.
My understanding to the problem is that any character from s, x, can only be replaced by a character from b, y, if y is said to replace x.
First off, strings s and t must be of the same length, otherwise return false
We know that x is replaced by ya if x has not been replaced by any other character yb where ya != yb
We also know that from the instructions we can freely replace any character with the same character from both strings.
The replacements must occur while preserving the order of characters
With the example I gave above a walkthrough my solution would be as follows...
s = "bbbaaaba"
t = "aaabbbba"
for index = 0, replace b with a, so b -> a
for index = 1, replace b with a, so b -> a # we can do this because a has already
replaced b
for index = 2, replace b with a, so b -> a # same reasoning as above
for index = 3, replace a with b, so a -> b # we have not replaced any characters from
s with the character b, so this is allowed
for index = 4, replace a with b, so a -> b
for index = 5, replace a with b, so a -> b
for index = 6, replace b with b, # this is allowed because both characters
from each string s, and t match at the
current index
for index = 7, replace a with a # same reasoning as before
After finishing we should return true successfully, how come the answer is suppose to be false?
All occurrences of a character must be replaced with another character.
If you choose to replace b with a, all b in the original string need to be changed to a. Therefore, the string would be aaabbbab instead of the desired answer. The answer is therefore false.

Does SWI-Prolog have a way to read scientific notation?

Does SWI-Prolog have a way to read scientific notation? I couldn't find anything here or in the document. For example, is there a way to read 7.33E-05 besides hard-parsing it?
Thank you!
One option to read numbers like 7.33E-05 is by using "read_term" predicate family, e.g.:
read_term_from_atom('7.33E-05', N, [])
should parse and save 7.33E-05 into variable N.
Here's an example use of the scientific notation:
?- X = 7.33E-05, Y = 6.4E-03, Z = X+Y, Y > X.
X = 7.33e-5,
Y = 0.0064,
Z = 7.33e-5+0.0064.
To actually do simple math use:
?- use_module(library(clpr)).
?- X = 7.33E-05, Y = 6.4E-03, {Z=X+Y}.
X = 7.33e-5,
Y = 0.0064,
Z = 0.0064733

NiceLabel VBScript - Adding Variables

x and y are always numeric.
x, y, and Quantity are always "1" by default if the user does not change values.
I've set y = 4.
When running the code below, I receive error:
Variable "Quantity" has format Numeric. Value "4+1-1" is invalid for this format"
Dim x, y, z, result
x = EndingLabel.Value
y = BarcodedNumber.Value
z = x & "+" & 1 & "-" & y
result = z
If (z > y) Then
Quantity.Value = result
Else
End If
I'm not certain if the problem is my code or the program I'm writing it in, but it doesn't appear to be calculating the actual equation "4+1-1". What am I doing wrong?
You are assuming that "4+1-1" isn't being seen as a string which it is. I'd suggest putting an "Eval" around it so that it is taken in that form. Change the assignment of z to this:
z = eval(x & "+" & 1 & "-" & y)
If you want another way to consider this. Think of a 2 that in code could be the digit 2, an ASCII character of the digit 2 or something else and thus interpretation is a key point here.

Ruby: Print un-printable data string

If I have a variable, x, that will sometime be a normal printable string, and sometimes be some random hex data (including unprintable chars), how can I reliably print that will space padding? ex:
def print(x)
puts("%-15s" % x)
end
x = "test"
print(x)
x = Array.new(256) { rand(256) }.pack('c*')
print(x)
def print(x)
puts "%-15s" % [x.inspect]
end
And if you want to get rid of the "...":
puts "%-15s" % [x.inspect[1..-2]]

Ruby - newlines and operators

Consider the following code:
x = 4
y = 5
z = (y + x)
puts z
As you'd expect, the output is 9. If you introduce a newline:
x = 4
y = 5
z = y
+ x
puts z
Then it outputs 5. This makes sense, because it's interpreted as two separate statements (z = y and +x).
However, I don't understand how it works when you have a newline within parentheses:
x = 4
y = 5
z = (y
+ x)
puts z
The output is 4. Why?
(Disclaimer: I'm not a Ruby programmer at all. This is just a wild guess.)
With parens, you get z being assigned the value of
y
+x
Which evaluates to the value of the last statement executed.
End the line with \ in order to continue the expression on the next line. This gives the proper output:
x = 4
y = 5
z = (y \
+ x)
puts z
outputs 9
I don't know why the result is unexpected without escaping the newline. I just learned never to do that.
Well you won't need the escaping character \ if your lines finishes with the operator
a = 4
b = 5
z = a +
b
puts z
# => 9

Resources