290 likes | 449 Views
Programming for Engineers in Python. Recitation 13. Plan. Error detection Luhn Algorithm RAID Data structures Queue Stack Nested lists Matrices Recursion Even and Odd Recursive directory tree walk. Teaching Survey .
E N D
Programming for Engineers in Python Recitation 13
Plan • Error detection • Luhn Algorithm • RAID • Data structures • Queue • Stack • Nested lists • Matrices • Recursion • Even and Odd • Recursive directory tree walk
Teaching Survey • Please answer the teaching survey: https://www.ims.tau.ac.il/Tal/ • This will help us to improve the course • Deadline: 4.2.12
Checksums: Check Digit • A check digit is a form of redundancy check used for error detection • It consists of a single digit computed from the other digits in the message. • Detect simple errors in the input of a series of digits • Detect a single mistyped digit or some permutations of two successive digits. • http://en.wikipedia.org/wiki/Check_digit
LuhnAlgorithm – “mod 10” Algorithm • Simple algorithm • Used to validate: • Credit Card numbers • IMEI numbers (GSM SIM card identifiers) • Israeli ID Cards • Checks for mistyping errors – not for forgery! • Designed by Hans Peter Luhnwhile working in IBM during the 1950s • http://en.wikipedia.org/wiki/Luhn_algorithm
Luhn: Calculating the check digit • Double every second digit, starting from the last • Sum the digits of the doubling product • Sum the other digits as well • Multiply results by 9 (67*9=603) • Return results mod 10 (603%10=3)
Luhn: Checking number validity • Do the same process, add the check digit, and the result should be 0 mod 10 • Code: Luhn.py
XOR – Exclusive Or • Logic operator on 2 arguments • “One or the other but not both“ • Results in True: • If one of the arguments is True and one if False • Returns False: • If both arguments are True or both arguments are False • Examples: • I’m happy XOR I’m sad • It’s raining XOR it’s not raining • http://en.wikipedia.org/wiki/Exclusive_or
XOR – Exclusive Or • “One or the other but not both“ • Bitwise operation (Python ^): • Returns 1 if the bits are different, 0 if bits are identical • 1 XOR 1 = 0 • 1 XOR 0 = 1 • 0 XOR 1 = 1 • 0 XOR 0 = 0 • 1110 XOR 1001 = 0111 • Equivalent to addition without carry • Useful to calculate parity bit: • 1 XOR 1 XOR 0 XOR 1 XOR 0 = 1
RAID Parity • RAID = redundant array of independent/inexpensive disks • Storage unit combining multiple drives • One drive serves as the parity drives and allows the recovery of lost data • http://en.wikipedia.org/wiki/RAID#RAID_Parity
RAID Parity Data is written to drives #1-4 Drive #6 is updated to store the parity of drives #1-4: 00101010 XOR 10001110 XOR 11110111 XOR 10110101 = 11100110
RAID Parity When drive #3 fails, the data can be recovered by using the same parity operation with drives #1,2,4,6 00101010 XOR 10001110 XOR 11100110 XOR 10110101 = 11110111 Recovered data is stored in drive #5, the Hot Spare When drive #3 is fixed or replaced, it will be the new Hot Spare
RAID in Python • We will see a simplistic implementation • Our ‘RAID’ is a matrix of integers • We will be able to declare a matrix row as ‘faulty’ without losing its data • Code: RAID.py
Data Structure: Queue • A Queue is a FIFO data structure • Items enter in the bottom of the queue and are served from the top of the queue • Like standing in line at the bank • Useful for many applications: • Phone calls queue in call center or helpdesk • Passing “tasks” from GUI to computing process • Code: Queue.py
Queue Class classQueue: def__init__(self): self.data = [ ] defpush(self, item): self.data.append(item) defpop(self): returnself.data.pop(0)
Data Structure: Stack • A Stack is a LIFO data structure • Items enter in the top of the queue and are served from the top of the queue • Like a pile of dirt clothes… • Useful for many applications: • Parsing hierarchical data structures like XML or HTML • Towers of Hanoi • Quicksort • All we do is change the pop method of Queue!
Stack Class classStack: … defpop(self): returnself.data.pop(-1)
Flatten Lists • Assume that we have a hierarchal categorization of objects. For example: Cherry Red Fruit Strawberry Food Yellow Banana Milki Milk Milkshake
Flatten Lists • This hierarchy is represented in nested lists as: ['Food', ['fruit', ['Red','[Cherry','Strawberry']], ['Yellow',['Banana']], ['Milk', ['Milki','Milkshake']] ] • We want to flatten this structure so that all items appear in one list ['Food', 'fruit', 'Red', 'Cherry', 'Strawberry', 'Yellow', 'Banana', 'Milk', 'Milki', 'Milkshake']
Flatten Lists - Code defflatten(lst): printlst flat_lst = [ ] forx inlst: ifisinstance(x, list): flat_lst.extend(flatten(x)) else: flat_lst.append(x) returnflat_lst
Nested Lists • A list of lists • Example – matrix • Matrix multiplication: defmult(A,B): n = len(A) k = len(B) m = len(B[0]) iflen(A[0]) != k: raiseValueError("Matrices must be of appopriate sizes") C = [ [None]*m for x inrange(n)] for i inrange(n): for j inrange(m): C[i][j] = sum( [ A[i][x]*B[x][j] for x inrange(k) ] ) return C
defprint_matrix(M): for row in M: for entry in row: print entry, print print Nested Lists >>> A = [ [1,1], [1,0] ] >>> B = [ [0,1], [1,1] ] >>> print_matrix(A) 1 1 1 0 >>> print_matrix(B) 0 1 1 1 >>> print_matrix(mult(A,B)) 1 2 0 1
Simple Recursion Example • Check if a number is even or odd defeven(x): ifx == 0: returnTrue else: returnodd(x - 1) defodd(x): ifx == 0: returnFalse else: returneven(x - 1) http://en.wikipedia.org/wiki/File:Screenshot_Recursion_via_vlc.png http://paulbutler.org/archives/tail-recursion-in-python/
Simple Recursion Example >>> even(100) True >>> even(101) False >>> even(1000) RuntimeError: maximum recursion depth exceeded • This was expected…
Recursion – find files • We want to find all mp3 files on the disk • We’ll do a recursive search on the disk for such files • First some os commands: • os.listdir(dir): returns all files and directories in the directory dir • os.path.isdir(dir): returns True is dir is a name of a directory, False otherwise • os.path.join: joins a list of directory names and a file name to a path, separating the names with a separator suitable for this OS Windows: c:\examples\python Unix: /examples/python
Recursive search on directory tree • Recursion base: files • If it’s an mp3 file, we return its name • Otherwise we don’t • Recursion step: directories • We call the function for each subdirectory and merge the results http://www.mediacollege.com/internet/images/tree.gif
Recursive search on directory tree • Recursion base deffind_files(location, postfix=‘.mp3’): if not isdir(location): # not a directory – a file iflocation.endswith(postfix): return [location] else: return [ ]
Recursive search on directory tree • Recursion step else: results = [] for name inlistdir(location): new_path = join(location, name) results += find_files(new_path, postfix) return results • We’ll now run the function on this computer • Code: find_files.py
Exam Stuff • Preparation class • Yoav: Wed. 22.2, 13-15, Room 102 TOCHNA • Noga: Thu. 23.2, 10-12, Room 020 Auditorium • Send questions via email before 20.2 • We will go over the sample exam • Exam material – everything from lectures, recitations, exercises • You may bring a double sided A4 page, hand-written by yourself, no photo-copying – write on it whatever you want • This will be your very own Python reference in the future