Showing posts with label one. Show all posts
Showing posts with label one. Show all posts

Wednesday, August 10, 2011

Project Euler #1 in Ruby

Project Euler #1


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

The problem is fairly straightforward, and is the most correctly answered problem in the whole set.  I will be explaining a little bit of Ruby in this solution I give.

  1. x = 0
  2. sum = 0
  3. while (x < 1000)
  4.   if (x % 3 == 0 && x % 5 == 0)
  5.     sum += x
  6.   elsif (x % 3 == 0)
  7.     sum += x
  8.   elsif (x % 5 == 0)
  9.     sum += x
  10.   end
  11.   x += 1
  12. end
  13. puts sum

1.  This is the declaration of a variable in Ruby, and you will notice you do not have to declare its type.  It is inferred from what the variable is set equal to when it is declared.  Handy!  This is what is meant by being a "dynamically typed language", other programming languages like C require you to declare the variable's type at declaration and forever afterward it is of that type.  This is referred to as a "statically typed language". 

2.  See line 1.

3.  While loops in Ruby are very similar to those seen in Java or C++, the difference being we don't use brackets, instead we get a great deal of use out of 'end' statements.  You will notice that everything within the while loop is tabbed out once (at least).  If you use NetBeans, or Notepad++, or some such fancy editor, it will automatically do this and help you see where your blocks of code are located.

What you need to know for 'while' loops, is that you need the keyword 'while' then a conditional, and to close the 'while' block, you must include the 'end' keyword, just like I did on line 12. 

For more information on while loops in Ruby, you may use Tutorials Point, or the slightly more concise Techotopia wiki.

4.  This is our first look at 'if' statements.  Again, very similar to C++, even in the modifiers '&&' or '||' which are 'AND' and 'OR' respectively, in how they modify logical statements. 

In our case, line 4 says "if x modulus 3 is equal to 0 and if x modulus 5 is equal to 0 then...", and that is all there is to it!  For those new to the game, modulus is just looking for the remainder after we divide.

For example:  6 % 5 = 1, because 6/5 = 1 R 1, where 'R' stands for 'remainder', and % stands for 'modulus' or 'mod' for short.

If you would like to learn more about the modulus, or the area of modular arithmetic in mathematics, you can look here, for a quick primer on the subject.

5.  Line 5 is just what it looks like for anyone that has used C or Java, the "+=" operator takes the left hand side and simply adds to it the right hand side.  For example:

  1. x = 0   #=> 0
  2. x += 3  #=> 3
  3. x += 39 #=> 42

6.  An 'elsif' statement is just an "else" and "if" statement crammed together, and takes a conditional just like the 'if' statement in line 4.

10.  We will skip to line 10, as lines 7 through 9 are explained previously.  All there is to explain here is that when using 'if', you must also close the block with the 'end' keyword.  This is not true for an 'else' or 'elsif' as these are part of a larger 'if' block or statement.

13.  This is the last line in our code, and important if we want to know what the answer is!  "puts" stands for "put string", and all you have to do to use it is put a string after it.  Our sum happens to be of the 'Integer' class, but it has been written in such a way to know what to do when we use it in the 'puts' statement.  More on this later!



As for what we are trying to accomplish in this project Euler problem, we want to sum up all of the multiples of 3 and 5 below 1000.  Pretty straightforward.  There are a few places where one can trip up though:

1.  If you incorrectly sum up multiples of both 3 and 5.  For example, 15 is a multiple of both 3 and 5, and if you write your code incorrectly you will get a sum that is too large, because you have added twice every multiple of both 3 and 15.

2.  Another place is not noticing that the question states below 1000, not up to and including 1000.  So just be wary of that. 

If anyone has some other pitfalls to be wary of, please email me and I will be sure to include them here, for those that follow after!  Good luck all, and happy coding!

Tuesday, August 9, 2011

Ruby Tutorial: SQL Query to Console i.e. TinyTDS in Action

So a previous post talked about using TInyTDS as a means to connect to your database.  Now we actually want to make a query and use it.  The first thing I did when I got it working, was figure out how I could emulate SQL Server Management Studio and make a query and display it.  So here is my code: 

  1. sql = "SELECT * FROM Whatever"
  2. client = TinyTds::Client.new(:username => "myUsername", :password => "myPassword", :dataserver => "myServer", :database => "myDatabase")
  3. result = client.execute(sql)
  4. results = result.each(:symbolize_keys => true, :as => :array, :cache_rows => true, :empty_sets => true) do |rowset| end
  5. #THIS IS TO OUTPUT IT TO THE CONSOLE
  6. for i in (0..result.fields.length)
  7.   printf("%14s", result.fields[i])
  8. end
  9. for j in (0...result.affected_rows)
  10.   puts ""
  11.   for i in (0...result.fields.length)
  12.     printf("%14s",results[j].at(i))
  13.   end
  14. end

1.  This is your SQL Statement, pretty self-explanatory.  If you need help with SQL, a great place to start is here
2.  This is where the magic happens, logging into and connecting to the database.  This was the simplest gem I could find to connect to the database, we use SQL Server (fyi). 
3.  Also very readable, if you need help with what 'execute' does, you can read about TinyTDS's syntax here.  Basically it runs the sql query. 
4. 
6-8.  This will print out the column headers from the table you just created. 
9-14.  This will print out each row, one by one. 


This post is in the process of being written

Watir Tutorial: Error Recovery

So your script is happily humming along, testing myriad little tests, and then despite all your best efforts, it hangs!  Tragedy!  Sometimes despite our best efforts, it will still crash.  But it doesn't have to be like that.  No sir.

Ruby comes with error handling and with just a few lines of code, you can get your script to recover itself when it hangs, as it sometimes will.

If you need a primer on exceptions and error handling, I recommend Alan Skorkin's post about it.  As he covers a great deal of what you need to know, we will just be talking about the Watir code you will use.

And here it is:

  1. while !(b.frame(:id => "e1menuAppIframe").text.include? "Welcome!")
  2.   autoit.Send("^!l")
  3. end
  4. retry

What this will do, is hit cancel until you are back at the welcome screen.  So by placing this in the 'rescue' portion of your error handling and the begin statement after where you would initially be seeing the welcome screen in the first place, it will work like a dream.

I understand this won't work for all cases, but for some it will, and for those that it will, it makes the testing go that much smoother!  Enjoy~

Monday, August 8, 2011

Watir Tutorial: AutoIt


This will be a short post:  As this is just a quick primer into what you can do in JD Edwards with AutoIt and Watir.  I will assume you have read this post, so that everything is put together on your computer as it should be.

To be concise, for your reference you will need this page, to reference the different commands and code to get AutoIt to work as you want it to. 

If you want to hit the 'OK' button in the form exit, we use the keyboard shortcut to do so:  'CTRL+ALT+O'

The way you would do this in your script, would be to have the following lines of code:

  1. require 'win32ole'
  2. autoit = WIN32OLE.new('AutoItX3.Control')
  3. autoit.Send("^!o")


The first line is for loading the gem so that you can access AutoIt (I don't know exactly what it does, but I think it is for accessing other programs outside of Ruby, because AutoIt can be used seperately).  A new object called autoit is created, with the specified class.  The third line is where we actually tell the keyboard to click "ctrl+alt+o".

Remember to use this cheatsheet for your reference in using AutoIt!

Watir Tutorial: Text fields in JD Edwards Enterprise One

Because JD Edwards' Enterprise One system is a web application, it can be easily tested using Watir.  The only issue I found in the most straightforward of testing is when you encounter Javascript in places like grids.  Text fields and most buttons work as expected, although you do have to drill into the HTML a step or two.  Let me give you an example.  (I will assume that EnterpriseOne, and Watir are both installed and functioning properly, as these are beyond the scope of what I intend to do here.  I will also be using Internet Explorer for this, with the Developer toolbar installed, as per the recommendation by the Watir community to locate the correct HTML tags for testing).





Let's start with a nice basic window:  The Word search window, and in our system the Fastpath would be 'P01BDWRD'.  At first glance we have a text field, a check box and a grid.  This shouldn't require any testing really since it is maintained by Oracle as I understand it, but it's an example for how to write scripts, nothing more.

Let's open the Developer Toolbar and click on the 'Search word' text field to find where it is located in the HTML.  A blue box should appear, as shown below (where the red arrow is pointing).  At the bottom, note the id field, and the name field.  Both are similar and in my tests I have used both (probably redundantly), but you may only need to use one, I'll do some testing and update later.  (Ignore the 'LEWIS*' there for now, I was just testing it.)




The other information you need is a little ways back up on the left hand window, the 'tree view', what you are looking for is indicated below, the id "e1menuAppIframe", and this will also be used later in your testing. 


The way that we tell Watir through Ruby to fill in this field is through these parameters we have just discovered using the Developer Toolbar.  Let me show you what we would code up to illustrate: 

  1. require 'rubygems'
  2. require 'watir'
  3. b = Watir::Browser.new
  4. b.goto("http://my.e1site.org")
  5. b.text_field(:name => "User").set("myUserName")
  6. b.text_field(:name => "Password").set("myPassword")
  7. b.button(:type => "submit").click
  8. b.text_field(:id =>"TE_FAST_PATH_BOX").set "P01BDWRD"
  9. b.form(:action => "/jde/E1Menu_FastPath.mafService?e1.state=maximized&e1.mode=view&e1.namespace=&e1.service=E1Menu_FastPath&RENDER_MAFLET=E1Menu").submit
  10. b.frame(:id => "e1menuAppIframe").text_field(:id => "C0_18").set("LEWIS*")

The line that we are concerned with right now is line 15 which sets our text field to the value that we want.

"b.frame(:id => "e1menuAppIframe")" is what we have to drill into to get within the correct frame.  Simply coding up the last part of line 15 I have found is not enough for Watir to find the text field in question.  Just take it as a rule of thumb, that when you are working in a program within JD Edwards, that you need to tell it that you are using that frame, and then from there tell it the text field or the button or check box or whatever.

The other piece of information we got earlier is what we use to tell it which text field.

".text_field(:id => "C0_18")" is what I had to use next, to tell it that we wanted to talk with the "Search Word" field.

Finally, ".set("LEWIS*")" is used to set the text field to the value in question.

Don't forget, that we could have used ".set("#{variabledata}")" and a for loop to cycle through an array called variabledata to get through a while set of data that we want to test.  If you are following along, and haven't successfully made a call to your database to get that data, then we will cover that later.

I hope that this has been helpful!