Upcoming SlideShare
×

Python & Perl: Lecture 07

931

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
931
On Slideshare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: Lecture 07

1. 1. Python & Perl Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Outline ● Encoding & Decoding Messages with Huffman Trees ● String Methods ● Dictionarieswww.youtube.com/vkedco www.vkedco.blogspot.com
3. 3. Encoding & Decoding Messages with Huffman Treeswww.youtube.com/vkedco www.vkedco.blogspot.com
4. 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. 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. 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. 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. 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. 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. 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
12. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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