Check if array contains element and give output [closed] - ruby

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
tickets = Array.new(5) {rand(10)+1}
How can I make a ruby code, that checks if any tickets equals, for example 5. I want output something like
=> "Ticket #5 wins!"
So - if tickets generate 1, 5, 4, 2, 3 and my rule is it to equal 5, it outputs
=> "Ticket #5 wins!"

To check if an array includes an element and print the result.
winner = 5
puts "Ticket ##{winner} wins!" if tickets.include? winner

Related

Ruby count of the most frequent number in big integer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have big number for example 123456782345673455676767878
And i need to find the amount of the most frequent number in this big integer
num = num.digits # turn num into an array of digits [1, 2, ...]
num.tally.max_by { |digit, count| count }[0]
Note: solution only works on ruby 2.7+ (when tally method was added)

Get the mode of a matrix [closed]

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 a matrix that is 300x50 big filled with integers. I want to find what number appears the most in that 300x50 matrix and have it returned as an integer.
Just use mode on the linearized matrix:
mode(matrix(:))
Example:
>> matrix = [3 2 3; 2 2 1]
matrix =
3 2 3
2 2 1
>> mode(matrix(:))
ans =
2

Execution of ruby OR condition on an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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
In irb
[[]] | [3]
produces
[[], 3]
I am having some difficulty understanding it. How is the OR operator working here ?
According to the documentation,
Set Union — Returns a new array by joining ary with other_ary,
excluding any duplicates and preserving the order from the original
array.
[1,2,3] | [4,5,6] # => [1, 2, 3, 4, 5, 6]
[1,2,3] | [4,1,2] # => [1, 2, 3, 4]
|| is the logical OR operator you might be thinking of.
| with arrays performs a set union operation on the arrays and gives you an array that has all the unique elements of both arrays. More details at ruby-doc

Remove last two digits of number in Ruby [closed]

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 a number that comes from an external API but it has two extra zeros at the end. What would be a solution to remove the two zeros. I have tried the chomp method and that doesn't work. The number I receive from the API is a Bignum. I am using Ruby 2.
This is a sample input from the API is 1374577200000
This is a sample output I want is 13745772000
If the number is an integer, you can use integer division:
> 123456 / 100
=> 1234
You can't chomp a Fixnum or Bignum, so you'd have to convert it back and forth:
number = 123400
number.to_s[0..-3].to_i

How to use an upto method in ruby to count up by 100? [closed]

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 need to make a program that counts down from 10, displays a message, and then immediately after starts counting from 100 metres to 200 metres, and so on up to 100,000 metres. I can't figure out how to do the last part. This is the code I have so far.
10.downto(1) do|counter|
puts counter
sleep 1
end
puts "Blast off!"
If you mean in increments of 100, try this:
100.step(100_000, 100) {|c| puts c; sleep(0.5)}

Resources