24
Character Encoding CS 8: Introduction to Computer Science, Spring 2019 Lecture #15 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

CharacterEncodingCS8:IntroductiontoComputerScience,Spring2019

Lecture#15

ZiadMatni,Ph.D.Dept.ofComputerScience,UCSB

Page 2: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

Administrative•  Hw07–DUEONTUESDAY6/4•  Hw08outtoday–DUEONTHURSDAY6/6(lastdayoflecture)

•  Lab06–issued–  DuebynextweekThursdayby11:59PM–  Alittleinvolved,sofeelfreetopair-up(optional)

•  YouarestillworkingonProject#1…right?

5/30/19 Matni,CS8,Sp19 2

Page 3: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

ReviewingYourMidterm#2Exam•  Optional,butrecommendedforyoutounderstandyour

mistakes

•  Ifyou’reinthe8AMlab–gotoChongLiu’sofficehours•  Ifyou’reinthe9AMlab–gotoBrianYoung’sofficehours•  Ifyou’reinthe10AMlab–gotoShaneMasuda’sofficehours•  Ifyou’reinthe11AMlab–gotoProf.Matni’sofficehours

5/30/19 Matni,CS8,Sp19 3

Page 4: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

FinalExamExtraReviewSessionFriday,June7th1:00–3:00PMPHELP2510

(thisisoptional)

5/30/19 Matni,CS8,Sp19 4

Page 5: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

FinalsWeek•  Dr.Matniwillhaveofficehoursonfinalsweek

Monday 1:00pm–2:30pm

5/30/19 Matni,CS8,Sp19 5

Page 6: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

LectureOutline•  ASCIICodes,UTFCodes•  Functionsord()andchr()•  Exercises

5/30/19 Matni,CS8,Sp19 6

Page 7: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go
Page 8: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

UTFCodesUnicodeTransformationFormat

•  ASCIIuses7bitsforitscodes–  Thismeansthereare27(or126)possiblecodes–  PreferredencodingforbasictextfilesintheLatinalphabet

•  UTF-8isanotherstandard–  Uses8bitsforitscodes(so,28=256possibles)–  BackwardscompatiblewithASCII–  Preferredencodingfore-mailandwebpages

•  UTF-16isthe“widest”standard(uses16bits)–  CapableofencodingtheentireUnicoderepertoire.

5/30/19 Matni,CS8,Sp19 8

Page 9: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

UTF-8Schemes

5/30/19 Matni,CS8,Sp19 9

Page 10: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

Functionschr(n)andord(c) •  Charactersarestoredasnumbersincomputermemory

–  Therearestandardcodesforcharacters,e.g.ASCII,UTF-8,etc…

•  Forexample,'A'hascode65inASCII–  Usetheordfunctiontoverifythis:ord('A')is65–  Notice'A'isnotsameas'a': ord('a')is97

•  Everycharacter,seen(e.g.%,!,G,=,space,tab,…)andunseen(e.g.CONTROL-X,newline…)hasanASCIIcode

5/30/19 Matni,CS8,Sp19 10

Page 11: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

Functionschr(n)andord(c) •  Likewise,youcanfindcharacterassociatedwithaparticularcodeusing

chrfunction,forexample: chr(65) is 'A'

•  Youcanmanipulatenumbersinordertoprocesscharacters

chr(ord('a')+3)ischr(97),whichis'd'•  Noticedigitcharactershavecodestoo!

ord('6')is545/30/19 Matni,CS8,Sp19 11

Page 12: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

Examples•  HowcanIfindoutwhat’s13lettersafter‘e’??

–  Easyanswer:recitethealphabetfrom‘e’andcount13places–  Codeanswer:chr(ord('e')+13),whichis'r'

•  HowcanIfindoutwhat’s19lettersbefore‘Z’??–  Codeanswer:chr(ord('Z')-19),whichis'G'

•  What’stheASCIIcodeforthehashtagcharacter??–  Codeanswer:ord('#'),whichis35

5/30/19 Matni,CS8,Sp19 12

Page 13: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

HarderExample…•  HowcanIdoan(not-found-in-Python)

“addition”of2numeralstrings,like‘3’and‘4’andget‘7’??

•  Firstask:howcanImake‘3’into3? (HINT:We’llneedabaseline…)•  Thatbaselineisord(‘0’) ---howfarawayintheASCIIis‘3’from‘0’???•  Notethat:ord(‘3’)–ord(‘0’)=3•  Sothe“addition”isdonelikethis:

(ord(‘3’)–ord(‘0’))+(ord(‘4’)–ord(‘0’))=7(anint)

or,ord(‘3’)+ord(‘4’)–2*ord(‘0’)=7

5/30/19 Matni,CS8,Sp19 13

Page 14: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

SoICanCreateaFunctiontodoThis!defaddChars1(char1,char2):

numAddASCII=ord(char1)+ord(char2)–2*ord('0')returnnumAddASCII #Returnsaninteger

ImportantCaveat!Onlyworkswith1characternumbers!

5/30/19 Matni,CS8,Sp19 14

Page 15: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

WhatifIWantedtoReturnaStringResult?defaddChars2(char1,char2):

numAddASCII=ord(char1)+ord(char2)–2*ord('0')charNum=chr(numAddASCII+ord('0'))returncharNum #Returnsastring

ImportantCaveat!Again,onlyworkswith1characternumbers!

5/30/19 Matni,CS8,Sp19 15

Page 16: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

Exercise1•  CreateafunctionMyCipher(myStr)–takesastringargument

•  Makeseveryletterbecometheletterafterit–  Letter‘a’becomes‘b’,‘b’becomes‘c’,etc…–  Sothat“hello”becomes“ifmmp”(encryption)

•  Relatedquestion:Howwouldyoudecryptthis?

5/30/19 Matni,CS8,Sp19 16

Page 17: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

MyCipher()anditsReversedefMyCipher(myStr):enc_str=''forcinmyStr:enc_str+=chr(ord(c)+1)returnenc_str

defReverseMyCipher(myStr):dec_str=''forcinmyStr:dec_str+=chr(ord(c)-1)returndec_str

5/30/19 Matni,CS8,Sp19 17

Page 18: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

Exercise2MirroredAlphabet (or“thefirstshallbethelast”)•  Theletters a,b,c,d,…w,x,y,z maponto

z,y,x,w,…d,c,b,a

•  Sothat “bye”becomes“ybv” and“maria”becomes“nzirz” and“abcdef”becomes“zyxwvu”

•  Howwouldyoudecryptthis?•  Wouldyousaythisisasymmetricencryptionscheme?5/30/19 Matni,CS8,Sp19 18

Page 19: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

MirroredAlphabetCipher•  Let’sexaminethethinkingbehindthis:

a,b,c,d,e,f,g,….,w,x,y,zmapsonto

z,y,x,w,v,u,t,…..,d,c,b,a

5/30/19 Matni,CS8,Sp19 19

Page 20: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

OurAlgorithm1.  Givenastring(message)withNnumberofletters

2.  Gothrueveryletterinordertoexamineit(how?)

3.  Apply“mappingformula”toeachletter

(don’tknowwhatthat“formula”isyet,butthat’sok…)

4.  Onceformulaisapplied,

“gatherupthenewletters”intoaNEWstring(how?)

5.  ReturnthatNEWstringastheencodedmessage

5/30/19 Matni,CS8,Sp19 20

Page 21: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

MirrorEncrypt()defMirrorEncrypt(message):#messageisastringtyperesult=''#startwithanemptyresultforcinmessage:#gothrueveryletterinmessage#let’sapplythe“mirror”formula:nc=ord(c)nr=ord('a')+ord('z')-nc

#thenaccumulatetheencodedchars,oneatatimeresult=result+chr(nr)returnresult

5/30/19 Matni,CS8,Sp19 21

Page 22: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

MirrorEncrypt()Questions•  WhathappensifItry

MirrorEncrypt(MirrorEncrypt("cat"))?–  Why?

•  WhathappensifItry MirrorEncrypt("CAT")?–  Why?

5/30/19 Matni,CS8,Sp19 22

Page 23: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

YOURTO-DOsq  HW7(dueonTuesday,6/4)q  HW8(dueonThursday,6/6)

q  Lab6(dueonThursday,6/6)q  ProjectAssignment(dueonSunday,6/2)

5/30/19 Matni,CS8,Sp19 23

Page 24: CS8 Lecture15 CharEncoding · 2019-09-25 · Reviewing Your Midterm #2 Exam • Optional, but recommended for you to understand your mistakes • If you’re in the 8 AM lab – go

5/30/19 Matni,CS8,Sp19 24