FAQ: Redacted! - Great Work!

This community-built FAQ covers the “Great Work!” exercise from the lesson “Redacted!”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Great Work!

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi how would you edit this code to redact mutiple words and save the output as a variable?
Thanks

puts "Enter some text: "
text = gets.chomp
text.downcase!

puts "Enter words to redact: "
redact = gets.chomp

words = text.split(" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end
end

Multiple words means giving the program an array of words to redact, so multiple inputs, or one space separated input that can be split into an array.

puts "Enter one or more spaced separated words"
redacts = gets.chomp
redacts = redacts.split()

That means we now have to iterate over the phrase array once for each word in the redacts array, meaning nested .each loops, or another method to see if the word from the phrase is in the redacts array (array.include? comes to mind).

In order to return a list of redacted words which only really makes sense if we want a count since we inputted them, already. If all we want is the list, then return redacts, else build an array and on the return use it to create a histogram.

Something of note about this basic code: It cannot distinguish end in end. or "end", etc. This is a greater challenge to overcome, but first succeed at the initial objective and try to improve once that’s working as expected.

3 Likes
puts "Text to search through: "
text = gets.chomp
puts "Words to redact "
redact = gets.chomp

text.downcase!
redact.downcase!

words = text.split(" ")

words.each do |letters|
 if redact.include? letters
   print "REDACTED "
 else
   print letters + " "
 end
end
  • What could you do to make sure your redactor redacts a word regardless of whether it’s upper case or lower case? :white_check_mark:

  • How could you make your program take multiple, separate words to REDACT? :white_check_mark:
    In the beginning I was overthinking it, trying to think about using nested .each. And also finding different ways of comparing arrays:

matches = words & redacted
no_matches = words - redacted

matches.each do |letters|
  print "REDACTED "
end
no_matches.each do |redacted|
  print redacted + " "
end

# However I couldn't manage the way to print the result in the right order.

#=>Text to search through:
#=>Hello my name is
#=>Words to redact:
#=>Hello name
#=>REDACTED REDACTED my is

Turns out using .include? waaaaay simpler. I can’t believe it took me so long to figure this out.:scream:

  • How might you make a new redacted string and save it as a variable, rather than just printing it to the console? :poop:
    I could only save it as a local variable result.
words.each do |letters|
 if redact.include? letters
   x = "REDACTED "
 else
   y = letters + " "
 end
  result = "#{x} #{y}"
  print result
end

If anyone knows how to do this last bit, please let me know

2 Likes

The problem with the include? method above is it will not differentiate between any words that partly make up the word.

For example entering the following:

Text input: This is my text,
&
Words to redact: this my

Will yield:
REDACTED REDACTED REDACTED text.

Why? Because “is” is included in “this” even though it isn’t our target word.

2 Likes

We will need to see your exact code in order to reach a conclusion. Please post in a reply.

Second exercise : * How could you make your program take multiple, separate words to REDACT?

puts “Write a sentence:”
text = gets.chomp
puts “what do you want to delete from this text?”
redact = gets.chomp

words = text.split(" “)
twowords = redact.split(” ")
#censor = 0

words.each do |word|
twowords.each do |twoword|
if word.downcase == twoword.downcase
#print "CENSORED "
censor = 1
end
end
if censor == 1
print "CENSORED "
censor = 0
else
print word + " "
end
end

1 Like

Hello!
Just started the course and got stuck on this exercise.

My code is:
puts "Print your text here: "
text = gets.chomp
text.downcase!

puts "Choose a word to redact: "
redact = gets.chomp
redact.downcase!

words = text.split (" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end
end

  • What could you do to make sure your redactor redacts a word regardless of whether it’s upper case or lower case?
    How do I return a text into it’s original view?
    If i print “My name is Alisa” and search for word “alisa” it returns text like: “my name is REDACTED”, while I would like to see it as it was in the beginning (in this case - from upcase)

  • How could you make your program take multiple, separate words to REDACT?

  • How might you make a new redacted string and save it as a variable, rather than just printing it to the console?
    I have no clue, could you please show me and explain the solution?

Thanks a lot!

I found this worked to construct a new variable:

newv = ""

words.each do |x|
if x == redact
  newv += "REDACTED "
else 
  newv += x + " "
end
end

puts newv

2 Likes

Am I wrong to believe this exercise has a bug? It does not seem to accept the “do” alternative to .each method but would only allow an answer with the curly braces… I have reported it. Or maybe there is something I don’t get.

These were my lines of code:

puts "Enter some text: "
text = gets.chomp

puts "Enter words to redact: "
redact = gets.chomp

words = text.split(" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end

Late reply, but you were pretty close. The way your code works right now is that it prints out each "REDACTED " or letters for each loop, but it doesn’t save all of them into one variable. This is how I got your code to work:

result=""

words.each do |letters|
 if redact.include? letters
   x = "REDACTED "
 else
   y = letters + " "
 end
  result += "#{x} #{y}"
end

print result

I essentially just changed it so the local variable result would have the x and y’s concatenated to it. Then after the loops are finished, it prints out the final result.

1 Like

The way I solved this issue was using adding an elsif with a condition that if it was the first time going through the loop(and it wasn’t a REDACTED word, then it’d capitalize the word and made it so it wouldn’t capitalize any other words. I did this by declaring a variable equal to zero count = 0 and adding to it each time it went through one of the if elsif statements.
Here’s the code I used:

count = 0
words.each do |word|
  if word == redact
    print "REDACTED "
    count += 1
  elsif count == 0
    print word.capitalize + " "
    count += 1
  else
    print word + " "
  end
end

jpaulines’s answer works great for taking in multiple words to REDACT. The main trouble with nested .each’s are that if you format it wrong, it goes through the second array for each entry in the first array.
For example:

This is my text: a b c d
Redact: a c
Process: a==a (true) print REDACTED
a==c  (false) goes to else and prints word (a)
b==a (false) goes to else and prints word (b)
b==c (false) goes to else and prints word (b)
c==a (false) goes to else and prints word (c)
c==c (true) prints REDACTED
d==a (false) goes to else and prints word (d)
d==c (false) goes to else and prints word (d)

This is why jpauline’s code ends the second loop after it has printed CENSORED so that it cuts the loop as soon as there’s a match with the twoword and word (redact and entry). There’s also a censor variable added, which lets the program know when to print the word or the CENSORED. This is important because without the censor variable, there’s not a if / else statement that determines which one it prints out.

I’ve shown one example in my previous reply to hanseopark5834073654, but another way would be to have a new variable and concatenate (add) the "REDACTED" or word to it as it goes through the loops.
Referencing your code, I’d change it to:

result=""
words = text.split (" ")
words.each do |word|
  if word == redact
    print "REDACTED "
    result += "REDACTED "
  else
    print word + " "
    result += word + " "
  end
end
print result

This prints out the redacted sentence twice (just to show it works). You could either remove the **print** result or the **print** inside of the loops.

You’re missing an end at the bottom. You need one end for the if / else statement, and then another end for the do.

Your code right now is only using the opening curly brace { without the }.

Hahaha thanks! You’re completely right! :star_struck:
I didn’t know anything about variables’ scope at the time.

1 Like

since i wasn’t able to find a clear way to do multiple redactions in the comments section, it took me a while to hypothesize solutions, but eventually i came to this one…

puts "Please type text"
words = gets.chomp.split(" ")
puts "What would you like to redact?"
redact = gets.chomp.split(" ")
sentence = ""
redact.each do |redaction|
  for word in 0...words.length 
    if words[word].downcase == redaction.downcase
      words[word] = "REDACTED"
    end
  end
end
print words.join(" ")
2 Likes

Lately whenever I am trying to run my code the program never seems to actually complete the code. It might do the first line of the code but after that I get a message saying the execution has expired. Is anyone else having this issue? Is it possibly a problem with my internet browser (chrome) or just internet connection?

1 Like

After Run did you click on the prompt in the console and type some text? There is a time limit on that i/o.

1 Like

Hey there! I copied your code but my output isn’t correct as censored words print twice:
text: x, y, z
redact: y, z
Output: x CENSORED CENSORED CENSORED CENSORED

What am I doing wrong?

my code is:

puts "Write three different words: "
text = gets.chomp
puts "What to redact?"
redact = gets.chomp
words = text.split(" ")
twowords = redact.split (" ")
censor = 0
words.each do |word|
twowords.each do |twoword|
if word.downcase == twoword.downcase
print "CENSORED "
censor = 1
end
end
if censor == 1
print "CENSORED "
censor = 0
else
print word + " "
end
end

i have used your code as other recommendation was not working at all, i spent almost 6 hours on this D: - it was very helpful, thank you so much!
For me, the for was not working for me so i tried it without and it worked ok.
This is what i got out:

1 Like

Hi every, I try my code here, most of the thing works as according, saving the reacted string into a variable. However, I couldn’t figure out where should I place the .downcase method to preserve the original upper case word in the phrase, which doesn’t match the redact string. Would any one have a suggestion for that?

eg. “I” in the beginning of the words variable.