%w in ruby makes code not work? - ruby

Please compare the 2 codes (the first returning the correct value of false while the second returns a value of true) The only difference in the codes is the %w. Why does the %w cause this problem?
#1
def ordered_vowel_word?(word)
vowels = ["a", "e", "i", "o", "u"]
letters_arr = word.split("")
vowels_arr = letters_arr.select { |l| vowels.include?(l) }
(0...(vowels_arr.length - 1)).all? do |i|
vowels_arr[i] <= vowels_arr[i + 1]
end
end
ordered_vowel_word?("complicated")
#2
def ordered_vowel_word?(word)
vowels = %w[a, e, i, o, u]
letters_arr = word.split("")
vowels_arr = letters_arr.select { |l| vowels.include?(l) }
(0...(vowels_arr.length - 1)).all? do |i|
vowels_arr[i] <= vowels_arr[i + 1]
end
end
ordered_vowel_word?("complicated")

You should not be using it with commas. That is half the purpose of using this notation. Correctly:
%w[a e i o u]

Use this instead:
vowels = %w[a e i o u]
Commas are not needed.
irb(main):001:0> %w[a, e, i, o, u]
=> ["a,", "e,", "i,", "o,", "u"]
irb(main):002:0> %w[a e i o u]
=> ["a", "e", "i", "o", "u"]
As you can see, %w will treat its contents as a string and split by space.

Related

Decompose words into letters with Ruby

