I am trying to write a small function with ruby that gets fed a array from the user and then sums up the data in the array. I have written it as
def sum(a)
total = 0
a.collect { |a| a.to_i + total }
end
The function then runs through a rspec, which tests it by initially feeding into it a blank array. This this causes the following error
sum computes the sum of an empty array
Failure/Error: sum([]).should == 0
expected: 0
got: [] (using ==)
So its telling me that when it feeds the blank array in, its supposed to get 0 but instead its getting the array. I tried putting in a if statement written as
def sum(a)
total = 0
a.collect { |a| a.to_i + total }
if total = [] then { total = 0 end }
end
but it gives me an error saying
syntax error, unexpected '}', expecting => (SyntaxError)
what am I doing wrong?
You shouldn't use map/collect for this. reduce/inject is the appropriate method
def sum(a)
a.reduce(:+)
# or full form
# a.reduce {|memo, el| memo + el }
# or, if your elements can be strings
# a.map(&:to_i).reduce(:+)
end
See here: How to sum array of numbers in Ruby?
and here: http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-inject
def sum(a)
array.inject(0) {|sum,x| sum + x.to_i }
end
gets.split.map(&:to_i).inject(:+)
Related
I am trying to change numbers up to 100 from integers into words, but have run into some trouble, can anyone point out what is missing with my code:
def in_words(integer)
numWords = {
0=>"zero",
1=>"one",
2=>"two",
3=>"three",
4=>"four",
5=>"five",
6=>"six",
7=>"seven",
8=>"eight",
9=>"nine",
10=>"ten",
11=>"eleven",
12=>"twelve",
13=>"thirteen",
14=>"fourteen",
15=>"fifteen",
16=>"sixteen",
17=>"seventeen",
18=>"eighteen",
19=>"nineteen",
20=>"twenty",
30=>"thirty",
40=>"fourty",
50=>"fifty",
60=>"sixty",
70=>"seventy",
80=>"eighty",
90=>"ninety",
100=>"one hundred"
}
array = integer.to_s.split('')
new_array = []
numWords.each do |k,v|
array.each do |x|
if x = k
new_array.push(v)
end
end
end
new_array.join('')
end
Right now when I do:
inwords(0)
I get the following:
=>"zeroonetwothreefourfivesixseveneightnineteneleventwelvethirteenfourteenfiftee nsixteenseventeeneighteennineteentwentythirtyfourtyfiftysixtyseventyeightyninetyone hundred"
Edit
I noticed your code iterates through the array a lot of times and uses the = instead of the == in your if statements.
Your code could be more efficient using the Hash's #[] method in combination with the #map method.., here's a one-line alternative:
integer.to_s.split('').map {|i| numWords[i.to_i]} .join ' '
Also, notice that the integer.to_s.split('') will split the array into one-digit strings, so having numbers up to a hundred isn't relevant for the code I proposed.
To use all the numbers in the Hash, you might want to use a Regexp to identify the numbers you have. One way is to do the following (I write it in one line, but it's easy to break it down using variable names for each step):
integer.to_s.gsub(/(\d0)|([1]?\d)/) {|v| v + " "} .split.map {|i| numWords[i.to_i]} .join ' '
# or:
integer.to_s.gsub(/(#{numWords.keys.reverse.join('|')})/) {|v| v + " "} .split.map {|i| numWords[i.to_i]} .join ' '
# out = integer.to_s
# out = out.gsub(/(#{numWords.keys.reverse.join('|')})/) {|v| v + " "}
# out = out.split
# out = out.map {|i| numWords[i.to_i]}
# out = out.join ' '
Edit 2
Since you now mention that you want the method to accept numbers up to a hundred and return the actual number (23 => twenty three), maybe a different approach should be taken... I would recommend that you update your question as well.
def in_words(integer)
numWords = {
0=>"zero",
1=>"one",
2=>"two",
3=>"three",
4=>"four",
5=>"five",
6=>"six",
7=>"seven",
8=>"eight",
9=>"nine",
10=>"ten",
11=>"eleven",
12=>"twelve",
13=>"thirteen",
14=>"fourteen",
15=>"fifteen",
16=>"sixteen",
17=>"seventeen",
18=>"eighteen",
19=>"nineteen",
20=>"twenty",
30=>"thirty",
40=>"fourty",
50=>"fifty",
60=>"sixty",
70=>"seventy",
80=>"eighty",
90=>"ninety",
100=>"one hundred"
}
raise "cannot accept such large numbers" if integer > 100
raise "cannot accept such small numbers" if integer < 0
return "one hundred" if integer == 100
if integer < 20 || integer %10 == 0
numWords[integer]
else
[numWords[integer / 10 * 10], numWords[integer % 10]].join ' '
end
end
the integer / 10 * 10 makes the number a round number (ten, twenty, etc') because integers don't have fractions (so, 23/10 == 2 and 2 * 10 == 20). The same could be achieved using integer.round(-1), which is probably better.
It seems like all you're trying to do is find a mapping from an implicit hash
module NumWords
INT2STR = {
0=>"zero",
1=>"one",
2=>"two",
3=>"three",
4=>"four",
5=>"five",
6=>"six",
7=>"seven",
8=>"eight",
9=>"nine",
10=>"ten",
11=>"eleven",
12=>"twelve",
13=>"thirteen",
14=>"fourteen",
15=>"fifteen",
16=>"sixteen",
17=>"seventeen",
18=>"eighteen",
19=>"nineteen",
20=>"twenty",
30=>"thirty",
40=>"fourty",
50=>"fifty",
60=>"sixty",
70=>"seventy",
80=>"eighty",
90=>"ninety",
100=>"one hundred"
}
module_function
def in_words(integer)
INT2STR[integer]
end
end
The above code separates the hash definition from the method call so that the hash doesn't get recreated every time you call in_words.
You can also use Hash#fetch instead of Hash#[] as Andrey pointed out.
Your test whether x = k is your first problem (in two ways).
Firstly, if x = k means assign the value of k to x and then execute the if block if that value is true (basically anything other than false or nil).
What you should actually be testing is x == k which will return true if x is equal to k.
The second problem is that you converted your number into an array of string representation so you are comparing, for example, if "0" == 0. This won't return true because they are different types.
If you convert it to if x.to_i == k then your if block will be executed and you'll get:
> in_words(0)
=> "zero"
Then you get to move onto the next problem which is that you're looking at your number digit by digit and some of the values you are testing against need two digits to be recognised:
> in_words(10)
=> "zeroone"
You might be in looking at a different question then - or maybe that is the question you wanted answered all along!
Here's another way you might do it:
ONES_TO_TEXT = { 0=>"zero", 1=>"one", 2=>"two", 3=>"three", 4=>"four",
5=>"five", 6=>"six", 7=>"seven", 8=>"eight", 9=>"nine" }
TEENS_TO_TEXT = { 10=>"ten", 11=>"eleven", 12=>"twelve",
13=>"thirteen", 15=>"fifteen" }
TENS_TO_TEXT = { 2=>"twenty", 3=>"thirty", 5=>"fifty", 8=>"eighty" }
def in_words(n)
raise ArgumentError, "#{n} is out-of_range" unless (0..100).cover?(n)
case n.to_s.size
when 1 then ONES_TO_TEXT[n]
when 3 then "one hundred"
else
case n
when (10..19)
TEENS_TO_TEXT.key?(n) ? TEENS_TO_TEXT[n] : ONES_TO_TEXT[n]+"teen"
else
t,o = n.divmod(10)
(TENS_TO_TEXT.key?(t) ? TENS_TO_TEXT[t] : ONES_TO_TEXT[t]+"ty") +
(o.zero? ? '' : "-#{ONES_TO_TEXT[o]}")
end
end
end
Let's try it:
in_words(5) #=> "five"
in_words(10) #=> "ten"
in_words(15) #=> "fifteen"
in_words(20) #=> "twenty"
in_words(22) #=> "twenty-two"
in_words(30) #=> "thirty"
in_words(40) #=> "fourty"
in_words(45) #=> "fourty-five"
in_words(50) #=> "fifty"
in_words(80) #=> "eighty"
in_words(99) #=> "ninety-nine"
in_words(100) #=> "one hundred"
Here the increased complexity may not be justified, but this approach may in fact simplify the calculations when the maximum permitted value of n is much greater than 100.
I'm trying to solve this exercise from Ruby Monk website, which says:
Try implementing a method called occurrences that accepts a string
argument and uses inject to build a Hash. The keys of this hash should
be unique words from that string. The value of those keys should be
the number of times this word appears in that string.
I've tried to do it like this:
def occurrences(str)
str.split.inject(Hash.new(0)) { |a, i| a[i] += 1 }
end
But I always get this error:
TypeError: no implicit conversion of String into Integer
Meanwhile, the solution for this one is quite the same (I think):
def occurrences(str)
str.scan(/\w+/).inject(Hash.new(0)) do |build, word|
build[word.downcase] +=1
build
end
end
Okay so your issue is that you are not returning the correct object from the block. (In your case a Hash)
#inject works like this
[a,b]
^ -> evaluate block
| |
-------return-------- V
In your solution this is what is happening
def occurrences(str)
str.split.inject(Hash.new(0)) { |a, i| a[i] += 1 }
end
#first pass a = Hash.new(0) and i = word
#a['word'] = 0 + 1
#=> 1
#second pass uses the result from the first as `a` so `a` is now an integer (1).
#So instead of calling Hash#[] it is actually calling FixNum#[]
#which requires an integer as this is a BitReference in FixNum.Thus the `TypeError`
Simple fix
def occurrences(str)
str.split.inject(Hash.new(0)) { |a, i| a[i] += 1; a }
end
#first pass a = Hash.new(0) and i = word
#a['word'] = 0 + 1; a
#=> {"word" => 1}
Now the block returns the Hash to be passed to a again. As you can see the solution returns the object build at the end of the block thus the solution works.
I am creating a method that will take an array of numbers and add them together. I don't want to use inject because I haven't learned it yet. I prefer to start with the basics. I want to use either each or while.
I've been re-writing this code and testing it against rspec, and I keep running into a problem because the first test consists of the array being empty with nil. I tried doing an if else statement to set nil to 0 if the array is empty?, but that didn't seem to work. Here is what I've got right now.
def sum(x)
total = 0
sum.each { |x| total += x}
total
end
The rspec is testing an empty array [] as well as others that have multiple integers. Thoughts?
You're not enumerating the array passed in to the method, you're enumerating the variable sum. You want x.each { |x| total += x}, although using x within the {} is a little odd in this case because you've used the name for your method parameter.
You can use compact! to remove the nils from your array.
def sum(x)
total = 0
x.compact! #lose the nils
x.each { |i| total += i}
total
end
Edit:
If the x being passed to your sum() method is nil, you can check for that with nil?.
The do something like
if x.nil?
0 #assuming you want to return 0
else
#rest of your function
You want to return nil if the array passed in is empty?
You are getting confused with your identifiers. You are trying to iterate over sum, which is the name of the method, and you are using x as both the method parameter and the iteration block parameter.
I suggest you use something more descriptive, like arr for the method parameter and v for the block parameter (holding the value of each value from the array).
Finally, you need to initialise the total to nil so that the correct value is returned if the array is empty. Unfortunately you can't do arithmetic on nil, so in the code below I have added a line to set total to zero if it isn't already set.
This will do what you ask.
def sum(arr)
total = nil
arr.each do |v|
total = 0 unless total
total += v
end
total
end
p sum [1,2,3]
p sum []
output
6
nil
You could create a new instance method for the Array class:
class Array
def sum
total = 0.0
self.each {|x| total += x if ['Fixnum', 'Float'].include?(x.class.name)}
total%1==0 ? total.to_i : total
end
end
Then you would use it like so:
puts [].sum # => 0
puts [1, 2, 3].sum # => 6
puts [2, nil, "text", 4.5].sum # => 6.5
I need to group numbers that are in numerical order from an array.
(using ruby 1.9.2, rails 3.2)
Example1:
[1,2,4,5,6]
Example2:
[1,3,4,6,7]
Example3:
[1,2,3,5,6]
Example4:
[1,2,4,5,7]
After grouping
Example1:
[[1,2],[4,5,6]]
Example2:
[[1],[3,4],[6,7]]
Example3:
[[1,2,3],[5,6]]
Example4:
[[1,2],[4,5],[7]]
You get the idea.
(What I'm actually doing is grouping days, not relevant though)
Thanks in advance!
I'm not sure what you'd call this operation, but it's a sort of grouping method based on the last element processed. Something like:
def groupulate(list)
list.inject([ ]) do |result, n|
if (result[-1] and result[-1][-1] == n - 1)
result[-1] << n
else
result << [ n ]
end
result
end
end
The Enumerable module provides a large number of utility methods for processing lists, but inject is the most flexible by far.
Perfect problem to use inject (aka reduce) with:
def group_consecutive(arr)
arr.inject([[]]) do |memo, num|
if memo.last.count == 0 or memo.last.last == num - 1
memo.last << num
else
memo << [ num ]
end
memo
end
end
See it run here: http://rubyfiddle.com/riddles/0d0a5
a = [1,2,4,5,7]
out = []
a.each_index do |i|
if out.last and out.last.last == a[i]-1
out.last << a[i]
else
out << [a[i]]
end
end
puts out.inspect
I want to use if-else condition in one line. I have used the ternary operator, but doesn't seem to work. Any clues?
class Array
def painful_injection
each do |item|
sum = yield (defined?(sum).nil?) ? 0 : sum, item #pass the arguments to the block
end
sum
end
end
puts [1, 2, 3, 4].painful_injection {|sum, nxt_item| sum + nxt_item}
This gives me an error:
Error :undefined method `+' for false:FalseClass
There are a couple of problems here. One is that defined? of some variable doesn't return nil within an assignment to that variable e.g.
irb(main):012:0> some_new_var = defined?(some_new_var)
=> "local-variable"
You also need some extra parentheses due to operator precedence.
Finally, variables defined inside a block are only available inside that call to the block so when each yields subsequent items the previous value of sum would be lost.
Why not just set sum to 0 outside of the each e.g.
class Array
def painful_injection
sum = 0
each do |item|
sum = yield(sum, item) #pass the arguments to the block
end
sum
end
end
... but then just might as well just use normal inject
[1,2,3,4].inject(0) { |sum, item| sum + item }
so perhaps you need to clarify the problem you're trying to solve?
There are two errors here.
Beware of operator priority. Use parenthesis when you are not sure
If you don't define sum outside the block, then sum won't preserve its value outside the blog.
Here's the code
class Array
def painful_injection
sum = 0
each do |item|
sum = yield((sum.zero? ? 0 : sum), item) # pass the arguments to the block
end
sum
end
end
puts [1, 2, 3, 4].painful_injection {|sum, nxt_item| sum + nxt_item}
I think this is a sollution to this specific case.
class Array
def painful_injection
sum = 0
each do |item|
sum = yield(sum,item)
end
sum
end
end
puts [1, 2, 3, 4].painful_injection {|sum, nxt_item| sum + nxt_item}
I hope this is what you're trying to achieve, I didn't get the inline if to work for the following reason:
If you use it like this:
sum = yield((defined?(sum) ? sum : sum = 0),item)
you get a problem because sum is defined but will become nil at some point and you cannot test it for defined? and nil? in the same line because the nil? test will fall over the fact that it's not defined.
So I think there is no solution to your problem.