6. Great work!


#1


What could you do to make sure your redactor redacts a word regardless of whether it's upper case or lower case?
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'd like to see example codes explaining these questions. It would be simpler for me to understand.

`


#2

I am on this one too :slight_smile: I solved the first one like so:

*(i won't post the whole code, just the parts you need to edit)

1.
puts "What is the text?"
text=gets.chomp
text.downcase!

puts "What to take"
redact=gets.chomp
redact.downcase!

That way it downcases everything so you are good to go.

2.
Just add another gets.chomp for the new variable "redact2" and add it also as an elseif below so it checks if there are any words that are equal to redact2

puts "what to take ALSO"
redact2=gets.chomp
redact.downcase!

words=text.split(" ")

words.each do |word|
if word == redact
print "REDACTED "

**elsif word == redact2**

** print "REDACTED "**

else
    print word + " "
end

end

3.
I guess they just want us to do:

var = redact (sounds too simple, not sure)


#3

Firts method is not the best as it makes all the text to turn into downcase so the user input wont appear exactly as it should.

Here is my way of working this out:

/////////////////////////////////////////////////////////////
words.each do |word|

if word.downcase ==  redact  #checks if the downcased word in the user input equals 
to the word he/she wants to REDACT, ex: text = "BEST random text", word to REDACT : "best"

print "REDACTED "

elsif word.upcase == redact  #if there is not such a case it would check the opposite 
case, for example: text = "best random text", word to REDACT = "BEST"

print "REDACTED "

/////////////////////////////////////////////////////////////

check the comments in this code to get an explanation + an example.

About the other ones, I'm still working on them and I will be sure to update if I get it right.

Good luck.


#4

Oh nice, interesting way to solve it! Cudos man !
You could add a final one for "Best" just to cover all "standard" options, but other than that, perfect ! :wink:


#5

I thought about it but I'm too lazy for that :slight_smile: and not I'm stuck at 2 :frowning:

By the way thanks I appriciate the comment <3 !

EDIT:

I have managed to solve number 2 without limiting the user to only 2 redactions, I won't take all the credit because I did use Google that reminded me of the .include? method.
/////////////////////////////////////////////////////////////////////
puts ("Type a text:")

text = gets.chomp

puts ("What words should I look for? (press SPACE after everyword)")

redact = gets.chomp.downcase

words = text.split (" ")

redaction_list = redact.split (" ")

words.each do |word|

if redaction_list.include? word.downcase

print "REDACTED "

else
print word + " "

end
end
//////////////////////////////////////////////////////////////////////

Explanation: I have changed the code a bit and made it smaller,added a new array for the multiple redaction words, and made the code to downcase the words that the user wants to REDACT, also I have made the code to downcase each word speratly in the array "words" to compare to the downcased redacted words in the "redaction_list" array.
Note that if the word in the "words" array is not required to be REDACTED the prompt will be the original word and not the downcased word so it works perfectly.


#6

Impressive man :slight_smile: So simple and effective !


#7

`
puts ("Type a text:")

text = gets.chomp

puts ("What words should I look for? (press SPACE after everyword)")

redact = gets.chomp

words = text.split (" ")

redaction_list = redact.split (" ")

words.each do |word|

if redaction_list.include? word.downcase||word.upcase

print "REDACTED "

else
print word + " "

end
end
//////////////////////////////This amendment of an OR operator + .upcase, finalises it so that whatever case is input in either of the two fields the word will be redacted.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.