C# (C sharp) next_permutation function

Following the algorithm on wikipedia, I created a next_permutation function for C#. You have my permission to use it for anything (such as work, personal, or school projects) unless, of course, you are being directly tested on creating such a function (such as a school quiz, test, or homework assignment) in which case you do not have my permission to use it in any way.

/*  * Transitions perm to the next lexigraphical permutation and  * returns true, unless it is the last permutation, in which case  * it resets to the first permutation and returns false  */ static public bool next_permutation(int[] perm) {     int n = perm.Length;     int k = -1;     for (int i = 1; i < n; i++)         if (perm[i - 1] < perm[i])             k = i - 1;     if (k == -1)     {         for (int i = 0; i < n; i++)             perm[i] = i;         return false;     }     int l = k + 1;     for (int i = l; i < n; i++)         if (perm[k] < perm[i])             l = i;     int t = perm[k];     perm[k] = perm[l];     perm[l] = t;     Array.Reverse(perm, k + 1, perm.Length - (k + 1));     return true; }

LaTeX pseudocode tricks

Using the “algorithm” package, add the following LaTeX code anywhere in your document before your algorithms:

  • Add for each
    \newcommand{\FOREACH}[1]{\FOR{\textbf{each} #1}}
  • Get rid of then and do
    \renewcommand{\algorithmicthen}{\textbf{}}
    \renewcommand{\algorithmicdo}{\textbf{}}

Java Programming Tutorial Redirection

I made these tutorials because I wasn’t aware of any good (i.e. simple, clear, and concise) Java programming tutorials online. But recently I’ve been introduced to a couple of good resources, so I’m not going to make any more tutorials.

Good luck with your programming!

Java Programming Tutorial 4: Variables

Variables store information in memory (such as a person’s name, the position of the mouse cursor on the screen, and whether a window is minimized or not)
  • CREATING – Variables are always made by specifying the type of variable and then the variable name (ex. ”int age” creates an integer variable called “age”).   Variable names can’t contain spaces our strange characters. And, of course, if all you’re doing is creating the variable on one line by itself, then you need to end your command with a semi-colon (ex. “int age;”)
  • ASSIGNING – Set a variable’s value with the “=” operator.  This can be done at the same time that the variable is created (ex. “int age = 25;”) and/or at any other time in the program (ex. “age = 20;”)
  • USING – Simply type the name of the variable, by itself, to use it (ex. “age”).

These are the most important variables:

  • int – An integer stores whole numbers, using 32-bits of memory (less common are the byte, short, and long variables which are just like the int, but use 8-bits, 16-bits, and 64-bits, respectively).  The more memory a variable uses, the bigger the number the variable can hold.
  • double – Stores a decimal number, using 64-bits of memory (less common is the float which is just like the double, but uses 32-bits). More memory, in this case, means better precision.
  • char – Just one letter or character (ex. ‘a‘ or ‘*‘) which must be surrounded by two apostrophe marks.
  • boolean – Is either true or false.

Give it a try!

  1. Open your file from the previous tutorials
  2. Before the line that starts with “System.out.println”, create several lines of variables of different types.  Assign some of these variables when you create them and some of them later in the program. For example: “int age = 5;” and “bool hungry = true;” and “age = 20;”
  3. Duplicate the “System.out.println” line for each variable that you have, deleting the “Hello World” message (and its quotation marks) and replacing it with the name of the variable. For example: “System.out.println(age);”

Your program might look something like this:

public class Program {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int age = 5;

double cost = 2.89;  // Gas prices in northern Utah

char middleInitial = ‘Z’;

boolean hungry = false;

boolean male;

age = 20;

hungry = true;

male = hungry;

System.out.println(“Hello World!”);

System.out.println(age);

System.out.println(cost);

System.out.println(middleInitial);

System.out.println(hungry);

System.out.println(male);

}

}

Run your program and notice the output in the console.

Java Programming Tutorial 3: Writing Your First Program

The file that you created in the previous tutorial should look like this:

public class Program {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

 

}

 

}

 

  • COMMENTS – Anything on a line that is to the right of “//” is ignored by Eclipse.  Also, anything in the program after a “/*” and before a “*/” is ignored.  These are tools for you to write comments in your program that help other programmers (and yourself) to remember what’s going on in your program – to take notes, you might say.
  • FUNCTIONS – Functions are small parts of your program.  The line that says “public static void main(String[] args)” starts a function called “main”.  The following “{” marks the beginning of the function, and the next “}” marks the end of the function.  As you can see, nothing actually happens in the function right now.  When you run a Java program, it starts by looking for a function called “main” and runs it.  We’ll talk more about the words that come before the function name (“public static void”), and the stuff between the parenthesis, later.
  • CLASSES – Classes are groupings of functions and data.  They always have the word “class” before the name of the class.  They also start and end with “{” and “}”.

THE BIG PICTURE – As you can see, we have a class (called “Program”) containing one function (called “main”) that does absolutely nothing.  Our first task will be to make the program display text to the user.  To do that, write the following somewhere between the main function’s “{” and “}” markers:

System.out.println(“Hello World!”);

This command runs another function called “System.out.println”.  When running another function, everything inside of the parenthesis is sent as information to that function.  All text messages (called “Strings”) must be surrounded in quotation marks.  Finally, all commands end with a semi-colon.

You’re final program should look like this:

 public class Program {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(“Hello World!”);

}

 

}

