9 Replies - 2245 Views - Last Post: 05 January 2014 - 06:27 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

RPN Calculator

Posted 05 January 2014 - 02:23 PM

Having trouble figuring out the last line in the inject code block.


rpn = '70 10 4 + 5 * -'
array = rpn.split(" ").inject([]) do |array, i| # split all the numbers and operaters into seperate elements into an array
      if i =~ /\d+/          # if any of the elements in the array are single or multi-digit numbers 
        array << i.to_i      # push them into our array
      else                   # else
        b = array.pop(2)     # create an array b that consists of the last two elements of our origional array 
        array << b[0].send(i, b[1])     # calling a method on the first element in b? and pushing that in to array??????
      end
end
     


This post has been edited by macosxnerd101: 05 January 2014 - 06:18 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: RPN Calculator

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: RPN Calculator

Posted 05 January 2014 - 02:37 PM

Quote

calling a method on the first element in b? and pushing that in to array??????


Right, exactly. Note that in Ruby arithmetic operators like + and * are methods, so 10.send("+", 4) will calculate 10 + 4.
Was This Post Helpful? 0
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: RPN Calculator

Posted 05 January 2014 - 03:14 PM

thank you very much for your response. However, I am a bit confused about the array b established in line 6. When I puts the b array I get an out put of
10
4
14
5
70
70

I was expecting only two elements to be in the b array since it was assigned to the last two elements popped off of our original array. I'm still trying to work out how this algorithm works. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: RPN Calculator

Posted 05 January 2014 - 03:48 PM

nevermind....getting closer
Was This Post Helpful? 0
  • +
  • -

#5 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: RPN Calculator

Posted 05 January 2014 - 04:51 PM

I broke down the whole process. At this point I am wondering how the operators in our RPN string expression are being converted from string objects to regular old operators. If the string number have to to be converted to integers why don't the string operators have to be converted to the methods that they are??


1. "70", "10", and "4" are matched, converted into integers and are pushed into our array
array = [70, 10, 4]
the 4th element in the array is a plus sign so we go to the else statement

2. b is assigned the last two elements popped of our array
b = [10, 4]
then we proceed to the second line of our else clause

3. the first element of our b array (10) is sent the + the current character being accessed in the iteration (the plus sign is still a string object right?) and is then sent the second element of our b array (4)
10. + 4 = 14
14 is then pushed into our array
array = [70, 14]

4.back to the top of the iteration. '5' from our inputted RPN expression is now matched and converted into an integer and pushed into array
array = [70, 14, 5]

the next element encountered in the iteration is * moving us to the else clause
5. b is assigned the last two elements of array
b = [14, 5]

6.the first element in b (14) is sent i ("*") and then the second element in b (5) is sent to 14 as well
14 * 5 = 70
and 70 is pushed into our array
array = [70, 70]

7.We go back to the top. the - sign is the next character encountered in the iteration. we go to the else clause.
8. b is assigned the last two elements of out array
b = [70, 70]

8. the first element in b is sent i (-) and then it is sent second element of b (70)
70 - 70 = 0
Was This Post Helpful? 0
  • +
  • -

#6 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: RPN Calculator

Posted 05 January 2014 - 06:12 PM

why am I getting this error : undefined method `evaluate' for RPNCalculator:Class (NoMethodError)

class RPNCalculator
  def evaluate(rpn)
    @rpn = rpn
    array = @rpn.split(" ").inject([]) do |array, i| # split all the numbers and operaters (where there is an empty space) and puut them into our array
      if i =~ /\d+/                                 # if any of the elements in the array match a single or multi-digit number 
         array << i.to_i                            # convert them from string objects to integers and push them into our array
      else                                          # else
        b = array.pop(2)                            # create an array b that consists of the last two elements of our origional array 
        array << b[0].send(i, b[1])                 # send the current obeject in the iteration(an operator) and the second element of b to the first element of b
      end
    end
  end
end

r = RPNCalculator
r.evaluate('70 10 4 + 5 * -')


Was This Post Helpful? 0
  • +
  • -

#7 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1253
  • View blog
  • Posts: 4,044
  • Joined: 12-May 09

Re: RPN Calculator

Posted 05 January 2014 - 06:13 PM

If the code you supplied is accurate, you're assigning r the class RPNCalculator. evaluate is an instance method of RPNCalculator. What you wanted to do was assign r to an instance of RPNCalculator:
r = RPNCalculator.new
r.evaluate('70 10 4 + 5 * -')


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: RPN Calculator

Posted 05 January 2014 - 06:19 PM

I've merged your related threads and renamed your title to be more descriptive. Please use a title more descriptive than "Help", "Stuck" or "I'm a noob."
Was This Post Helpful? 0
  • +
  • -

#9 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: RPN Calculator

Posted 05 January 2014 - 06:22 PM

sorry about the repeat I recieved an error when I submitted the topic initially so I tried again and I ended up making 2 posts. When I add the .new keyword i receive no output on the command line.

This post has been edited by heaphyg: 05 January 2014 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: RPN Calculator

Posted 05 January 2014 - 06:27 PM

View Postheaphyg, on 06 January 2014 - 12:51 AM, said:

At this point I am wondering how the operators in our RPN string expression are being converted from string objects to regular old operators. If the string number have to to be converted to integers why don't the string operators have to be converted to the methods that they are??


That's what send does: it takes a method name as a string and calls the method with that name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1