Write a function square_sum(n) that takes a positive integer n and returns the sum of the squares of all integers from 1 through n. Your function must have only one line of code. In other words, its body must look like this:
def square_sum(n): return _________
Furthermore, it must use only a constant amount of memory.
Write the built-in function filter() using a generator function.
Write a function sequence(a, b) that takes two iterators a and b, and returns an interator that produces all the elements of a followed by all the elements of b.
Write a function combinations() that takes a list l and an integer k, and prints all combinations of k elements of l. For example:
>>> combinations([2, 4, 6, 8, 10], 3) 2 4 6 2 4 8 2 4 10 2 6 8 2 6 10 2 8 10 4 6 8 4 6 10 4 8 10 6 8 10
a) Write a program that can search for all solutions to the eight queens problem and print them in this form:
. . Q . . . . . Q . . . . . . . . . . Q . . . . …
b) Modify your program so that it finds and prints only a single solution.
c) Generalize the program so that it can search for a solution to the problem of placing N queens on an N x N chessboard.
d) Modify the program so that it reports the time in milliseconds to find a solution to the N-queens problem for every value N in the range 1 ≤ N ≤ 20. Run the program and plot the results on a graph.
Write a method that takes a connected undirected graph G in adjacency list representation. The method should determine whether it is possible to color the graph with three colors (red, green, blue) such that no two adjacent vertices have a same color. If it is, it should print out a valid coloring.
A clique of a graph is a subgraph that is
connected, i.e. in which there are edges between all pairs of
vertices. Write a function clique
that takes an
undirected graph in adjacency-matrix representation and an integer N,
and returns true if the graph has any clique of size N. No efficient
algorithm is known to solve this problem, so you will need to
enumerate all possible cliques.
Two graphs G and H are isomorphic if there is a one-to-one mapping f from the vertices of G to the vertices of H such that u and v are adjacent in G if and only if f(u) and f(v) are adjacent in H. Write a function that takes two undirected graphs in adjacency-list representation and returns true if they are isomorphic. No efficient algorithm is known to solve this problem, so you will need to check all possible bijections between the graphs.