Run your program by clicking on the “Run” menu and selecting “Run” or “Run Last Launched.”  Notice, in the “Console” tab, that the message “Hello World!” was displayed!

Java Programming Tutorial 2: Creating a New Project

Every program that you make needs to be in a new “Project.”

  1. Open eclipse.exe
  2. From the “File” menu, select “New\Java Project”
  3. Enter the name of your program into the “Project name” box (ex. “Hello World”)
  4. Click “Finish”

Congratulations, you made a project for your first program!  Now you need to create a file to write your Java code in.

  1. Find your project In the “Package Explorer”
  2. Click on the “+” next to your project to expand it
  3. Right-click on the “src” item, and then select “New\Class” from the drop-down menu
  4. Enter the name of the file that you want to make (ex. “Program”) in the “Name” box
  5. If this is the main file for your program, check the box next to “public static void main(String[] args)”
  6. Click “Finish”

Now you’re ready to start programming!

Java Programming Tutorial 1: Installing Tools

These instructions will lead you through the process of installing tools needed in order to program in Java on a Windows computer.

  1. You need a JDK (Java Development Kit) installed on your computer. You don’t need to download any package with additional tools, such as JavaFX or Java EE, just the JDK by itself. You can get it from the following website:
  2. http://www.oracle.com/technetwork/java/javase/downloads/index.html

  3. You should have Eclipse installed on your computer (we will use this as the Java code editor). You need the “Eclipse IDE for Java Developers.” You can get it from the following website:
  4. http://www.eclipse.org/downloads/

    Then unzip the file to some directory (say, “C:\Program Files” – it should create a subdirectory called “eclipse”)

Leaving it to the Officials, Whenever Possible

Cliff Stoll’s love, Martha, addressed his ethical concerns as he tracked down a hacker in his book, “The Cuckoo’s Egg.” Most people value privacy. Martha personally wouldn’t care to invade anyone’s privacy, but she encouraged Cliff to compromise the privacy of several users when it allowed him to find one hacker. She minimized concern for punishment by saying, “the worst it would be is invasion of privacy … so I don’t see why you can’t” (Stoll 23). Later, she suggested that – depending on the circumstances – the hacker might even be the “good guy.” The ethical implications of Cliff’s attempts are challenged as she says, “maybe this hacker is closer to us politically than those security people. What if you’re chasing someone on your own side?” (Stoll 82). Loyalty and the importance of privacy are two of the ethical issues addressed by Martha.

Loyalty to one side also means commitment in fighting the opposition. If the hacker shared the same political agendas with Cliff, and was hoping to improve the world through such hacking attempts, then it would relatively be ethically right for Cliff to support the hacker. Being in favor of the U.S. government, however, it was Cliff’s ethical responsibility to fight the hacker.

Sometimes it would be too difficult, or impossible, to enforce a law without invading privacy. In this situation, the privacy of several users was compromised at the cost of watching a hacker. If everyone was legally allowed to watch what anyone else was doing at any time, then there would be no assurance of privacy. However, it’s important that some law enforcers are permitted to breach privacy, on certain grounds, for detective work to progress. To keep individual privacy, citizens should be punished for invading the privacy of others unless such law enforcers refuse to take action. In Cliff’s situation, he is justified in watching the activity of all users, and in tracing phone calls, because the FBI wouldn’t look into the case themselves.

Flatter, Smaller, and One in Culture

Eventually, even if it takes a while, decisions that affect most or all of a population, will settle on what the majority prefers. When the majority of the Nephites were opposed to God, they chose to govern contrary to His commandments – selfishly engaging in war, and creating allies that promoted dishonesty (with the Gadianton robber’s, for example). When the majority of the city of Enoch favored God, they lived the law of consecration. “Flattening” the world, as described by Thomas L. Friedman in his book, “The World is Flat,” is preferable to the current majority of humankind because it seems to empower us to live more comfortably, to be able to get more accomplished in life, and to profit more than we could have in the past. The world is flattening because the majority of humankind wants it to.

Not only is the world flattening, but it’s getting smaller too. With Google Earth, I can visit Paris without leaving my bedroom. I can also drive around my own street, and see the apartment complex that I live in, using Google’s Street View. I don’t feel so far from my parents, who live 700 miles away, because I can hear their voices over the phone. I almost felt like I was able to visit my friend, in person, who lives 1,800 miles away, as I talked to her with my WebCam. Not all, but some of the same forces that are flattening the world, are making it smaller at the same time.

As the world is getting flatter and smaller, individuals are becoming more alike. Flattening applies to job opportunities and information availability, sure, but also to cultural diversity. British bands are finding fans in America, and American bands are becoming popular in Asia. Clothing fashions are being shared among nations. Core values, within a culture, will define the rate and direction that the culture allows itself to change. But in all, cultures are combining into one.

Don’t Make Yourself Too Comfortable

As I am getting older, I am beginning to hear more, specific, rules to making computer use a safe experience. I hear these rules and I think, “that’s right! How could anyone ever be safe without living that rule? I don’t remember MY parents giving me that rule.” That’s because the potential dangerous of computer use is increasing – for at least two reasons. Offenses are increasing as the number of Internet users is increasing and predators are becoming smarter. Our defense is decreasing as we are becoming more comfortable with the Internet – “letting down our guard.” Other than imprisoning predators, there is little that we can do to decrease the offensive side of Internet use. We can, however, personally improve our defense by learning and following Internet safety rules.