Writing a ruby while loop that take integers as an input and stores them in an array
when a certain integer is entered (-1) the loop should stop and print out the array.
When -1 is entered the loop stops but there is no output.
puts " Enter a number"
x = -1.to_i
number = ' '
numbers = []
while number != x
number = gets.chomp
numbers.push(number)
end
puts
numbers.pop
p numbers
The loop will never stop gets.chomp returns a String so the loop will go on for ever because:
"-1" != -1
#=> true
Try changing this to:
while number != x
number = gets.chomp.to_i
numbers.push(number)
end
Related
I wanted to add a timer which would count the time of sorting the five highest numbers with detailing each one individually, that is, the timer starts counting when sorting starts, then saves the time of sorting the first highest number, then resets and counts the time of the second highest number until the first five numbers are counted.
Here is my code, I don't know how to place timer so it will count what I want
def bubble_sort(tablica)
n = tablica.length
liczba_max = tablica.max(5){ |a, b| a<=>b}
loop do
zamienione = false
(n-1).times do |i|
if tablica[i] > tablica[i+1]
tablica[i], tablica[i+1] = tablica[i+1], tablica[i]
zamienione = true
end
end
break if not zamienione
end
puts "Sorted array:"
tablica
end
puts "size of array:"
ilosc_liczb = gets.chomp()
i = 0
liczba = Array.new(ilosc_liczb.to_i)
puts "Unsorted array:"
while i < ilosc_liczb.to_i
nubmers = Random.new_seed
liczba[i] = nubmers
puts liczba[i]
i += 1
end
puts bubble_sort(liczba)
puts "5th number"
puts liczba[-5]
Please help me
I have the code, that will generate random numbers, unless the generated random number is 0. When the result is 0, the loop breaks.
So when the loop breaks, I want a code, that keeps adding the random numbers that kept generated and display it at the last. Can I do that in ruby?
def batting
loop do
runs = [0,1,2,3,4,5,6]
myruns = runs.shuffle.first
Newscore =
puts "Press W to hit a shot"
user_input = gets.chomp
while user_input.include? "W"
puts myruns
until myruns == 0
break
Score = Score + myruns
break
This is throwing Dynamic Constant assignment error at Score = Score + myruns which I basically think, its wrong, since the myruns keep changing at every generated event?
So, I would want to create a new variable, that would store the total of all the random numbers generated until the generated random number is 0.
Could anyone help?
May be you are looking for something like this ?
def batting
runs = [0,1,2,3,4,5,6]
final_score = 0
puts "Press W to hit a shot"
user_input = gets.chomp
while user_input.include? "W"
myruns = runs.sample
if myruns != 0
final_score += myruns
else
break
end
puts "myruns=#{myruns}","final_score=#{final_score}"
puts "Press W to hit a shot"
user_input = gets.chomp
end
puts "myruns=#{myruns}","final_score=#{final_score}"
end
You can do something like this:
def batting
loop.with_object([]) do |_,obj|
x = rand 7
x == 0 ? raise(StopIteration) : obj << x
end.sum
end
batting #=> 33
batting #=> 0
batting #=> 18
Using loop this continually creates random numbers from 0 - 6 with rand 7. We use a ternary operator to stop the loop with StopIteration if x == 0, otherwise we push x into the obj array (which is initially []). Finally we sum the obj array.
Key methods: loop, Enumerable#with_object, rand, Array#sum
This question already has answers here:
How to test if a string is basically an integer in quotes using Ruby
(19 answers)
Closed 6 years ago.
I have this code where I am entering input for sides of a triangle. Depending on the values, it will print it the triangle is equilateral, isoceles, or scalene. It's executing for number values, but how do I specify that the input should only be integers? For example, if I type in "w" , it should say invalid or error, but in this case, it executes. How do I solve this?
Basically, I am looking for a way to write that if a string were to be inputted, it should show up as an error (then I would write a print statement saying it is invalid). So could I put that into another if statement? (before the ones mentioned below)
Example Code:
puts "Enter the triangle length"
x = gets.chomp
puts "Enter the triangle width"
y = gets.chomp
puts "Enter the triangle height"
z = gets.chomp
if x == y and y == z
puts "This triangle is equilateral"
else if
x==y or y == z or x==z
puts "This triangle is isoceles"
else if
x!=y and y!=z and x!=z
puts "this triangle is scalene"
end
end
end
If you are dealing with integers, you can check this with ruby.
Note, this is not as robust as regex, but it covers most cases.
if (input != '0') && (input.to_i.to_s != input.strip)
# input is not a number
else
# input is a number
end
strip is there if you want to accept input with leading or trailing whitespace, otherwise you can leave it off.
While all the other answers are probably more or less robust, I would go with another one. Since you have a triangle sides lengths, they are to be greater than zero, right? Then one might use the side effect of String#to_i method: for everything that is not converting to integer it returns zero. Therefore:
x = gets.chomp.to_i
y = gets.chomp.to_i
z = gets.chomp.to_i
raise "Wrong input" unless x > 0 && y > 0 && z > 0
# ...
You can do something like this:
x = x.to_i
puts "Please enter an integer" if x == 0
Why?
Because:
"ABC".to_i # returns 0
You may be better off calling strip instead of chomp
gets.strip.to_i
An example:
## ruby user_age.rb
# input variables
name = ""
years = 0
MONTHS_PER_YEAR = 12 # a constant
# output variable
months = 0
# processing
print "What is your name? "
name = gets.strip
print "How many years old are you? "
years = gets.strip.to_i
puts "please enter an integer" if years == 0
months = years * MONTHS_PER_YEAR
puts "#{name}, at #{years} years old, "\
"you are #{months} months old."
There are several ways of doing it. If you allow for a leading sign,
x =~ /^[+-]?\d+$/
would be a possibility.
You will also have to think whether or not you allow surrounding or embedding spaces (for instance, a space between the sign and the first digit).
I assume that any string value that, when converted to a float, equals an integer is to be accepted and the integer value is to be returned. Moreover, I assume integers can be entered with the "xen" (or "xEn") notation, where x is an integer or float and n is an integer.
def integer(str)
x = Float(str) rescue nil
return nil if x==nil || x%1 > 0
x.to_i
end
integer("3") #=> 3
integer("-3") #=> -3
integer("3.0") #=> 3
integer("3.1") #=> nil
integer("9lives") #=> nil
integer("3e2") #=> 300
integer("-3.1e4") #=> -31000
integer("-30e-1") #=> -3
integer("3.0e-1") #=> nil
You could use Integer() to check if a string contains an integer:
Integer('1234')
#=> 1234
Integer('foo')
#=> ArgumentError: invalid value for Integer()
This could be combined with a retry:
begin
number = Integer(gets) rescue retry
end
Here is the problem:
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.
You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
Input
The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s.
Output
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them.
If the solution doesn't exist, print "-1" (without quotes).
Here is my code:
k = gets.to_i
str = gets.chomp.split(//)
n = str.length/k
map = Hash.new(0)
map2 = Hash.new(0)
str.each { |i| map[i] += 1 }
x = str.uniq.permutation(n).map(&:join).each do |string|
string.each_char { |c| map2[c] += k }
if map2 == map
puts string*k
exit
end
map2 = Hash.new(0)
end
puts '-1'
To me this solution seems like it should work, but it fails on a test case. Can anyone tell me why?
Here's my solution.
Just create one segment, then output it k times. If a character does not appear k times (or a multiple of it), then stop early and output -1.
k = gets.to_i
str = gets.chomp.split(//)
counts = Hash.new(0)
str.each { |i| counts[i] += 1 }
out = ''
str.uniq.each do |c|
if counts[c] % k != 0
puts -1
exit
end
out = out + c*(counts[c]/k)
end
puts out*k
I'm reading Chris Pine's book "Learn to Progam" (it's about Ruby). Right now I'm trying to write a program that sorts words. Unfortunately I'm stuck with: stack level too deep (system stack error) in line 16, which, if i Googled correctly means that there is an infinite loop, but I don't know why.
Here's the code:
words = []
wordss = []
word = 'word'
i = 0
k = 0
def sortw array
i = 0
if (array.length == 1) || (array.length == 0)
else sort array, [], [], i
end
return array
end
def sort unsorted, unsort, sorted, i
k = 0 # The error should be here, according to command prompt
while i < unsorted.length
while (unsorted[i] < unsorted[k])
if k < unsorted.length
k = k + 1
elsif k == unsorted.length
sorted.push unsorted[i]
else unsort.push unsorted[i]
end
end
i = i + 1
sort unsorted, unsort, sorted, i
end
if unsort.length != 1
i = 0
sort unsort, [], sorted, i
else sorted.push unsort[0]
end
return sorted
end
puts 'type one word per line...'
puts 'typing enter on an empty line sorts the inputted words'
while word != ''
word = gets.chomp
words = words.push word
end
wordss = (sortw words)
puts 'your words'
puts words
puts 'sorted here'
puts wordss
You are getting the error because recursion does not stop due to a problem with the sorting algorithm. In the sort method, k is always less than unsorted.length. This causes the other arrays, sorted and unsort to never populate.
For example try these for input:
dog
zebra
cat
Additionally, I think you want to not include the blank line so I would change the code from:
words = words.push word to words = words.push word if word != ''
This creates the unsorted array:
[0] dog
[1] zebra
[2] cat
Numbered below are the iterations of the recursive sort method.
#initial variable state:
i = 0
k = 1
dog = dog
skip second while loop
i = 1
zebra > dog
skip second while loop
i = 2
cat < dog
enter second while loop
k = 1, now cat < zebra, so keep looping
k = 2, now cat = cat, so exit while
i = 3
Since i is now equal to the unsorted array length, the two while loops never get entered anymore.
Therefore, the following code results in an infinite loop since nothing was pushed to the unsort array:
if unsort.length != 1
i = 0
sort unsort, [], sorted, i #Problem is that `unsort` and `sorted` are empty
elsif
...
end