Given the following ruby array:
["2XL", "3XL", "4XL", "5XL", "6XL", "L", "M", "S", "XL"]
How do I sort it so that it is in this order?
["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]
Note that every size is not always present.
For history's sake, this was my original implementation.
sorted_sizes = []
sorted_sizes << "S" if sizes.include?("S")
sorted_sizes << "M" if sizes.include?("M")
sorted_sizes << "L" if sizes.include?("L")
sorted_sizes << "XL" if sizes.include?("XL")
sorted_sizes << "2XL" if sizes.include?("2XL")
sorted_sizes << "3XL" if sizes.include?("3XL")
sorted_sizes << "4XL" if sizes.include?("4XL")
sorted_sizes << "5XL" if sizes.include?("5XL")
sorted_sizes << "6XL" if sizes.include?("6XL")
sorted_sizes
["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"] & ["2XL", "3XL", "4XL", "5XL", "6XL", "L", "M", "S", "XL"]
# => ["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]
Here's a way to do it that can handle repeats:
SORT_ORDER = ["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]
["2XL", "3XL", "4XL", "5XL",
"6XL", "L", "M", "S", "XL"].sort_by { |x| SORT_ORDER.index(x) }
I really like #nicooga's version to this problem and would just suggest to wrap the logic in a lambda. That way it can be used in many places throughout the code.
clothing_size = ->x{%w(S M L XL 2XL 3XL 4XL 5XL 6XL).index(x)}
size_list = ["2XL", "3XL", "4XL", "5XL", "6XL", "L", "M", "S", "XL"]
size_list.sort_by &clothing_size
array = ["2XL", "3XL", "4XL", "6XL", "L", "M", "S", "XL"]
sort_order = ["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]
sort_order - (sort_order - array)
# => ["S", "M", "L", "XL", "2XL", "3XL", "4XL", "6XL"]
Related
I could not figure out the functionality of the below Ruby program. I know we are opening xxx.csv & creating 9 columns, but I don't understand the following:
out <<, do |out|, (1..10).map{|i| "r" + i.to_s} (1..10 rep a set of 1 to 10 ???), map method and its argument, and flatten.join("\t") + "\n".
File.open("xxxx.csv", "w") do |out|
out << [
'x', 'y', 'l', 'b',
'r', 'v', (1..10).map{|i| "r" + i.to_s},
'xxx', 'd'
].flatten.join("\t") + "\n"
Example of the data: 13/#137/2011/0/15/5.8/5/4.....2..4/PG
I'm learning Ruby and programming in general and apologize if my question is a basic one.
Thanks
Amy
See also Export content of a SQLite3 table in CSV to get an answer on your problem.
But to explain the code. Lets try it in parts:
p (1..10).map{|i| "r" + i.to_s}
result:
["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"]
Now lets do it in details:
(1..10) is a Range object. If you execute
(1..10).to_a
you get get an array from 1 to 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
map loops on the objects of the range and and creates a new array with the result of the given block. So (1..10).map{|i| "r" + i.to_s} loops on all values of the range 1 to 10, gives the value to the block internal variable i and builds a new String with r and (+) the string representation of the number i (to_s means to string).
In total you get an array like this:
["x", "y", "l", "b", "r", "v",
["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"],
"xxx", "d"
]
flatten makes array flat. If there is an array in an array, then flatten resolve the inner array(s) and makes it 'flat'
Example:
[1,[2,3]].flatten #[1, 2, 3]
join takes all entries of the array and concatenates them into a string. The (optional) parameter defines the content between two elements. So in your code you get all entries as a csv-line with tab-separartor.
And in the end a newline is added.
In steps:
["x", "y", "l", "b", "r", "v",
["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"],
"xxx", "d"
].flatten
results in
["x", "y", "l", "b", "r", "v",
"r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", #No inner array any longer
"xxx", "d"
]
Then the join is done:
["x", "y", "l", "b", "r", "v",
"r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
"xxx", "d"
].join("\t")
result:
"x\ty\tl\tb\tr\tv\tr1\tr2\tr3\tr4\tr5\tr6\tr7\tr8\tr9\tr10\txxx\td"
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?
Not sure if that's the term I should use for it but what I'm trying to do is add len amount of characters to an array, then output that to a .txt. I have the generation done to my satisfaction but I'm not sure how to pack the strings into an array. Right now it just spits out all the strings into the console because of the puts statement, just to make sure it works.
#Password list generator by Nightc||ed, ©2015
norm = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
caps = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
puts "How many passwords would you like to generate?"
num = gets.to_i
system "cls"
puts "Password length (1-x):"
len = gets.to_i
system "cls"
puts """
Which characters would you like to use?
[1] a-z, 0-9
[2] A-Z, 0-9
[3] a-z, A-Z, 0-9
"""
arr = []
type = gets.chomp
if type == "1"
arr = [norm,nums]
elsif type == "2"
arr = [caps,nums]
elsif type == "3"
arr = [norm,caps,nums]
else
exit
end
num.times do |pass|
len.times do |char|
arr2 = arr.to_a.sample
char = arr2.to_a.sample
puts char
end
end
sleep
here your code simplified
#Password list generator by Nightc||ed, ©2015
norm = [*"a".."z"]
caps = [*"A".."Z"]
nums = [*0..9]
num, len, type = [
"How many passwords would you like to generate?",
"Password length (1-x):",
"Which characters would you like to use?
[1] a-z, 0-9
[2] A-Z, 0-9
[3] a-z, A-Z, 0-9"].map do |msg|
puts msg
gets.to_i
end
arr = case type
when 1 then
norm + nums
when 2 then
caps + nums
when 3 then
norm + caps + nums
else
exit
end
passwords = num.times.map { arr.sample(len).join }
puts passwords.inspect
sleep
I think you can simplify your life by replacing the if... and below with the following:
case type
when "1"
arr = [norm,nums].flatten
when "2"
arr = [caps,nums].flatten
when "3"
arr = [norm,caps,nums].flatten
else
exit
end
passwd_set = []
num.times { passwd_set << arr.sample(len).join }
p passwd_set
I find case statements easier to read, and more easily extended. Flattening the arrays makes it so sample can directly produce the desired number of characters/symbols, and those can be joined to produce a string which can be appended to your passwd_set array.
You can add to an array using the << method. For example:
arr = []
3.times do |el|
arr << el
end
arr.inspect #=> [0, 1, 2]
Another option would be the push method:
arr = []
(0..2).each { |el| arr.push(el)}
I have this question.
Using the Ruby language, have the function CaesarCipher(str,num) take the str parameter and perform a Caesar Cipher shift on it using the num parameter as the shifting number. A Caesar Cipher works by shifting each letter in the string N places down in the alphabet (in this case N will be num). Punctuation, spaces, and capitalization should remain intact. For example if the string is "Caesar Cipher" and num is 2 the output should be "Ecguct Ekrjgt".
Any my code looks like this. I think the onlt problem i have is to update each letter and then each word within the loops. please help. thank you
def Caesar_cipher(str, num)
if num > 25
num -= 26
end
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
str = str.split(" ")
str.each do |word|
word.each_char do |c|
if alphabet.include?(c)
n = alphabet.index(c) + num
if n > 25
n -= 26
end
c = alphabet[n]
end
end
end
return str
end
puts Caesar_cipher("zabcd", 1) // "zabcd"
str = str.split("")
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
alphabet2 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
while num > 25
num -= 26
end
str = str.map do |char|
if alphabet.include?(char)
n = alphabet.index(char) + num
while n > 25
n -= 26
end
char = alphabet[n]
elsif alphabet2.include?(char)
m = alphabet2.index(char) + num
while m > 25
m -= 26
end
char = alphabet2[m]
else
char
end
char
end
return str.join
end
def cipher_shift(s, n)
letters = [*'a'..'z']
s.chars.map {|x| letters.include?(x.downcase) ? (x.ord + n).chr : x}.join
end
So I figured out that I can count through the hash, the problem is that for 7 and 9 I have four values. I have tried several other things with no luck. Can someone help understand what else I could do to get the values I want out of the hash. I realize that I can match the numbers with the key, but I am confused how to get the values to permute.
letters = {"1" => ["1", "1", "1"],
"2" => ["a", "b", "c"],
"3" => ["d", "e", "f"],
"4" => ["g", "h", "i"],
"5" => ["j", "k", "l"],
"6" => ["m", "n", "o"],
"7" => ["p", "q", "r", "s"],
"8" => ["t", "u", "v"],
"9" => ["w", "x", "y", "z"]}
phone_number = gets.chomp.to_s
words = []
word = []
numbers = phone_number.chomp.chars
count0 = 0
while count0 < 3
count1 = 0
while count1 < 3
count2 = 0
while count2 < 3
count3 = 0
while count3 < 3
count4 = 0
while count4 < 3
count5 = 0
while count5 < 3
count6 = 0
while count6 < 3
word[0] = letters[numbers[0]][count0]
word[1] = letters[numbers[1]][count1]
word[2] = letters[numbers[2]][count2]
word[3] = letters[numbers[3]][count3]
word[4] = letters[numbers[4]][count4]
word[5] = letters[numbers[5]][count5]
word[6] = letters[numbers[6]][count6]
words << word.join
count6 += 1
end
count5 += 1
end
count4 += 1
end
count3 += 1
end
count2 += 1
end
count1 += 1
end
count0 += 1
end
puts words
Edit:
I want to a seven digit number and print out all possible letter combinations. I am a beginner so I want to understand with the things I know now. I want to try and do this with if statements please.
numbers = phone_number.chomp.chars
if letters.key?(numbers[0])
if letters.key?(numbers[1])
if letters.key?(numbers[2])
if letters.key?(numbers[3])
if letters.key?(numbers[4])
if letters.key?(numbers[5])
if letters.key?(numbers[6])
end
end
end
end
end
end
end
I understand how to grab a value from a matching key, but don't get how I can hold the first value while going through the rest, if that makes any sense.
product is the function you are looking for, the following works with any number of digits:
digits = '27'
keys = digits.chars.map{|digit|letters[digit]}
p keys.shift.product(*keys).map(&:join) #=> ["ap", "aq", "ar", "as", "bp", "bq", "br", "bs", "cp", "cq", "cr", "cs"]
This prints all possible words for a variable-sized phone number:
letters = {"1" => ["1"],
"2" => ["a", "b", "c"],
"3" => ["d", "e", "f"],
"4" => ["g", "h", "i"],
"5" => ["j", "k", "l"],
"6" => ["m", "n", "o"],
"7" => ["p", "q", "r", "s"],
"8" => ["t", "u", "v"],
"9" => ["w", "x", "y", "z"]}
digits = gets.chomp.split ''
# Total number of combinations
n = digits.inject(1) { |a,b| a * letters[b].size }
words = []
0.upto n-1 do |q|
word = []
digits.reverse.each do |digit|
q, r = q.divmod letters[digit].size
word.unshift letters[digit][r]
end
words << word.join
end
puts words
For example, if the input is 67, then there are 12 combinations:
mp mq mr ms np nq nr ns op oq or os
Edit: I don't see a way to make use of the 7 if statements as you have written, but perhaps this is closer to the kind of answer you are looking for:
words = []
letters[digits[0]].each do |c0|
letters[digits[1]].each do |c1|
letters[digits[2]].each do |c2|
letters[digits[3]].each do |c3|
letters[digits[4]].each do |c4|
letters[digits[5]].each do |c5|
letters[digits[6]].each do |c6|
words << [c0,c1,c2,c3,c4,c5,c6].join
end
end
end
end
end
end
end
puts words
A good exercise would be to re-write this in a way that can work for phone numbers of any length, not just 7. Again, this is only for instructional purposes. In practice, one would use Array's product method as in hirolau's answer.
LETTERS = {"1" => ["1", "1", "1"],
"2" => ["a", "b", "c"],
"3" => ["d", "e", "f"],
"4" => ["g", "h", "i"],
"5" => ["j", "k", "l"],
"6" => ["m", "n", "o"],
"7" => ["p", "q", "r", "s"],
"8" => ["t", "u", "v"],
"9" => ["w", "x", "y", "z"]}
def convert_to_phone_number(string)
string.each_char.with_object([]) { |x, arr| LETTERS.each { |k,v| (arr.push k; break) if v.include?(x) }}.join
end
convert_to_phone_number "foobar"
#=> "366227"
i think so it is issue of cache memory
u need to change like below
LETTERS = {"1" => ["1", "1", "1"],
"2" => ["a", "b", "c"],
"3" => ["d", "e", "f"],
"4" => ["g", "h", "i"],
"5" => ["j", "k", "l"],
"6" => ["m", "n", "o"],
"7" => ["p", "q", "r", "s"],
"8" => ["t", "u", "v"],
"9" => ["w", "x", "y", "z"]}
def convert_to_phone_number(string)
string.each_char.with_object([]) { |x, arr| LETTERS.each { |k,v| (arr.push k; break) if v.include?(x) }}.join
end
convert_to_phone_number "foobar"