Archive
Improve web readability with … Readability
arc90 has a free service called Readability which is invaluable for reading articles on the web. In a nutshell, it strips out all the extraneous content on a page and presents just the main article text, in a beautifully formatted way. You can configure the way the final page is rendered using the previously linked page, with such options as margin width, text size, the ability to convert hyperlinks to footnotes to avoid distraction, and a few different typographical options. Once you’ve done that, you merely drag button up to your bookmarks as a link; whenever you’re on a page that’s overly busy, you just choose that bookmark and the page instantly converts to a more readable document.
The one drawback I’ve found is that nicely formatted code in an original document does not come out that way in the Readability version. Still, if what you’re reading is not full of source code, this site is absolutely vital.
Car Talk Puzzler #4: Flipping Ages
RAY: This was sent in many weeks ago by Wendy Gladstone, and as usual I tweaked it a little bit.
She writes: “Recently I had a visit with my mom and we realized that the two digits that make up my age when reversed resulted in her age. For example, if she’s 73, I’m 37. We wondered how often this has happened over the years but we got sidetracked with other topics and we never came up with an answer.
“When I got home I figured out that the digits of our ages have been reversible six times so far. I also figured out that if we’re lucky it would happen again in a few years, and if we’re really lucky it would happen one more time after that. In other words, it would have happened 8 times over all. So the question is, how old am I now?”
Here’s the fourth in my Car Talk Puzzler series; today I’m going to be using Python because it’s my current favorite language, and because it’s well suited to filtering, mapping, etc. I won’t put too much commentary here.
# Find all the ages such that the second age is the reverse of the first age. Don't worry that there are a lot of impossibilities; we'll fix it through filtering # Note that [::-1] is the slice operator that says iterate backwards through the string; this effectively reverses the list. matching_ages = map(lambda x:(x, int(str(x)[::-1])), range(0,100)) matching_ages # OUT: [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 1), (11, 11), (12, 21), (13, 31), (14, 41), (15, 51), (16, 61), (17, 71), (18, 81), (19, 91), (20, 2), (21, 12), (22, 22), (23, 32), (24, 42), (25, 52), (26, 62), (27, 72), (28, 82), (29, 92), (30, 3), (31, 13), (32, 23), (33, 33), (34, 43), (35, 53), (36, 63), (37, 73), (38, 83), (39, 93), (40, 4), (41, 14), (42, 24), (43, 34), (44, 44), (45, 54), (46, 64), (47, 74), (48, 84), (49, 94), (50, 5), (51, 15), (52, 25), (53, 35), (54, 45), (55, 55), (56, 65), (57, 75), (58, 85), (59, 95), (60, 6), (61, 16), (62, 26), (63, 36), (64, 46), (65, 56), (66, 66), (67, 76), (68, 86), (69, 96), (70, 7), (71, 17), (72, 27), (73, 37), (74, 47), (75, 57), (76, 67), (77, 77), (78, 87), (79, 97), (80, 8), (81, 18), (82, 28), (83, 38), (84, 48), (85, 58), (86, 68), (87, 78), (88, 88), (89, 98), (90, 9), (91, 19), (92, 29), (93, 39), (94, 49), (95, 59), (96, 69), (97, 79), (98, 89), (99, 99)] # Here we filter by only allowing matches in which the mother's age is greater than that of the child. Note the use of a lambda expression, basically an anonymous function. filtered1 = filter(lambda (mother,child):mother > child, matching_ages) filtered1 # OUT: [(10, 1), (20, 2), (21, 12), (30, 3), (31, 13), (32, 23), (40, 4), (41, 14), (42, 24), (43, 34), (50, 5), (51, 15), (52, 25), (53, 35), (54, 45), (60, 6), (61, 16), (62, 26), (63, 36), (64, 46), (65, 56), (70, 7), (71, 17), (72, 27), (73, 37), (74, 47), (75, 57), (76, 67), (80, 8), (81, 18), (82, 28), (83, 38), (84, 48), (85, 58), (86, 68), (87, 78), (90, 9), (91, 19), (92, 29), (93, 39), (94, 49), (95, 59), (96, 69), (97, 79), (98, 89)] # Assume that the mother was at least 15 when she had the kid, and no more than 60 filtered2 = filter(lambda(mother, child):mother-child >= 15 and mother-child < 60, filtered1) filtered2 # OUT: [(20, 2), (30, 3), (31, 13), (40, 4), (41, 14), (42, 24), (50, 5), (51, 15), (52, 25), (53, 35), (60, 6), (61, 16), (62, 26), (63, 36), (64, 46), (71, 17), (72, 27), (73, 37), (74, 47), (75, 57), (82, 28), (83, 38), (84, 48), (85, 58), (86, 68), (93, 39), (94, 49), (95, 59), (96, 69), (97, 79)] len(filtered2) # OUT: 30 # Create a new list comprised of the differences in age between mother and child age_diff = map(lambda(mother,child):mother-child, filtered2) age_diff # OUT: [18, 27, 18, 36, 27, 18, 45, 36, 27, 18, 54, 45, 36, 27, 18, 54, 45, 36, 27, 18, 54, 45, 36, 27, 18] sorted(age_diff) # OUT: [18, 18, 18, 18, 18, 18, 18, 27, 27, 27, 27, 27, 27, 36, 36, 36, 36, 36, 45, 45, 45, 45, 54, 54, 54] # The puzzler states that it's will happen a total of 8 times; that matches the age difference of 18 years filter(lambda(mother,child):mother-child == 18, filtered3) # OUT: [(20, 2), (31, 13), (42, 24), (53, 35), (64, 46), (75, 57), (86, 68), (97, 79)]
Thus the mother is currently 75 years old and the daughter is 57. Tada
Quotes, quotes, quotes: A primer for the command line
In Bash programming, there are a lot of ways to get input into programs. In particular, there are a slew of different quoting methods you should understand. This article provides a quick reference of the difference between using No quotes, Double Quotes, Single Quotes, and Backticks
No quotes
Standard shell scripts assumes arguments are space delimited. You can iterate over elements in this way:
for i in Hi how are you; do echo $i; done Hi how are you
This is why it is a problem to have spaces in your file names. For instance,
$ ls with spaces.txt $ cat with spaces.txt cat: with: No such file or directory cat: spaces.txt: No such file or directory
Here I naively typed with spaces.txt thinking the cat program could handle it. Instead, cat saw two arguments: with, and spaces.txt. In order to handle this, you can either escape the space,
$ cat with\ spaces.txt
or use the double quotes method. (Note that if you use tab autocompletion, the backslash escape will be added automatically)
Double quotes
Double quotes can be used when you want to group multiple space delimited words together as a single argument. For instance
for i in "Hi how" "are you"; do echo $i; done Hi how are you
In the previous example, I could do
$ cat "with spaces.txt"
and the filename would be passed as a single unit to cat.
An important thing to note is that shell variables are expanded within double quotes.
name=Frank; echo "Hello $name" Hello Frank
This is crucial to understand. It also allows you to solve problems caused by having spaces in file names, especially when combined with the * globbing behavior of the shell. For instance, let’s say we wanted to iterate over all the text files in a directory and do something to them.
$ ls with spaces.txt withoutspaces.txt $ for i in *.txt; do cat $i; done cat: with: No such file or directory cat: spaces.txt: No such file or directory # Surround the $i with quotes and our space problem is solved. $ for i in *.txt; do cat "$i"; done
(Yes I know iterating over and calling cat on each argument is silly, as cat can accept a list of files (e.g. *.txt). But it illustrates the point that commands will be confused by spaces in the name and should use double quotes to handle the problem).
Single quotes are also good when you need to embed single quotes in a string (you do not need to escape them)
$ echo "'Single quotes'" 'Single quotes' $ echo "\"Escaped quotes\"" "Escaped quotes"
Double quotes are my default while I’m working in the terminal.
Single quotes
Single quotes act just like double quotes except that the text inside of them is interpreted literally; in other words, the shell does not attempt to do any more expansion or substitution. For instance,
$ name=Frank; echo 'Hello $name' Hello $name
This can save you some backslash escaping your normally would have to do.
Use it when:
- You need double quotes embedded in your string
$ echo '"How are you doing?", she said' "How are you doing?", she said
- You do not need any literal single quotes in your string (it’s very difficult to get single quotes/apostrophe literals to appear in such a string)
Back ticks
Back ticks (“, the key to the left of the 1 and above the Tab key on a standard US keyboard), allow you to substitute in the output of another command. For instance:
$ current_dir=`pwd` $ echo $current_dir /Users/nicholasdunn/Desktop/Scripts [/sourecode] This can be combined with the double quotes, but will be treated as literal characters in the single quotes: echo "`pwd`" /Users/nicholasdunn/Desktop/Scripts $ echo '`pwd`' `pwd`
Use when:
You want to capture the results of another command, usually for purposes of assigning a variable.
Hopefully this brief tour through the different types of quotes in bash has been useful.
Ternary operator in bash
Here’s a really quick tip for bash programmers.
In languages like C++, Java, Python, and the like, there’s the concept of a ternary operator. Basically it allows you to assign one value if a condition is true, else another.
In C/Java:
int x = valid ? 0 : 1;
In Python:
x = 0 if valid else 1
In Scala:
val x = if (valid) 0 else 1
Well, there’s no ternary operator in Bash, but there is a way to fake it.
valid=1 [ $valid ] && x=1 || x=0
Where whatever conditional you want is within the brackets.
If it’s valid, then the branch after the AND is followed, otherwise that after the OR is followed.
This is equivalent though perhaps a bit less readable then
if [ $valid ]; then x=1; else x=0; fi
So you should be aware of the construct in case you ever run into it, but it’s arguably less readable than just listing out explicitly what you’re doing.
Thanks to experts-exchange for making me aware of this little tip.
bpython – an excellent interpreter for python
If you use Python, you know that its interactive shell is a great way to test out ideas and iterate quickly. Unfortunately, the basic interactive shell is very barebones – there is no syntax highlighting, autocompletion, or any of the features we come to expect from working in IDEs. Fortunately if you’re on a Unix system, there is a great program called bpython which adds all of those missing features.
If you have easy_install, it’s the simplest thing in the world to install:
sudo easy_install bpython
I can’t recommend this product enough. It’s free, so what’re you waiting for?
Mac OSX – copy terminal output to clipboard
Here’s a quick tip: If you want the results of some shell computation to be accessible to your clipboard (e.g. so you can paste the results into an e-mail or into some pastebin service), you can pipe the command into the `pbcopy` program.
echo "Hello world" | pbcopy # "Hello world" is now in your clipboard
Apparently there is a way to do a similar thing on Ubuntu as well
How to remove “smart” quotes from a text file
If you’ve copied and pasted text from Microsoft Word, chances are there will be the so-called smart quotes in that text. Some programs don’t handle these characters very well. You can turn them off in Word but if you’re trying to remedy the problem after the fact, sed is your old friend. I’ll show you how to replace these curly quotes with the traditional straight quote.
Recall that you can do global find/replace by using sed.
sed s/[”“]/'"'/g File.txt
This won’t actually change the contents of the File, but you can save the results to a new file
sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt
If you wish to save the files in place, overwriting the original contents, you would do
sed -i ".bk" s/[”“]/'"'/g File.txt
This tells the sed command to make the change “in place”, while backing up the original file to File.txt.bk in case anything goes wrong.
To fix the smart quotes in all the text files in a directory, do the following:
for i in *.txt; do sed -i ".bk" s/[”“]/'"'/g $i; done
At the conclusion of the command, you will have double the number of text files in the directory, due to all the backup files. When you’ve concluded that the changes are correct (do a diff File.txt File.txt.bk to see the difference), you can delete all the backup files with rm *.bk.