Below is the code I came up with for first and last questions:
puts "Enter the non-redacted text please:"
text = gets.chomp
puts "Enter the word you'd like to redact please:"
redact = gets.chomp
final = []
words = text.split (" ")
words.each do |x|
if x.downcase == redact.downcase
final.insert(-1, "REDACTED ")
else
final.insert(-1, "#{x} ")
end
end
final.each {|x| print x}
puts ""
To explain: To solved the first question, I simply added. if x.downcase == redact.downcase, which got all the text comparison on an even level, but retained the cases on the final output.
Solving the third question was slightly trickier. First, I made sure to define a variable final with nothing in it. If I didn’t put " = [] it wouldn’t spit out what I wanted. Same if I didn’t define it at all.
This all took a little bit of extra research, which I’m sure was their intention. This website was quite useful. Read that to see how the .insert method works, and how I used that to put my redacted text into it’s own variable.
I’m still having trouble with the second question. I’ve used stackexchange.com to try to see how one might compare separate elements in two arrays. Still working on it…
puts "Enter the non-redacted text please:"
text = gets.chomp
puts "Enter the word(s) you'd like to redact please:"
redact = gets.chomp
final = []
set = false
words = text.split (" ")
noShow = redact.split (" ")
words.each do |x|
noShow.each do |q|
if x.downcase == q.downcase
final.insert(-1, "REDACTED ")
set = true
end
end
final.insert(-1, "#{x} ") unless set
set = false
end
final.each {|x| print x}
puts ""
The words are separated by space. If they are separated by something else, if they are already in an array, then don’t use split? I’m not seeing how that’s not exactly what’s happening in the exercise already
Here’s my version. I took the redacted string and converted it to an array as well. Then used the .include? method to search the array. Then the .insert method to put the results into the new array “final”. printed each item from that array at the end just to make sure it worked.
puts “Enter your text to search.”
text = gets.chomp
text.downcase!
puts “Enter the text you wish to redact.”
redact = gets.chomp
redact.downcase!
final =
red_str = redact.split " "
words = text.split " "
words.each do |x|
if red_str.include?(x)
final.insert(-1, "REDACTED " )
else final.insert(-1, x + " " )
end
end
Nice work. I’ve tried to make a slightly cleaner version and changed some of the names for clarity. By adding the " " into the .each loop at the end you remove the need for a space in "REDACTED ". You also don’t need the set = false at the top because it will only check if it is true which it will not be if it doesn’t exist.
puts "What is the text you want to alter?"
text = gets.chomp
puts "What are the word(s) you want redacted?"
redact = gets.chomp
final = []
words = text.split (" ")
redact_words = redact.split (" ")
words.each { |word|
redact_words.each { |redact_word|
if word.downcase == redact_word.downcase
final.insert(-1, "REDACTED")
redacted = true
end
}
final.insert(-1, word) unless redacted
redacted = false
}
final.each { |word| print word + " "}