Category: Programming


Running Python on GoDaddy


If you subscribe to GoDaddy hosting, you’ve likely noticed that they claim to support Python. However, if you are a noob to python and the web like myself, it’s unclear how to get started. So, below is a sample on how you can render a simple html page in Python under GoDaddy hosting.

1.) Create a new file with a .cgi extension (i.e. test.cgi – see: http://oscarvalles.com/test.cgi)
2.) Copy and paste the following text into your CGI file.

#!/usr/bin/python2.4
'''
This is a comment,
If you see this, CGI is not working
'''
print "Content-type: text/html\n\n"
print "<body bgcolor='000'>"
print "<font face='Courier New' color='white'>Test python page.  CGI extension </font>"
print "</body>"

Let the fun begin.

Rules for overriding methods


In C#, when you have a method in a base class that you would like to use in a derived class but with some modifications you are overriding a method.  When overriding a method in a derived class you must have the exact same signature as the method you are going to override in the base class.  In addition, it is good practice to include the virtual keyword in the signature of the base class method that is to be overridden by a derived class method.

Example method in base class:

public virtual string setSpecies()
{<br />&nbsp;&nbsp; &nbsp;return &quot;Mammal&quot;; &nbsp;&nbsp;<br />}

 

Example – overridden method in derived class

public override string setSpecies()
{<br />&nbsp;&nbsp; &nbsp;return &quot;Reptile&quot;;<br />}

 

 

 


This program demonstrates the use of the split method to populate a new array.  The user will enter his/her first name and the results will show the user’s last name followed by a comma then the first name in uppercase.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace pe7_3

{

    classProgram

    {

        staticvoid Main(string[] args)

        {

            string[] arSplit;

            string inValue;

 

            Console.Write(“Enter your first and last name: “);

            inValue = Console.ReadLine();

            arSplit = inValue.Split(‘ ‘);

            Console.Write(arSplit[1].ToUpper() + “, “ + arSplit[0].ToUpper());

           

            Console.ReadLine();

        }

    }

}

 

 


This program will generate two sets of 10 random numbers using C#.  Two arrays are used to hold two different sets of random numbers.  The product of those random numbers are then stored in a third array.  The output is displayed in a message box. To display a message box in a console program you will have to do 2 things.  1.) place the statement ”using System.Windows.Forms” in your program and add the reference to the assembly.  You can do this by right clicking on the ‘References’ folder on your Solution Explorer in VS Studio.  Click on Add Reference.  When the list of references come up, choose ‘Windows.Forms’.

 

using System;

using System.ComponentModel;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace pe7_4

{

    class Program

    {

        static void Main(string[] args)

        {

            Random rand = new Random();

            double[] aOne = new double[10];

            double[] aTwo = new double[10];

            double[] aThree = new double[10];

            int randNum = rand.Next(20);

            string result = "The product of 2 random numbers is: \n";

 

            for (int i = 0; i < 10; i++)

            {

                aOne[i] = rand.Next(20);

                aTwo[i] = rand.Next(20);

                aThree[i] = aOne[i] * aTwo[i];

                result += aOne[i] + " * " + aTwo[i] + " = " + aThree[i] + "\n";

            }//end for loop

 

            MessageBox.Show(result, "Random numbers and their squares. ",

                            MessageBoxButtons.YesNo, MessageBoxIcon.Information);

 

        }//end Main method

    }

}

 

 

Sorting an ArrayList in C#


This program illustrates how to accept any number of names into an ArrayList.  It asks the user to enter the last name then first name of the person.  The input is controlled by a sentinel value where the user is asked to enter the word “END”.  It can be in either lower or upper case. After typing end, the program will show a sorted list of the names entered.

 

This program makes use of a separate method to perform the sort function, all though it could have all been done under the Main method.  It uses the string.ToUpper method for the sentinel value and makes use of the break statement in the loop so as to not add any variant of the word “END” in the list.

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

 

namespace pe7_5

{

    class SortingArray

    {

        static void Main(string[] args)

        {

            ArrayList lnameFname = new ArrayList();

            string sentinel = "";

            int alCount;

            Console.WriteLine("Enter the last and first " +

                              "name of a person. " +

                              "Type END to exit");

            while (sentinel != "END")

            {

                Console.Write("Last Name & First Name: ");

                sentinel = Console.ReadLine();

                if (sentinel.ToUpper() == "END")

                    break;

                lnameFname.Add(sentinel);

            }

 

            alCount = lnameFname.Count;

            ArraySort(lnameFname, alCount);

 

            Console.Write("The Program has terminated. Press [ENTER] to exit.");

            Console.ReadLine();

        }// end Main method

 

        static void ArraySort(ArrayList lnameFname, int alCount)

        {

            lnameFname.Sort();

            foreach (string lf in lnameFname)

                Console.WriteLine(lf);

        }

    }

}