Skip to content

Instantly share code, notes, and snippets.

@jeffhussmann
Created January 18, 2014 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffhussmann/8484220 to your computer and use it in GitHub Desktop.
Save jeffhussmann/8484220 to your computer and use it in GitHub Desktop.
Explanations of Rosalind's Python Village problems.
{
"metadata": {
"name": "Jeff Hussmann 01-16-14 Rosalind Python Village"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "## Rosalind - Installing Python"
},
{
"cell_type": "code",
"collapsed": false,
"input": "import this",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Rosalind - Variables and Some Arithmetic\nThe operator for exponentiation is two astericks - `**`."
},
{
"cell_type": "code",
"collapsed": false,
"input": "a = 3\nb = 5\nprint a**2 + b**2",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "34\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Rosalind - Strings and Lists\nNotice how we can assign multiple values to variables at the same time.\n\nSince the problem says 'inclusively', we need to add 1 to the slice end points."
},
{
"cell_type": "code",
"collapsed": false,
"input": "s = 'HumptyDumptysatonawallHumptyDumptyhadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain.'\na, b, c, d = 22, 27, 97, 102\nprint s[a:b + 1] + ' ' + s[c:d + 1]",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Humpty Dumpty\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Rosalind - Conditions and Loops"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Again, the word 'inclusively' means we need to add 1 to the `range` end point."
},
{
"cell_type": "code",
"collapsed": false,
"input": "a, b = 100, 200\ntotal = 0\nfor i in range(a, b + 1):\n if i % 2 == 1:\n total += i\nprint total",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "7500\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "A more experienced Python programmer might use a generator expression."
},
{
"cell_type": "code",
"collapsed": false,
"input": "sum(i for i in range(a, b + 1) if i % 2 == 1)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": "7500"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Rosalind - Dictionaries"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Strings have a `split()` method that splits the string at any whitespace and returns a list of the resulting pieces."
},
{
"cell_type": "code",
"collapsed": false,
"input": "s = 'We tried list and we tried dicts also we tried Zen'",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": "counts = {}\nfor word in s.split():\n if word not in counts:\n counts[word] = 1\n else:\n counts[word] += 1\n\nfor word in counts:\n print word, counts[word]",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "and 1\nWe 1\ntried 3\ndicts 1\nZen 1\nlist 1\nwe 2\nalso 1\n"
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": "A more experienced Python programmer might use a special kind dictionary object from the `collections` module called `Counter`."
},
{
"cell_type": "code",
"collapsed": false,
"input": "from collections import Counter\ncounts_two = Counter(s.split())",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Let's check that this gives the same answer as the other way."
},
{
"cell_type": "code",
"collapsed": false,
"input": "print counts == counts_two",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True\n"
}
],
"prompt_number": 9
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment