Download - Programming for Engineers in Python

Transcript

PowerPoint Presentation

Recitation 13

Programming for Engineers in Python1PlanError detectionLuhn AlgorithmRAIDData structuresQueueStackNested listsMatricesRecursionEven and OddRecursive directory tree walk

22Teaching Survey3Please answer the teaching survey: https://www.ims.tau.ac.il/Tal/This will help us to improve the courseDeadline: 4.2.12

3Checksums: Check Digit4Acheck digitis a form ofredundancy checkused forerror detectionIt consists of a single digit computed from the other digits in the message.Detect simple errors in the input of a series of digitsDetect a single mistyped digit or some permutations of two successive digits.http://en.wikipedia.org/wiki/Check_digit4Luhn Algorithm mod 10 Algorithm5Simple algorithmUsed to validate:Credit Card numbersIMEI numbers (GSM SIM card identifiers)Israeli ID CardsChecks for mistyping errors not for forgery!Designed by Hans Peter Luhn while working in IBM during the 1950shttp://en.wikipedia.org/wiki/Luhn_algorithm5Luhn: Calculating the check digit6Double every second digit, starting from the lastSum the digits of the doubling productSum the other digits as wellMultiply results by 9 (67*9=603)Return results mod 10 (603%10=3)

6Luhn: Checking number validity7Do the same process, add the check digit, and the result should be 0 mod 10

Code: Luhn.py

7XOR Exclusive Or8Logic operator on 2 argumentsOne or the other but not bothResults in True:If one of the arguments is True and one if FalseReturns False:If both arguments are True or both arguments are FalseExamples:Im happy XOR Im sadIts raining XOR its not raininghttp://en.wikipedia.org/wiki/Exclusive_or8XOR Exclusive Or9One or the other but not bothBitwise operation (Python ^):Returns 1 if the bits are different, 0 if bits are identical1 XOR 1 = 01 XOR 0 = 10 XOR 1 = 10 XOR 0 = 01110 XOR 1001 = 0111 Equivalent to addition withoutcarryUseful to calculate parity bit:1 XOR 1 XOR 0 XOR 1 XOR 0 = 19RAID Parity10RAID = redundant array of independent/inexpensive disks Storage unit combining multiple drivesOne drive serves as the parity drives and allows the recovery of lost data

http://en.wikipedia.org/wiki/RAID#RAID_Parity10RAID Parity11

00101010 XOR 10001110 XOR 11110111 XOR 10110101 = 11100110Data is written to drives #1-4Drive #6 is updated to store the parity of drives #1-4:

11RAID Parity12When drive #3 fails, the data can be recovered by using the same parity operation with drives #1,2,4,6

Recovered data is stored in drive #5, the Hot SpareWhen drive #3 is fixed or replaced, it will be the new Hot Spare00101010 XOR 10001110 XOR 11100110 XOR 10110101 = 1111011112RAID in Python13We will see a simplistic implementationOur RAID is a matrix of integersWe will be able to declare a matrix row as faulty without losing its dataCode: RAID.py13Data Structure: Queue14A Queue is a FIFO data structureItems enter in the bottom of the queue and are served from the top of the queueLike standing in line at the bankUseful for many applications:Phone calls queue in call center or helpdeskPassing tasks from GUI to computing processCode: Queue.py

14Queue Class15class Queue:

def __init__(self): self.data = [ ]

def push(self, item): self.data.append(item)

def pop(self): return self.data.pop(0)15Data Structure: Stack16A Stack is a LIFO data structureItems enter in the top of the queue and are served from the top of the queueLike a pile of dirt clothesUseful for many applications:Parsing hierarchical data structures like XML or HTMLTowers of HanoiQuicksortAll we do is change the pop method of Queue!

16Stack Class17class Stack:

def pop(self): return self.data.pop(-1)17Flatten Lists18Assume that we have a hierarchal categorization of objects. For example:

FoodFruitMilkRedYellowMilkiMilkshakeCherryStrawberryBanana18Flatten Lists19This 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']

19Flatten Lists - Code20def flatten(lst): print lst flat_lst = [ ] for x in lst: if isinstance(x, list): flat_lst.extend(flatten(x)) else: flat_lst.append(x) return flat_lst

20Nested Lists2121Nested Lists22>>> A = [ [1,1], [1,0] ]>>> B = [ [0,1], [1,1] ]>>> print_matrix(A)1 11 0>>> print_matrix(B)0 11 1>>> print_matrix(mult(A,B)) 1 20 1def print_matrix(M): for row in M: for entry in row: print entry, print print22Simple Recursion Example23Check if a number is even or odddef even(x): if x == 0: return True else: return odd(x - 1) def odd(x): if x == 0: return False else: return even(x - 1)

http://en.wikipedia.org/wiki/File:Screenshot_Recursion_via_vlc.pnghttp://paulbutler.org/archives/tail-recursion-in-python/23Simple Recursion Example24>>> even(100)True>>> even(101)False>>> even(1000)RuntimeError: maximum recursion depth exceeded

This was expected24Recursion find files25We want to find all mp3 files on the diskWell do a recursive search on the disk for such filesFirst some os commands:os.listdir(dir): returns all files and directories in the directory diros.path.isdir(dir): returns True is dir is a name of a directory, False otherwiseos.path.join: joins a list of directory names and a file name to a path, separating the names with a separator suitable for this OSWindows: c:\examples\python Unix: /examples/python25Recursive search on directory tree26Recursion base: filesIf its an mp3 file, we return its nameOtherwise we dontRecursion step: directoriesWe call the function for each subdirectory and merge the results

http://www.mediacollege.com/internet/images/tree.gif26Recursive search on directory tree27Recursion base

def find_files(location, postfix=.mp3): if not isdir(location): # not a directory a file if location.endswith(postfix): return [location] else: return [ ] 27Recursive search on directory tree28Recursion step else: results = [] for name in listdir(location): new_path = join(location, name) results += find_files(new_path, postfix) return results Well now run the function on this computerCode: find_files.py28Exam Stuff29Preparation classYoav: Wed. 22.2, 13-15, Room 102 TOCHNANoga: Thu. 23.2, 10-12, Room 020 AuditoriumSend questions via email before 20.2We will go over the sample exam

Exam material everything from lectures, recitations, exercisesYou may bring a double sided A4 page, hand-written by yourself, no photo-copying write on it whatever you wantThis will be your very own Python reference in the future

29