In my language there are composite or compound letters, which consists of more than one character, eg "ty", "ny" and even "tty" and "nny". I would like to write a Ruby method (spell) which tokenize words into letters, according to this alphabet:
abc=[*%w{tty ccs lly ggy ssz nny dzs zzs sz zs cs gy ny dz ty ly q w r t z p l k j h g f d s x c v b n m y}.map{|z| [z,"c"]},*"eéuioöüóőúűáía".split(//).map{|z| [z,"v"]}].to_h
The resulting hash keys shows the existing letters / composite letters of the alphabet and also shows which letter is a consonant ("c") and which one is a vowel ("v"), becase later I would like to use this hash to decompose words into syllables. Cases of compound words when accidentally composite letters are formed at the words common boundary shoudn't be resolved by the method of course.
Examples:
spell("csobolyó") => [ "cs", "o", "b", "o", "ly", "ó" ]
spell("nyirettyű") => [ "ny", "i", "r", "e", "tty", "ű" ]
spell("dzsesszmuzsikus") => [ "dzs", "e", "ssz", "m", "u", "zs", "i", "k", "u", "s" ]
You might be able to get started looking at String#scan, which appears to be giving decent results for your examples:
"csobolyó".scan(Regexp.union(abc.keys))
# => ["cs", "o", "b", "o", "ly", "ó"]
"nyirettyű".scan(Regexp.union(abc.keys))
# => ["ny", "i", "r", "e", "tty", "ű"]
"dzsesszmuzsikus".scan(Regexp.union(abc.keys))
# => ["dzs", "e", "ssz", "m", "u", "zs", "i", "k", "u", "s"]
The last case doesn't match your expected output, but it matches your statement in the comments
I sorted the letters in the alphabet: if a letter appears earlier, then it should be recognized instead of its simple letters. When a word contains "dzs" it should be considered to "dzs" and not to "d" and "zs"
I didn't use the preference in which you sorted, rather I used higher character word will have higher preference than lower character word.
def spell word
abc=[*%w{tty ccs lly ggy ssz nny dzs zzs sz zs cs gy ny dz ty ly q w r t z p l k j h g f d s x c v b n m y}.map{|z| [z,"c"]},*"eéuioöüóőúűáía".split(//).map{|z| [z,"v"]}].to_h
current_position = 0
maximum_current_position = 2
maximum_possible_position = word.length
split_word = []
while current_position < maximum_possible_position do
current_word = set_current_word word, current_position, maximum_current_position
if abc[current_word] != nil
current_position, maximum_current_position = update_current_position_and_max_current_position current_position, maximum_current_position
split_word.push(current_word)
else
maximum_current_position = update_max_current_position maximum_current_position
current_word = set_current_word word, current_position, maximum_current_position
if abc[current_word] != nil
current_position, maximum_current_position = update_current_position_and_max_current_position current_position, maximum_current_position
split_word.push(current_word)
else
maximum_current_position = update_max_current_position maximum_current_position
current_word = set_current_word word, current_position, maximum_current_position
if abc[current_word] != nil
current_position, maximum_current_position = update_current_position_and_max_current_position current_position, maximum_current_position
split_word.push(current_word)
else
puts 'This word cannot be formed in the current language'
break
end
end
end
end
split_word
end
def update_max_current_position max_current_position
max_current_position = max_current_position - 1
end
def update_current_position_and_max_current_position current_position,max_current_position
current_position = max_current_position + 1
max_current_position = current_position + 2
return current_position, max_current_position
end
def set_current_word word, current_position, max_current_position
word[current_position..max_current_position]
end
puts "csobolyó => #{spell("csobolyó")}"
puts "nyirettyű => #{spell("nyirettyű")}"
puts "dzsesszmuzsikus => #{spell("dzsesszmuzsikus")}"
Output
csobolyó => ["cs", "o", "b", "o", "ly", "ó"]
nyirettyű => ["ny", "i", "r", "e", "tty", "ű"]
dzsesszmuzsikus => ["dzs", "e", "ssz", "m", "u", "zs", "i", "k", "u", "s"]
Meanwhile I managed to write a method which works, but 5x slower than String#scan:
abc=[*%w{tty ccs lly ggy ssz nny dzs zzs sz zs cs gy ny dz ty ly q w r t z p l k j h g f d s x c v b n m y}.map{|z| [z,"c"]},*"eéuioöüóőúűáía".split(//).map{|z| [z,"v"]}].to_h
def spell(w,abc)
s=w.split(//)
p=""
t=[]
for i in 0..s.size-1 do
p << s[i]
if i>=s.size-2 then
if abc[p]!=nil then
t.push p
p=""
elsif abc[p[0..-2]]!=nil then
t.push p[0..-2]
p=p[-1]
elsif abc[p[0]]!=nil then
t.push p[0]
p=p[1..-1]
end
elsif p.size==3 then
if abc[p]!=nil then
t.push p
p=""
elsif abc[p[0..-2]]!=nil then
t.push p[0..-2]
p=p[-1]
elsif abc[p[0]]!=nil then
t.push p[0]
p=p[1..-1]
end
end
end
if p.size>0 then
if abc[p]!=nil then
t.push p
p=""
elsif abc[p[0..-2]]!=nil then
t.push p[0..-2]
p=p[-1]
end
end
if p.size>0 then
t.push p
end
return t
end

Ruby - converting a string into hash with each character as key and index as value?

I am trying to transform a given string into a hash with each its character = key and index = value.
For example, if I have str = "hello", I would like it to transform into {"h"=>0, "e"=>1, "l"=>2, "l"=>3, "o"=>4}.
I created a method as such:
def map_indices(arr)
arr.map.with_index {|el, index| [el, index]}.to_h
end
#=> map_indices('hello'.split(''))
#=> {"h"=>0, "e"=>1, "l"=>3, "o"=>4}
The problem is it skips the first l. If I reverse the order of el and index: arr.map.with_index {|el, index| [index, el]}.to_h, I get all the letters spelled out: {0=>"h", 1=>"e", 2=>"l", 3=>"l", 4=>"o"}
But when I invert it, I get the same hash that skips one of the l's.
map_indices('hello'.split('')).invert
#=> {"h"=>0, "e"=>1, "l"=>3, "o"=>4}
Why is this behaving like such? How can I get it to print {"h"=>0, "e"=>1, "l"=>2, "l"=>3, "o"=>4}?
It can be done, but will confuse other Ruby programmers.A normal hash treats a key "a" as identical to another "a". Unless a little known feature .compare_by_identity is used:
h = {}.compare_by_identity
"hello".chars.each_with_index{|c,i| h[c] = i}
p h # => {"h"=>0, "e"=>1, "l"=>2, "l"=>3, "o"=>4}
Any of the following could be used. For
str = "hello"
all return
{"h"=>[0], "e"=>[1], "l"=>[2, 3], "o"=>[4]}
str.each_char
.with_index
.with_object({}) { |(c,i),h| (h[c] ||= []) << i }
See String#each_char, Enumerator#with_index and Enumerator#with_object. The block variables have been written to exploit array decomposition.
str.each_char
.with_index
.with_object(Hash.new { |h,k| h[k] = [] }) { |(c,i),h| h[c] << i }
See the form of Hash::new that takes a block and no argument. If a hash has been defined
h = Hash.new { |h,k| h[k] = [] }
and later
h[c] << i
is executed, h[c] is first set equal to an empty array if h does not have a key c.
str.size
.times
.with_object(Hash.new { |h,k| h[k] = [] }) { |i,h| h[str[i]] << i }
str.each_char
.with_index
.group_by(&:first)
.transform_values { |a| a.flat_map(&:last) }
See Enumerable#group_by, Hash#transform_values (introduced in Ruby v2.5) and Enumerable#flat_map.
Note that
str.each_char
.with_index
.group_by(&:first)
#=> {"h"=>[["h", 0]], "e"=>[["e", 1]], "l"=>[["l", 2], ["l", 3]],
# "o"=>[["o", 4]]}
Another option you can use is zipping two enumerations together.
s = "hello"
s.chars.zip(0..s.size)
This yields: [["h", 0], ["e", 1], ["l", 2], ["l", 3], ["o", 4]]
I am new to Ruby and I am sure this can be refactored, but another alternative might be:
arr1 = "Hello".split(%r{\s*})
arr2 = []
for i in 0..arr1.size - 1
arr2 << i
end
o = arr1.zip(arr2)
a_h = []
o.each do |i|
a_h << Hash[*i]
end
p a_h.each_with_object({}) { |k, v| k.each { |kk,vv| (v[kk] ||= []) << vv } }
=> {"H"=>[0], "e"=>[1], "l"=>[2, 3], "o"=>[4]}

How to join nested char array to string

I have the following code:
def caesar_cipher(text, move_by)
move_by %= 26
chars = Hash[('a'..'z').map.with_index.to_a]
converted = text.split.map do |word|
word.chars.map do |char|
if (chars[char.downcase] + move_by) <= 26
chars.key(chars[char.downcase] + move_by)
else
chars.key(chars[char.downcase] + move_by - 26)
end
end
end
end
print caesar_cipher("What a string", 5)
It converts string from variable text to integer. Here is the output I get when I run it: [["b", "m", "f", "y"], ["f"], ["x", "y", "w", "n", "s", "l"]], and I'd like it to be joined like this"bmft f xywnsl". I've tried .join method, but it gives me "bmftfxywnsl"
If:
arr = [["b", "m", "f", "y"], ["f"], ["x", "y", "w", "n", "s", "l"]]
then
arr.map(&:join).join(' ')
#=> "bmfy f xywnsl"
You can think of map(&:join) as:
arr.map { |a| a.join }.join(' ')
Isn't Ruby great?

How to convert array with ranges in to alone array in ruby

I have some array
>> a = ["a..c", "0..2"]
=> ["a..c", "0..2"]
I need convert this array to another array
>> b = ("a".."c").to_a + (0..2).to_a
=> ["a", "b", "c", 0, 1, 2]
How I can do it?
a.flat_map do |string_range|
from, to = string_range.split("..", 2)
(from =~ /^\d+$/ ? (from.to_i..to.to_i) : (from..to)).to_a
end
#=> => ["a", "b", "c", 0, 1, 2]
what about this?
a = ["a..c", "0..2"]
b = a.map { |e| Range.new( *(e).split('..') ).to_a }.flatten
no flat_map used so it works the same on all versions
as #steenslag correctly mentioned, this version does not convert to integers.
here is a version that does:
b = a.map do |e|
Range.new( *(e).split('..').map{ |c| c =~ /\A\d+\Z/ ? c.to_i : c } ).to_a
end.flatten
see it in action here
a = ["a..c", "0..2"]
b = a.flat_map{|str| Range.new(*str.split('..')).to_a} # => ["a", "b", "c", "0", "1", "2"]
p b.map!{|v| Integer(v) rescue v} # => ["a", "b", "c", 0, 1, 2]

Array method that accepts an index and returns a new array with the item at the index removed

'delete_at' and 'slice' remove the item at the index and return that item. But I don't really care about the removed item. I just want a new array with that item removed. Ruby's Array class doesn't seem to provide such a method.
Example would be:
a = ['a','b','c','d']
b = a.remove(2) #b => ['a','b','d']
Here 'remove' is a fictitious method that does what I want. I need the original array, so I want a new array return. I wonder if Ruby already has some built-in like this?
class Array
def remove(idx)
self[0...idx] + self[idx+1..-1]
end
end
a = ['a','b','c','d']
a.reject {|i| i == a[2] }
#=> ["a", "b", "d"]
irb(main):001:0> a = %w[ a b c d ]
#=> ["a", "b", "c", "d"]
irb(main):002:0> a.reject.with_index{ |o,i| i==2 }
#=> ["a", "b", "d"]
irb(main):003:0> a
#=> ["a", "b", "c", "d"]
Requires Ruby 1.9
With some monkey-patching:
irb(main):013:0> class Array
irb(main):014:1> def remove_at(i)
irb(main):015:2> self.dup.tap{ |clone| clone.delete_at(i) }
irb(main):016:2> end
irb(main):017:1> end
#=> nil
irb(main):018:0> a.remove_at(2)
#=> ["a", "b", "d"]
irb(main):019:0> a
#=> ["a", "b", "c", "d"]
it's quite hacky:
a = ['a','b','c','d']
b, = [a, a.delete_at(0)] # => ['b','c','d']
but it's faster(on my eeepc)
require 'benchmark'
n = 5000
Benchmark.bm do |x|
a = (1..5000).to_a
b = nil
x.report { n.times do; b, = [a, a.delete_at(0)]; end }
a = (1..5000).to_a
b = nil
x.report { n.times do; b = a.reject.with_index{ |o,i| i == 0 }; end }
end
user system total real
0.032000 0.000000 0.032000 ( 0.034002)
21.808000 0.156000 21.964000 ( 22.696298) OMG!

Resources