Windows terminal: the sort function

The tutor shares a great find on the Windows terminal.

When I was a kid, we spent many hours, in grades three through six, rewriting lists of words in alphabetical order. The advantage was twofold: writing practice (which, back then, was practically half of elementary school) and understanding how words are ordered in a dictionary. Those exercises could be ponderous; here I am, anyway:)

Today, you needn’t alphabetize a list yourself. Just type it, one word per line, to a file, then let the Windows terminal sort function take care of it. The basic syntax, at the command prompt, is

sort input_file /o output_file

To test the sort command, I first created a file called tosort0.txt. Its contents:

lewis
louise
tender
ant
importation
22
101
01
a
!

At the command prompt, I then ran

sort tosort0.txt /o sorted0.txt

Next, I opened the file sorted0.txt, which hadn’t existed before. Indeed, its contents are

!
01
101
22
a
ant
importation
lewis
louise
tender

However, the file tosort0.txt remains unchanged.

Next I ran, from the command line,

sort tosort0.txt

The list, in alphabetical order, printed to the screen.

Once again: if you don’t specify the output file, you’ll see the sorted list print right to the command line. If you specify an output file that doesn’t exist, the command will create it, then print the sorted list to it. The input file whose contents get sorted remains unchanged.

Neat, huh?

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Physics: average velocity

The tutor illuminates a very plain, yet critical, concept from physics.

Average velocity has the following definition:

    \[v_{ave}=\frac{displacement}{time}\]

Displacement means your change in position. (Read more about the difference between distance and displacement in my post here.)

Travelling straight-line, displacement and distance are numerically equivalent, although for displacement you need to report the direction.

Let’s consider the situation of straight-line travel, but with velocity that is not consistent:

Example. A motorist travelling East drives for three hours at 80km/h, then stops for a 45 minute lunch, then continues East at 70km/h for another 40 minutes. What is the average velocity?

Solution:

We refer to the formula

    \[v_{ave}=\frac{sum\ of\ displacements}{total\ time}\]

Using the formula

    \[displacement=velocity \times time\]

we can find the driver’s displacement along each leg of the journey, then add them. We divide that sum by the total travel time, including the break. Of course, the minutes need be converted to hours:

    \[v_{ave}=\frac{(80 \times 3 + 0 \times \frac{45}{60} + 70 \times \frac{40}{60})}{(3 + \frac{45}{60} + \frac{40}{60})}\ \frac{km}{h}\ East\]

which simplifies to

    \[v_{ave}=\frac{286.67}{4.42}= 64.9  \frac{km}{h}\ East\]

Due to breaks, and even stopping and starting, a person’s average velocity can be significantly less than their driving speed:)

Source:

Heath, Robert et al, Fundamentals of Physics. D.C. Heath Canada, Ltd, 1981.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Perl: a perfect squares list

The tutor offers a simple script to demonstrate the Perl range operator.

Let’s imagine someone is studying radicals (which you’ll find numerous posts about here, by typing it in the search box). They want to memorize the perfect squares; a list would be helpful.

Here’s a Perl program that prints the perfect squares from 12 to the number decided by the user:


#!/usr/bin/perl
print “Hello! I print the perfect squares.\n\n”;
print “Enter the number you want to go up to, please\n\n”;
$top=<STDIN>;
print “\nokay…\n\n”;#recall \n means newline
chomp $top;
$i=1;
for(1..$top){#range operator: loops from 1 to $top
print $i*$i.”\n”;# . joins parts of a string together for output
$i++;
}

I’ll be showing more helpful hints about Perl over the summer:)

Source:

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tree identification: a tale of two trees

The tutor shares a confusing identification experience.

In the woods where I walk, an old Sitka spruce always dominates the forest right around it. A large Sitka spruce is easy to spot by its scaly bark, rather than the vertical furrows other local evergreens have. Furthermore, Sitka spruce bark has a different color from other trees; Audubon describes it as “purplish brown.”

A few months ago, out for a walk with my kids, I decided to show them a Sitka spruce, including its cones. Next to a big Sitka spruce trunk lay many cones, but they were small (maybe 2cm long), nearly spherical, and dark brown. I recognized the cones, but knew they weren’t Sitka spruce ones.

Twenty feet away lay light-brown cones, around 8cm long and 2cm wide, with a honeycomb pattern on them. I immediately knew they must be Sitka cones; later, the guide confirmed it. On the other hand, the smaller cones I’d found right beside the Sitka spruce trunk were western hemlock ones.

The pattern repeated itself around other big Sitka spruce trees: western hemlock cones closely surrounding the trunk, with Sitka cones further out.

In these woods, the Sitka spruce are so big, they drop their cones well away from the trunk. Growing close to each Sitka spruce tree, however, you’ll find western hemlock whose cones fall around the Sitka trunk.

I’ll be sharing more about my tree observations in future posts:)

Source:

Pojar, Jim and Andy MacKinnon. Plants of Coastal British Columbia. Vancouver:
   BC Ministry of Forests and Lone Pine Publishing, 1994.

Audubon Society Field Guide to North American Trees. New York: Alfred A. Knopf,   1980.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Chess: end game, part 0

The tutor opens the discussion about the end game in chess.

At Killegar Chess, you can hear Sean Godley’s coverage of Aron Nimzowitsch’s My System, which is a leading resource for modern chess players. I’ve listened to many chapters there.

One of Nimzowitsch’s many points is that end game play distinguishes good chess players from great ones. I understand his point.

