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
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!
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 GCCCAAThis is just the beginning to having fun with DNA problems.