Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to write a program in ruby programming language which prints the longest name among others,use the split method,size max ,length.
This is what I have so far:
name = gets.chomp.split
name.each do |x|
puts x.size
for i in 1..x.size do
puts i.max
end
end
Use a a variable which is initially an empty string.
max_name = ""
When you are inside the loop, check if each x.size is larger than max_name.size. If that is the case, you have found a new max_name, so do max_name = x.
The code fails when trying to get the maximum of the integer 1. That's an odd-looking guess at the correct code, and means you should probably revise how Ruby's blocks work (you appear to be expecting an interaction between the max and each that really doesn't exist).
The usual way to get the maximum of something from a list, if you are not allowed to use built-ins, is to set a "current maximum" value and then scan through the list, checking each item to see if it is larger than the current. If it is, set the current value to that instead. At the end, you will have the largest item.
name = gets.chomp.split
current_max = ''
name.each do |x|
if x.size > current_max.size
current_max = x
end
end
puts current_max
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am running the following ruby code in my local environment:
def multiples(max)
array = []
(0...max).each do |n|
if (n % 3 == 0) || (n % 5 == 0)
array << n
end
end
array.inject(:+)
end
multiples(1000)
and nothing happens at all. My code looks good to me. What's the issue here?
I'm not sure what you're expecting, but if I paste your code into irb it does in fact do something.
> multiples(1000)
233168
If you are running your code as a command-line Ruby script, then perhaps you want to print this value so you can see the result on the console? In that case you want to use puts:
puts multiples(1000)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
how to enter the values in an array dynamically and to use it
and I'm trying to reverse it after getting the input from the keyboard.
This should get you started:
array = []
puts "Please enter each item on a separate line, then"
puts "end the input by hitting ENTER on an empty line."
while line = gets.chomp
break if line.empty?
array << line
end
puts "You entered:"
puts array.reverse
You really need to read a basic tutorial about Ruby, I have recommended this one to a few people and they liked it: https://pine.fm/LearnToProgram/.
If you want to reverse-load an array (that is, load it in reverse order, so oldest is last) then use 'unshift' while you're adding the array elements.
array.unshift = gets.chomp
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.
As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.
Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
end
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
So say I have three strings. I am trying to check if the letters in two appear EXACTLY ONCE in "doopdedoo", and if the letters in three appear an unlimited amount of times.
one = "doopdedoo"
two = "dp"
three = "o"
if one.{|a| a.chars.all? {|c| "#{three}".include?(c)}} && one.{|a| a.chars.once? {|c| "#{two}".include?(c)}}
I have used the above to test for the presence of an unlimited amount of o's. How to test for a limited amount of d's and p's?
Edit:
Sorry but I need to clarify. My expected output would be nothing for this case.
[]
Because doopdeedoo contains more than one instance of d or p.
It does contain many o's, so that's fine.
I also added the &&... part to the method above. I realize there is no 'once' method but if there is something like that I'd like to use it.
You can use the String#count method like this:
test_string = "foopaad"
must_appear_once = ['d', 'p']
must_appear = ['o']
must_appear_once.all? {|c| test_string.count(c) == 1} \
and must_appear.all? {|c| test_string.count(c) > 0}
This ensures that 'd' and 'p' each appear exacly once and that 'o' appears in the string (no matter how often).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've go a string made in this way.
"AABBCCDD....." grouped by 4 with variable lenght.
I need a method that swap that 2 by two the chars in this string
def swap2_by_2( string )
???
end
If the input is AABBCCDD the output will be BBAADDCC
Thanks, i'm very noob in ruby.
Edit: my mistake, a more comprhensive example may be.. Input: ABCDEFGH -> CDABGHEF
It is not clear what the OP is trying to do, but if it is to flip the first and the second characters with the third and fourth characters for every four characters, then the example that the OP showed is highly misleading and inappropriate (It should have been "ABCD..." instead of "AABB..."). In that case, a solution would be:
string.gsub(/(..)(..)/, '\2\1')
Thinking about your question, an interpreting the "ABCDEF", I am sure, that you are looking for pack / unpack in Ruby: I found a good page here How to change bit order in Ruby
And here are two a non-regexp versions:
p 'AABBCCDD'.chars
.each_slice(2)
.each_slice(2)
.map(&:reverse)
.join
#=> "BBAADDCC"
# or
'AABBCCDD'.chars
.each_slice(4)
.map{|x| x.rotate(2)}
.join