A few years ago, when I was more into python, I stumbled on python challenge. It was great fun, I learned a bunch of stuff and it forced me to play with libraries I wasn’t familiar with.
In their own words: (about)
Python Challenge is a game in which each level can be solved by a bit of (Python) programming.
The Python Challenge was written by Nadav Samet.
All levels can be solved by straightforward and very short1 scripts.
Python Challenge welcomes programmers of all languages. You will be able to solve most riddles in any programming language, but some of them will require Python.
Sometimes you’ll need extra modules. All can be downloaded for free from the internet.
It is just for fun – nothing waits for you at the end.
Keep the scripts you write – they might become useful.
Learning
People who know me know that I read a lot. I am, therefore, painfully aware that books are not the best way to learn things.
What is the best way to learn something?
To be honest … I don’t know. Not books, not screencasts… There are better ways: a one-on-one session, pair programming. On the side of DOING there is always: DOING more. Read more code, program more, release more.
Doing
In the spirit of the python challenge, I released today Command-line One-liner Challenges.
The idea is not exactly the same: I do provide solutions and you are allowed to move on based on your interests (or frustration).
I strive to make each challenge look and feel like any other challenge. The directory structure will be something like this:
Each challenge is its own directory (numbered). Inside it, you can find a very short instructions.txt file. There are 2 subdirectories: problem and solution. Those subdirectories should be the same except for the content of the compare.sh file.
Look at the input.txt file. Then, look at the expected.txt file. Imagine how, as a one-liner, you could transform input.txt into expected.txt.
You are supposed to run compare.sh. Just open it and fill in the blanks, so to speak.
convert() {
cat "$@"
}
convert input.txt > actual.txt
${DIFF:-diff -q} actual.txt expected.txt
The project’s README contains more information.
Comments
This is meant to be fun. Clone the repository and give it a go. I’m going to push more challenges over time, you might want to watch the repo.
If you have a better solution than what I provide, please send it to me, I’ll find a way to include it in the project.
Also, if you have an idea for a challenge, let me know.
Nice initiative! I would like to propose some Awk-ward (but really efficient—time both yours and mine one-liners on million-lines input, and be impressed how fast Awk could be sometimes) solutions for your three problems: http://gist.github.com/629488.
Whoah … nice!
I must concede your Awk supremacy. :-)
Of course, using any scripting language, these problems become trivial…
Your Awk solutions have variables and side-effects. I guess I’m leaning more toward functional programming in Bash.
(you might want to watch the github project, I’m going to push more challenges)
It’s interesting to compare different ways of solving even simple problems in one command, anyway. For example, I’ve not been aware of `-c` option that `uniq` has.
I think that a one-liner in Awk (being alone or between two pipes) does not contradict the spirit of Bash one-lining. Of course, it’s so only if an Awk (as well as, say, Sed) one-liner does not have more than about eighty characters in total and use only a few variables. When such a script becomes a (small) program, it’s definitely not a true one-liner anymore. As Larry Wall once said about scripting: “I can’t define it, but I’ll know it when I see it.”
By the way, what side-effects are you talking about? In a sense of one-liner as a functional program, Awk script is just like a flexible function that could be configured (too much :-) to take another functions as arguments. It’s like a pipe in itself.
Anyway, I’ll watch your repo on GitHub.