Objectives Achieved
This week started with understanding references and dictionaries in Python and ended with an introduction to text files and CSV files, which stands for comma-separated Values. Generally, we go through the lines in a file by using the split method.
for line in new_file:
line = line.replace("\n", "")
parts = line.split(";")
The following are the list of concepts I have gone through:
lists within lists, two-dimensional array, matrix, row, reference, id
pure functions, dictionary, keys, del, clear(), tuple, text files, file handle,
line break \n, CSV file, comma-separated Values, split, strip(), lstrip, rstrip
Some Statistics: 46% Completed
Parts Completed This Week: 5.1, 5.2, 5.3, 5.4
Here are some codes I did this week
A Sudoku Block
This code combines 3 previous functions. It takes a two-dimensional array representing a sudoku grid as its argument. It checks whether each of the nine rows, columns and 3 by 3 blocks in the grid contain each of the numbers 1 to 9 at most once.
Transposing a square matrix
We assume the input must be a square represented in terms of a list. The code transposes the matrix by setting up a duplicate matrix. This allows us to use the helper variable temp to to obtain elements in the original matrix without interfering with out output.
Inverting a dictionary
This code swaps the keys and values in a dictionary. Essentially we now have dict[value] = key.
Finding the largest number in numbers.txt
This reads every number in the text file and returns the maximum number.
Returning a dictionary from a CSV file
line.replace gets rid of the lines breaks. line.split takes the separator character(s) as a string argument, and returns the contents of the target string as a list of strings, separated at the separator.
parts[0] is the name of the fruit and parts[1] is its price.
That’s it for this week.
P.S. I have condensed the content here because I want to focus more on actually coding, rather than documentation.
Thank you.
About Me
Hello I am Barry and I study mathematics at the University of Warwick. I also write on Medium.
If you want to reach out, please do so below 🦁