Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, July 12, 2011

Python Exercises - Count the Words

My Python solution to the problem "count the frequency of words from a given input stream" is here at ideone.com.

I started out by using a For loop to send each word from a single line to the dictionary mapping function. Once this was working and proved that the dictionary mapping function worked, I re-factored the code to use the map() function instead of the For loop. Perhaps I could save some time in the mapping function (named wordcounter) by using the .setdefault method of the Dictionary object instead of explicitly checking for a key's existence in the Dictionary every time the function is called.


Friday, May 6, 2011

Python Exercises - Reverse a String

As an exercise to gain more experience with Python, I wrote a function to reverse a string. Playing with ideone.com's online Python IDE was also a bonus. You can run the following code here. This is probably not the best way to solve this problem, but it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
# implement a function that reverses a string
def reverse_string(in_string):
        # reverse string using brute force
        print " input is %s" % in_string
        length = len(in_string)
        out_string = ""
        for position in range(length, 0, -1):
                out_string += in_string[position-1]
        print "output is %s" % out_string
        return out_string
 
a = reverse_string("1234")
print a

Thursday, May 5, 2011

Robot Framework

A great blog post by Frank Berthold helped me learn to use Robot Framework. Some questions remained for me after reading Robot Framework's Quick Start Guide, and Frank's blog post answered them. My application was a content management system, and for historical purposes is available online here.

Here is one of the test suites that I came up with for testing the application:
*** Testcases ***
Homepage allows Admin Login
   Sign in as Admin

Homepage allows Admin to Add an Entry
   Click Link  Add
   Page Should Contain  Add a blog entry

Add an Entry page allows adding a test Entry
   Input Text  title  Testing Entry Title
   Input Text  subtitle  Subtitle for Testing Entry
   Input Text  content  This is a test Entry.
   Click Button  Save
   Page Should Not Contain  Error

Homepage shows test Entry
   Page Should Contain   Testing Entry Title