Your SlideShare is downloading. ×
0
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Python & Perl: Lecture 07
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Python & Perl: Lecture 07

906

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
906
On Slideshare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python & Perl Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Encoding & Decoding Messages with Huffman Trees ● String Methods ● Dictionarieswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. Encoding & Decoding Messages with Huffman Treeswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. Sample Huffman Tree {A, B, C, D, E, F, G, H}: 17 1 0 {B, C, D, E, F, G, H}: 9 A: 8 1 0 {E, F, G, H}: 4 {B, C, D}: 5 0 1 0 1 {C, D}: 2 {E, F}: 2 {G, H}: 2 B: 3 0 1 0 1 0 1 C: 1 D: 1 E: 1 F: 1 G: 1 H: 1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Symbol Encoding ● Given a symbol s and a Huffman tree ht, set current_node to the root node and encoding to an empty list (you can also check if s is in the root nodes symbol leaf and, if not, signal error) ● If current_node is a leaf, return encoding ● Check if s is in current_nodes left branch or right branch ● If in the left, add 0 to encoding, set current_node to the root of the left branch, and go to step 2 ● If in the right, add 1 to encoding, set current_node to the root of the right branch, and go to step 2 ● If in neither branch, signal errorwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. Example ● Encode B with the sample Huffman tree ● Set current_node to the root node ● B is in current_nodes the right branch, so add 1 to encoding & re- curse into the right branch (current_node is set to the root of the right branch – {B, C, D, E, F, G, H}: 9) ● B is in current_nodes left branch, so add 0 to encoding and re- curse into the left branch (current_node is {B, C, D}: 5) ● B is in current_nodes left branch, so add 0 to encoding & recurse into the left branch (current_node is B: 3) ● current_node is a leaf, so return 100 (value of encoding)www.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Message Encoding ● Given a sequence of symbols message and a Huffman tree ht ● Concatenate the encoding of each symbol in message from left to right ● Return the concatenation of encodingswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. Example ● Encode ABBA with the sample Huffman tree ● Encoding for A is 0 ● Encoding for B is 100 ● Encoding for B is 100 ● Encoding for A is 0 ● Concatenation of encodings is 01001000www.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. Message Decoding ● Given a sequence of bits message and a Huffman tree ht, set current_node to the root and decoding to an empty list ● If current_node is a leaf, add its symbol to decoding and set current_node to hts root ● If current_node is hts root and message has no more bits, return decoding ● If no more bits in message & current_node is not a leaf, signal error ● If messages current bit is 0, set current_node to its left child, read the bit, & go to step 2 ● If messages current bit is 1, set current_node to its right child, read the bit, & go to step 2www.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. Example ● Decode 0100 with the sample Huffman tree ● Read 0, go left to A:8 & add A to decoding and reset current_node to the root ● Read 1, go right to {B, C, D, E, F, G, H}: 9 ● Read 0, go left to {B, C, D}:5 ● Read 0, go left to B:3 ● Add B to decoding & reset current_node to the root ● No more bits & current_node is the root, so return ABwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. String Methodswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Useful String Constants ● string.digits – string with all digits 0 - 9 ● string.ascii_letters – string with all lower case and upper case ascii letters ● string.lowercase – string containing all lowercase letters ● string.uppercase – string containing all uppercase letters ● string.punctuation – string containing all punctuation characterswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. Sample String rumi_verse = Someone who makes a habit of eating clay gets mad when you try to keep him from it. Being a leader can also be a poisonous habit, so that when someone questions your authority, you think, "Hes trying to take over." You may respond courteously, but inside you rage. Jalaladdin Rumi, "The Mouse and the Camel" Translated by Coleman Barks www.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. find() >>> rumi_verse.find(who) ## where 1st who starts 9 >>> rumi_verse.find(") ## where 1st double quote starts 188 >>> rumi_verse.find(", 189) ## specifying optional start 214 >>> rumi_verse[189:214] ## slicing the quoted string "Hes trying to take over."www.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. find() ● When a string is not found, -1 is returned >>> rumi_verse.find($$$) ## no dollar signs in Rumi -1 ● Both start and end of search range can be specified >>> rumi_verse.find(poisonous habit, 110, 150) 114www.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. split() ● split() is used to split strings with specific characters ● The argument to split() is the sequence of characters (typically one character) by which the string is split into substrings ● The result is a list of strings ● The argument to split() is not included in any of the stringswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. split() ● Splitting rumi_verse into individual words by white space >>> rumi_verse.split( ) ● Splitting rumi_verse into individual lines >>> rumi_verse.split(n) ● Splitting rumi_verse into two strings by and >>> rumi_verse.split(and)www.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. join() ● join() is the reverse of split(): given a list of strings of strings join them with the sequence of characters on which join() is called as a method on its iterable argument >>> *.join((a, b, c)) a*b*c >>> &&.join([music, math, poetry]) music&&math&&poetry ● Splitting rumi_verse into individual lines and joining them together with n gets us the equivalent of the original string >>> rumi_verse == n.join(rumi_verse.split(n)) Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. replace() ● replace() replaces all occurrences of one string with another string >>> someone who makes a habit of eating dirt.replace(dirt, clay) someone who makes a habit of eating clay >>> 101010.replace(0, zero) 1zero1zero1zerowww.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. translate() ● translate() is similar to replace() but can perform multiple substitutions simultaneously ● translate() accepts a translation table created with maketrans() ● maketrans() takes two strings of the same length: each character in the first string will be replaced by the character in the same position in the second stringwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. translate() >>> import string >>> tbl = string.maketrans(abcd, 2358) >>> efg abcd efg.translate(tbl) efg 2358 efg >>> rumi_verse.translate(tbl) nSomeone who m2kes 2 h23it of e2ting 5l2yngets m28 when you try to keep him from it.nBeing 2 le28er 52n 2lso 3e 2 poisonous h23it,nso th2t when someone questions your 2uthority,nyou think, "Hes trying to t2ke over."nYou m2y respon8 5ourteously, 3ut insi8e you r2ge.nn J2l2l288in Rumi, "The Mouse 2n8 the C2mel"n Tr2nsl2te8 3y Colem2n B2rksnwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. Dictionarieswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. Dictionary ● Dictionary is a set of key-value pairs ● Dictionaries are mutable ● Dictionary is the only type built-in mapping data structure ● The keys are unorderedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. Basic Syntax ● Dictionary is defined by { } emptyDict = {} ## defines empty dictionary ● A key-value pair is defined as key colon value dict = {one : 1} ● Multiple key-value pairs are separated by commas dict = {one : 1, two : 2, three : 3}www.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. Keys and Values ● Keys can be any immutable objects: numbers, characters, tuples, and strings ● Lists and dictionaries cannot be keys ● Values can be any objects ● Dictionaries can be nested, i.e., there can be a dictionary within another dictionarywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. Example box = {size : {height : 10, width : 20}, isCardboard : True, color : red, contents : [nail, hammer, screw]}www.youtube.com/vkedco www.vkedco.blogspot.com
  • 27. Access >>> box[size] # must retrieve on key that exists {width: 20, height: 10} >>> box[size][width] 20 >>> box[contents] [nail, hammer, screw] >>> box[contents][-1] screwwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 28. Access >>> box.hasKey(size) True >>> box.items() # do it only on small dictionaries [(color, red), (isCardboard, True), (contents, [nail, hammer, screw]), (size, {width: 20, height: 10})] >>> box.items()[0] (color, red) >>> box.items()[0][-1] redwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 29. Adding Key-Value Pairs ● You can add new key value pairs to the previously created dictionary my_dict = {} for c in abcdefg: my_dict[c.upper()] = c >>> my_dict {A: a, C: c, B: b, E: e, D: d, G: g, F: f} >>> my_dict[(1, 2)] = [1, 2] >>> my_dict[(1, 2)] [1, 2]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 30. Reading & References ● www.python.org ● Ch 02, H. Abelson and G. Sussman. Structure and Interpreta- tion of Computer Programs, MIT Press ● S. Roman, Coding and Information Theory, Springer-Verlag ● Ch 03, M. L. Hetland. Beginning Python From Novice to Pro- nd fessional, 2 Ed., APRESS ● Ch 04, M. L. Hetland. Beginning Python From Novice to Pro- fessional, 2nd Ed., APRESSwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 31. Feedback Bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com

×