Getting stuck on chapter 8:
Type as many words as we want
One word per line, continuing until we just press Enter on an empty line
Repeats the words back to us in alphabetical order.
Use 'sort'
So, here's what I got to, but I'm having funny issues with not getting the first word to push into the array [among other things]
# alphabetting
puts 'Tell us some of your favorite things!'
# create an array
words = []
while gets.chomp != ''
words.push gets.chomp
words.sort
puts words
end
Did this and it works now... Do I have to have "thing" in there though? Seems naughty to assign within a 'while' loop.
puts 'Tell us some of your favorite things!'
words = []
puts words
while (thing = gets.chomp) != ''
words.push thing
end
puts words.sort
Your first gets call is not referred to by anything, and is thrown out. It is not just the first word, but every other word that is going to be thrown out. The output routine should also be outside of the loop. A fix is:
words = []
while word = gets.chomp and not word.empty?
words.push(word)
end
puts words.sort
Try this:
puts 'Tell us some of your favorite things!'
words = []
while line = STDIN.gets
line = line.chomp
break if line.empty?
words << line.chomp
end
words = words.sort
words.each {|word| puts word }
Related
awords = []
word = "x"
puts "Type as many words as you want, or press \"enter\" to quit."
while word != ""enter code here
#get word from user
word = gets.chomp
if word == ('')
puts 'you input nothing'
end
#add to array
awords.push word
end
#user exited loop test for array before printing
puts "Now sorting what you typed.. thanks."
puts awords.sort
Everything works fine, but I want this program to skip last two "puts" if users don't input anything. Is there anyway I could just stop program after puts 'you input nothing' if users decide not to input but press enter?
If you don't want to puts the last two lines, then you should check to make sure there are values within awords.
awords = []
word = 'x'
puts "Type as many words as you want, or press \"enter\" to quit."
until word.empty?
word = gets.chomp
if word.empty?
puts 'you input nothing'
end
awords << word
end
# Check to make sure awords has values in it
unless awords.empty?
puts "Now sorting what you typed.. thanks."
puts awords.sort
end
Now the easiest way to stop the program early if no input was ever given is to add a line after puts 'you input nothing' with a return.
puts 'you input nothing'
return if awords.empty?
You'll notice that I changed a lot of your == '' methods to .empty? because that is the ruby way of doing that type of check with strings.
awords = []
word = 'x'
puts "Type as many words as you want, or press \"enter\" to quit."
until word.empty?
word = gets.chomp
if word.empty?
puts 'you input nothing'
end
awords << word
end
# Check to make sure awords has values in it
unless awords.empty?
puts "Now sorting what you typed.. thanks."
puts awords.sort
end
thank you that was very clear
When coding in Ruby, I came up with an error about needing to state all words the user inputed. I tried to change my code to get it to output that, but the problem remained. Here is my code and the Ruby instructions.
Instructions
Add an if/else statement inside your .each.
if the current word equals the word to be redacted, then print "REDACTED " with that extra space.
Otherwise (else), print word + " ".
The extra space in both cases prevents the words from running together.
puts text = gets.chomp
puts redact = gets.chomp
words = text.split(" ")
words = ['hi', 'hello', 'what', 'why']
words.each do |word|
if gets = words
print "Redact "
else
print word + "Incorrect"
end
end
The problem it says I have with my code is... Oops, try again. Make sure to print each word from the user's text to the console unless that word is the word to be redacted; if it is, print REDACTED (all caps!).
I would appreciate all help, please and thank you.
Ben sorry these guys aren't being too helpful. :D It's been awhile but I hope this helps. In the first section titled "What you'll be building" Codecademy gives you the exact example (the answer) to the final problem in the section. This is always true and may help in the future. What you're looking for:
puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp
words = text.split(" ")
words.each do |word|
if word != redact
print word + " "
else
print "REDACTED "
end
end
Have pass several years I know, but I wanted pass and let my solution for this excercise.
puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp
words = text.split(',') # "," is necessary to identify each of the words
words.each do |x|
if x == redact # if words repeat, print REDACTED
print "REDACTED"
else # else, only write de word and space
print x + " "
end
end
I want to write a basic ABCs app as beginner programmer. I want to only see if the first letter in the user's input is correct, the reset of the word doesn't matter.
puts "What word starts with the letter 'A'?"
ans = gets.chomp
puts "Correct!" if ans.include? .......
I don't know how to evaluate only the first letter of the user's input. Please Help!
You can use the string.start_with?(arg) method.
http://apidock.com/ruby/String/start_with%3F
puts "What word starts with the letter 'A'?"
ans = gets.chomp # => 'apple'
"Correct!" if ans.start_with?('a') # => true
puts "What word starts with the letter 'A'?"
ans = gets.chomp
puts "Correct!" if ans =~ /^a/i # Case insensitive
I'm making an auditor with ruby which started off fine this morning (using single word, user inputted content to omit) but now that I've tried to implement a wordlist, it puts the string to search through as many times as there are words in the wordlist, only censoring it once or twice. My code is as follows.
#by Nightc||ed, ©2015
puts "Enter string: "
text = gets.chomp
redact = File.read("wordlist.txt").split(" ")
words = text.split(" ")
redact.each do |beep|
words.each do |word|
if word != beep
print word + " "
else
print "[snip] "
end
end
end
sleep
I kind of understand why it doesn't work but I'm not sure how to fix it.
Any help would be appreciated.
There's an easier way than iterating through each array. The Array#include method can be easily used to see if the word is contained in your redacted list.
Here's some code that should behave how you wanted the original code to behave:
puts "Enter string: "
text = gets.chomp
redact = File.read("wordlist.txt").split(" ")
words = text.split(" ")
words.each do |word|
if redact.include? word
print "[snip] "
else
print word + " "
end
end
Scrubbing text gets very tricky. One thing you want to watch out for is word boundaries. Splitting on spaces will let a lot of beep words get through because of puctuation. Compare the first two results of the sample code below.
Next, assembling the split text back into its intended form with punction, spacing, etc., gets to be quite challenging. You may want to consider using regex for something presuambly as small as user comments. See the third result.
If you're doing this as a learning exercise, great, but if the application is sensitive where you're likely to take heat over failures to bleep words, you may want to look for an existing well-tested library.
#!/usr/bin/env ruby
# Bleeper
scifi_curses = ['friggin', 'gorram', 'fracking', 'dork']
text = "Why splitting spaces won't catch all the friggin bleeps ya gorram, fracking dork."
words = text.split(" ")
words.each do |this_word|
puts "bleep #{this_word}" if scifi_curses.include?(this_word)
end
puts
better_words = text.split(/\b/)
better_words.each do |this_word|
puts "bleep #{this_word}" if scifi_curses.include?(this_word)
end
puts
bleeped_text = text # keep copy of original if needed
scifi_curses.each do |this_curse|
bleeped_text.gsub!(this_curse, '[bleep]')
end
puts bleeped_text
You should get these results:
bleep friggin
bleep fracking
bleep friggin
bleep gorram
bleep fracking
bleep dork
Why splitting spaces won't catch all the [bleep] bleeps ya [bleep], [bleep] [bleep].
I am going through the exercises in codecademy and am stuck at a place where my code isn't doing what I need it to do. All I need it to do is print the words but if the word is redacted, I need it to print "REDACTED". From what I can see, that is what my code is doing but I must have a symbol in the wrong place or something is missing. So, if anyone can see where I went wrong, I sure would appreciate a nudge in the right direction! Thanks you kindly! Here's my code :
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact print "REDACTED"+" " else print x+" "}
I found the answer. Thank you all anyway!
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact then print "REDACTED"+" " else print "#{x}"+" " end}
words is an array, and redact is a string. So you can't compare them using ==. What you're trying to do is see if redact is present anywhere in words. You can do this using include?:
if words.include?(redact) ...
An even better way to implement this would be to use a regex on the original input string:
print text.gsub(/\b#{redact}\b/, 'REDACTED')
Use $end instead of end for
words.each
{
|x|
if x == redact
print "REDACTED"+" "
else
print x+" "
}