Lab 5 COMP9021 solution

$19.99

Original Work
Category:

Description

5/5 - (1 vote)

1 Magic squares
A 3×3 square whose cells contain every digit in the range 1–9 is said to be magic of the sums of the rows, the sums of the columns and the sums of the diagonals are all equal numbers. Write a program that generates all magic squares. Here is the beginning of a possible run of the program (“possible”, as the order of the output solutions can vary):
$ python question_1.py 4 9 2 3 5 7 8 1 6
6 7 2 1 5 9 8 3 4

2 R Extracting information from a web page
Write a program that extracts titles from a front page of the Sydney Morning Herald, provided under the name SMH.txt, meant to be saved in the working directory. You are provided with the expected output, saved in the file question_2_outputs.txt, though you might do a better job and remove some of the titles (for instance, The Lady who lives on the Moon could go…). Make sure that the output does not include any unwanted HTML entity. For this question, you need to use regular expressions, in a similar way to what has been discussed during the 5th lecture when we examined the code of nash_equilibrium_calculator.py.
1
3 Sierpinski triangle
Write a program that generates Latex code, a .tex file, that can be processed with pdflatex to create a .pdf file that depicts Sierpinski triangle, obtained from Pascal triangle by drawing a black square when the corresponding number is odd. A simple method is to use a particular case of Luca’s theorem, which states that the number of ways of choosing k objects out of n is odd iff all digits in the binary representation of k are digits in the binary representation of n. For instance: •5 3= 10, which corresponds to a white square as 10 is even; indeed, 5 is 101 in binary, 3 is 11 in binary, and there is at least one bit set to 1 in 11 (namely, the leftmost one), which is not set to 1 in 101; •6 2=15, which corresponds to a black square as 15 is odd; indeed, 6 is 110 in binary, 2 is 10 in binary, and all bits (actually, the only bit) set to 1 in 10 are set to 1 in 110.
So your program has to generate a file named Sierpinski_triangle.tex, similar to the one provided; examine the contents of this file to see which text needs to be output. The file Sierpinski_triangle.pdf is also provided, but if you want to generate it yourself from Sierpinski_triangle.tex, you need to have Tex installed on your computer (install it if that is not the case, see http://www.tug.org/texlive/), and then execute
pdflatex Sierpinski_triangle.tex
from the command line, or open Sierpinski_triangle.tex in the Latex editor that comes with your distribution of Tex, and it will just be a matter of clicking a button…