This is a set of optional programming tasks that you should be able to implement in Python.
Write a program that counts the number of words on all input lines. (For this exercise and other exercises on this page, consider a word to be any sequence of printable characters.)
Write a program that prints the longest word found on any input line.
Write a program that chooses 5 random numbers from the range 1..25, and prints them all. No two of the numbers may be the same. The program must choose any possible set of 5 numbers with equal probability.
Write a program that reads a series of input lines and determines how many times each letter A-Z appears in the input. You should ignore case, considering 'a' and 'A' to be the same letter. Ignore any characters that are not letters from the Latin alphabet. The input text is guaranteed to contain at least one letter.
The program should print the most frequent letter
with a count of its occurrences. It should also print a histogram
showing each letter's
frequency as a fraction of all input letters, rounded up to the
nearest percent. For example, if 3.7% of letters are N, the program
should print 'n: ****
'.
Sample input:
The quick fox jumped over the lazy dog. Then the dog got up and jumped over the fox.
Sample output:
Most frequent letter: e (9) a: * b: c: * d: ** e: *** f: * g: * h: ** …
Write a program that reads an integer N, representing a number of people. Simulate a rumor that spreads among these people as follows. A random person starts the rumor. They tell it to another person selected at random, who tells it to someone else, and so on, until someone hears the rumor who has already heard it before. At that point the rumor stops circulating. The program should produce output like this:
Enter N: 100 Person 41 heard the rumor. Person 83 heard the rumor. Person 22 heard the rumor. … Person 83 heard the rumor again. 14 people heard the rumor in all.
Suppose that I walk into a casino in Prague and I have 100 Kc. I repeatedly play a game in which I will either win 9 Kc or lose 10 Kc, with an equal chance of either outcome. My goal is to double my money: if it increases to 200 Kc, I will leave happy. On the other hand, if I run out of money I will also have to leave.
Write a program that simulates my visit to the casino. At each step, it should print how much money I currently have. At the end, it should report the outcome and the number of games that I played.
Expand the program so that it reads an integer N and simulates N visits to the casino. The program should print out the fraction of visits that were successful (i.e. in which I doubled my money), as well as the average number of games that were played.
A robot begins at square (0, 0) on an infinite two-dimensional grid. At each step, it randomly walks up, down, left, or right. Will it ever return to its starting square?
Write a program that simulates this situation. The program should read an integer N and perform N simulations. In each simulation, if the robot returns to (0, 0) we will say that it has won. If it ever reaches a square (x, y) where |x| + |y| >= 50, it has wandered too far, and has lost. The program should print out the fraction of simulations in which the robot won, as well as the average length of each simulation.
Write a program that reads a square matrix of integers. The program should print the largest value in the matrix and the column number that contains it.
Input:
2 8 3 9 6 7 0 3 -1
Output:
Column 1 contains 9.
A matrix M is symmetric if Mij = Mji for all i and j. For example, here is a symmetric matrix:
1 2 3 2 5 4 3 4 6
Write a function that takes a square matrix M represented as a list of lists, and returns True if the matrix is symmetric.
The identity matrix of size N x N contains ones along its main diagonal, and zeroes everywhere else. For example, here is the identity matrix of size 4 x 4:
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Write a function identity_matrix(n) that returns the identity matrix of size n x n, represented as a list of lists.
Write a program that reads a square matrix of integers. The program should print the largest sum of any row or column in the matrix.
Input:
2 4 8 10 5 3 7 1 9 6 9 4 2 2 8 3
Output:
32
Write a function that takes two matrices, and returns the sum of the matrices. Assume that the matrices have the same dimensions.
Write a function that takes two matrices, and returns the product of the matrices. Assume that the matrices have dimensions that are compatible for multiplication.
Solve Project Euler's problem 8:
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Read the input number as a series of lines from standard input.
Solve Project Euler's problem 9:
There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.