For a reference manual of python 3.4, look at https://docs.python.org/3.4/index.html.

Compute the value of π

Let's start by looking at a circle inscribed in a square, as in the figure.


If the circle in centered at (0,0) and has a radius if 1, the length of the side of the square is 2. The area of the circle is π * r2= π and the area of the square is (2r)2=4. The ratio of the area of the circle to the area of the square is π/4.

This suggests a method to compute π. If we compute the ratio of the area of the circle to the area of the square and multiply the result by 4 we have the value of π.

To compute this ratio we use a Monte Carlo method, which is method that uses a sequence of random numbers. Suppose we select a random point (x,y) with [-1 ≤ x ≤ 1] and [-1 ≤ y ≤ 1] and we check if the point is in the circle or in the part of the square outside the circle. If we generate points many times, the ratio of the number of points that are inside the circle to the total number of points generated is a good approximation of π/4. The accuracy of the results depends on the number of points used. More points produce a more accurate value.

To use the Monte Carlo method described above, you need to generate points by generating random numbers in the [-1, 1] range. For each point, you need to check if the point is inside the circle or not (recall that the parametric form of a circle is x2 +y2 = r) and determine if the point falls within the circle or not. Try various sample sizes, including 100, 1,000, 100,000 and 1,000,000 and observe how the sample size affects the accuracy of the result.

To generate random numbers in python, you need to import the random module:

from random import *
uniform(-1,1)  #generates random float in -1 to 1 range

Compare your results with the value that python has for π, which is defined in the math module

from math import *
pi

The 1089 game

This is a simple game that you can implement in a few line of python code and you can use to impress your friends. Ask them to think about a 3 digit number, with the 3 digits different from each other and in decreasing order. Ask them to reverse the number and subtract the reversed number from the original one. Then take the answer and reverse it. Add the reversed answer to the result of the subtraction. The result is always 1089.
For example: the start number is 321. The reverse is 123. Compute 321-123 = 198. Reverse 198 and get 891. Add 891 + 198 = 1089.

Play Blackjack

BlackJack is a popular casino card game. The objective is to continue adding cards to your hand without its value exceeding 21. The value of the hand is the sum of the individual card values as follows: the cards 2 to 10 count at face value (2-10 points), the Jack, Queen and King count as 10 points each, and the Ace counts as either 1 or 11 depending on the most advantageous count.

Start by writing a program to determine the score of a BlackJack hand. First you need to decide how to represent the values of the cards (as characters or integers). Next, how to represent a hand (as a list, for instance). Given a hand of cards compute its value if less than 21. Since the Ace can count as either 1 or 11, you should initially value any Ace as 11 unless the total of the hand is greater than 21. If the value of the hand is greater than 21, you should recompute it assuming the Ace(s) is (are) valued as 1.

Next create a deck of cards, each with value and suite (for instance, using a list). You need to shuffle the deck, which you can do using another function from the random module, shuffle(x), where x is a sequence.
Write a function to start the game and .. start playing!

DNA

The DNA molecule employs a 4-element code consisting of sequences of the following nucleotides: Adenine, Guanine, Cytosine and Thymine. Biologists use a letter sequence to represent the genome, in which each letter stands for one of the four nucleotides. For example: . . . AGTCTATGTATCTCGTT . . .
Individual genes are substrings of a genome delineated by 3-element start and stop codons. The majority of genes begin with the start codon: ATG and end with one of the following 3 stop codons: TAG, TAA or TGA. Start codons can appear anywhere in the string, followed by a series of 3-element codons and ending with a stop codon. Note that genes are multiples of 3 in length and do not contain any of the triples ATG, TAG, TAA or TGA.

Write a Python program that reads a genome as a string and displays all the genes in the genome. Note that that finding a substring matching an end codon but which overlaps two codons would not be an actual end codon (i.e., the resulting gene string should be divisible by three).
Example:

Enter a DNA sequence: TCATGTGCCCAAGCTGACTATGGCCCAATAGCG
Gene 1 TGCCCAAGC
Gene 2 GCCCAA
This is just the beginning to having fun with DNA problems.
(Example from Mark LeBlanc, Wheaton College DNA course)


Copyright: © 2017-2019 by the Regents of the University of Minnesota
Department of Computer Science and Engineering. All rights reserved.
Comments to: Maria Gini