Against a computer at a reasonably challenging setting, the player likely faces a tough end game. Concentration is paramount, since a subtle difference in position can be decisive. Furthermore, many correct moves might be necessary in order to convert an advantage to a win. While the player likely gets fatigued, the computer doesn’t.

I’ll be discussing the end game in more detail in future posts. For the home player against the computer, it can be daunting.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Perl random walk simulation: code explanation

The tutor continues with some explanation of last post’s Perl random walk simulation.

Below is the Perl code from last post, this time with some comments (recall that # denotes a comment in Perl):

#!/usr/bin/perl # needed for Linux (Unix)
print “Welcome to the random walk simulation.\n\n”;
print “Enter the number of steps you’d like, please.\n\n”;
$steps=<STDIN>;# <STDIN> fetches user input
chomp $steps;# chomps the newline from the input
$pos=0;# walker’s position starts as 0
for($i=0;$i<$steps;$i++){
$rn=int(rand(100)); # Perl random number function
if($rn % 2==0){# % is Perl’s modulus operator
$pos=$pos-1;
}# if the random number is even, walker steps left
else{
$pos++; # adds 1 to the $pos value
}# otherwise, walker steps right
}
if($pos<0){ $dir="left"; $pos*=-1;# means $pos=$pos*-1;
}
else{
$dir=”right”;
}
print “The position, after $steps steps, is $pos units $dir\n\n”;

This random walk is a great springboard to summer posts about probability. Of course, we never need a reason to return to Perl:)

Source:

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Perl simulation: a random walk

The tutor returns to Perl programming with a random walk simulation.

A really simple conception of a random walk is that the walker starts at 0 on the number line, then makes consecutive steps, each of which can either be left or right. The probability of left or right can be equal or different. After a certain number of steps, the position of the walker is taken.

Perl, with its rand() function, facilitates easy simulation of a random walk:

#!/usr/bin/perl
print “Welcome to the random walk simulation.\n\n”;
print “Enter the number of steps you’d like, please.\n\n”;
$steps=<STDIN>;
chomp $steps;
$pos=0;
for($i=0;$i<$steps;$i++){
$rn=int(rand(100));
if($rn % 2==0){
$pos=$pos-1;
}
else{
$pos++;
}
}
if($pos<0){ $dir="left"; $pos*=-1; }
else{
$dir=”right”;
}
print “The position, after $steps steps, is $pos units $dir\n\n”;

Next post I’ll explain this program:)

Sources:

Ross, Sheldon M. Probability Models, Fifth Ed. San Diego: Academic Press, Inc., 1993.

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Field guides, old memories: tree identification

The tutor reflects on summers past as he discusses his latest identification.

I recall, growing up in the Annapolis Valley, Nova Scotia, a tree with deeply furrowed, dark grey bark, and compound leaves whose leaflets were rounded, not pointed. It comes to mind now because this is the time of year the Valley is abloom.

This morning I searched my guides for that type of tree. I recall people mentioning ash, but the guides suggest black locust. Though its native range doesn’t extend to Nova Scotia, one guide points out that it has been widely planted elsewhere. Could it dwell in the beloved Annapolis Valley?

Yes indeed, according to HistoricPlaces.ca.

Source:

Audobon Society Field Guide to North American Trees. New York: Alfred A. Knopf, 1980.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

English: more homonyms (aka homophones)

The tutor offers another couple of fun homonyms, or homophones, depending on which dictionary you use.

Homonyms are a favoured topic here. Just key homophone or homonym in the search box to bring up a few earlier articles about them.

As a family, we play Boggle, the old word-find game by Parker Brothers, 1973. Of course, knowing rare words is handy in that context.

Today, my older son claimed “wale”, asking for a dictionary check. The word turned out to be good: Webster says it means “a ridge on cloth”, like you see with corduroy. So whale and wale are another pair of homonyms for our growing list.

At the same time, there’s weal, which means (Webster, again) prosperity – as opposed to the much more common wheel.

Boggle is a great way to cut the hours spent with electronics:)

Sources:

Collins Essential Canadian English Dictionary, Harper-Collins, 2004.

Oxford Canadian Dictionary of Current English, Oxford University Press, 2005.

Merriam-Webster Dictionary, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Chess: GNU Chess

The tutor opens a discussion about the wide world of computer chess.

Back in the 70s and 80s, you (theoretically) needed an opponent if you wanted to play chess. (I think I’ve read that Bobby Fischer, as a kid, usually served as his own opponent – that’s how he learned.)

Since the advent of computer chess, however, you can play whenever you want – and even adjust the difficulty of your opponent.

When I got back into chess about a year and a half ago, I began with GNU Chess, which is convenient to download to a Ubuntu system. (Ubuntu is the flavour of Linux I use.)

GNU Chess has some great advantages:

  1. It offers a very plain view in which the different pieces are easy to tell apart.
  2. It’s (in my experience) fast and reliable, probably because it’s so plain.

GNU Chess offers three difficulty levels: easy, normal, and hard. I always play as black, so what follows is from Black’s point of view:

CAUTION: “Easy” doesn’t mean “beginner.” The “easy” setting is not “easy” to beat, unless you’re quite an experienced player. I’ve beaten the “normal” setting a couple of times.

On the internet I’ve noticed some people suggest that GNU Chess always responds the same way to the same moves. Not true. In my recent experience, White always does open with the same move (Kt to C3), but within a couple of moves, White’s can vary even if Black always makes the same ones.

Playing Black, I beat GNU Chess Normal Setting yesterday. It took me about 80 minutes; my brain was mush for the rest of the day.

HTH:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.