strings
play

Strings Repetition Hans-Joachim Bckenhauer and Dennis Komm Digital - PowerPoint PPT Presentation

Strings Repetition Hans-Joachim Bckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming Logical Values and Control Structures Autumn 2019 October 17, 2019 Strings Characters The Unicode Table Strings are lists


  1. Strings Repetition Hans-Joachim Böckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming Logical Values and Control Structures Autumn 2019 – October 17, 2019 Strings Characters – The Unicode Table Strings are “lists of characters” (there are differences) 0–18 19–37 38–56 57–75 76–94 95–113 114–127 Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Characters correspond (mostly) to keys on keyboard 0 NUL 19 DC3 38 & 57 9 76 L 95 _ 114 r 1 SOH 20 DC4 39 ’ 58 : 77 M 96 ‘ 115 s 2 STX 21 NAK 40 ( 59 ; 78 N 97 a 116 t Strings are written in quotation marks 3 ETX 22 SYN 41 ) 60 < 79 O 98 b 117 u 4 EOT 23 ETB 42 * 61 = 80 P 99 c 118 v Access to single characters using brackets 5 ENQ 24 CAN 43 + 62 > 81 Q 100 d 119 w 6 ACK 25 EM 44 , 63 ? 82 R 101 e 120 x 7 BEL 26 SUB 45 - 64 @ 83 S 102 f 121 y 8 BS 27 ESC 46 . 65 A 84 T 103 g 122 z String word = "HELLO WORLD" 9 HT 28 FS 47 / 66 B 85 U 104 h 123 { 10 LF 29 GS 48 0 67 C 86 V 105 i 124 | 11 VT 30 RS 49 1 68 D 87 W 106 j 125 } word[0] is first character 12 FF 31 US 50 2 69 E 88 X 107 k 126 ~ 13 CR 32 SP 51 3 70 F 89 Y 108 l 127 DEL word[1] is second character 14 SO 33 ! 52 4 71 G 90 Z 109 m . . . 15 SI 34 "’ 53 5 72 H 91 [ 110 n \ 16 DLE 35 # 54 6 73 I 92 111 o . . . 17 DC1 36 $ 55 7 74 J 93 ] 112 p 18 DC2 37 % 56 8 75 K 94 ˆ 113 q word[len(word)-1] is last character Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 1 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 2 / 38

  2. Characters – The Unicode Table Exercise – Print Characters Write a program that Use functions ord() and chr() outputs the first 26 uppercase letters ord(x) returns position of character x in Unicode table uses a for -loop to this end chr(y) returns character at position y in Unicode table Recall The letter A is located at position 65 in x = input("Enter a character: ") print("The character", x, "is at position", ord(x)) the Unicode table Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 3 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 4 / 38 Exercise – Print Characters Caesar Encryption for i in range(65,91): print(chr(i)) Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 5 / 38

  3. Symmetric Encryption Gallia est omnis divisa in partes Situation tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam, qui ipsorum lingua Celtae, Insecure channel Two parties A and B want to communicate through an insecure channel nostra Galli appellantur. Shared key k A encrypts message with k A sends encrypted message to B B decrypts message with k A is called sender B is called recipient Symmetric: Same key for encryption and decryption Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 6 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 7 / 38 Symmetric Encryption Caesar Encryption Recipient Sender Situation Parties A and B want to communicate over an insecure channel, this time plaintext plaintext using Caesar encryption Shared key k as number between 1 and 25 Encryption Decryption A encrypts message by adding k to each character Communication medium ciphertext ciphertext A sends encrypted message to B (messenger, internet, . . . ) B decrypts message by subtracting k from each character Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 8 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 9 / 38

  4. Caesar Encryption Caesar Encryption 1. Entered letter is Unicode character between A and Z Shift characters by fixed value k by adding k A B . . . W X Y Z 65 66 . . . 87 88 89 90 Example 2. Subtract 65 so that the result is between 0 and 25 A B C D E F G H I J K L M A B . . . W X Y Z W X Y Z A B C D E F G H I 0 1 . . . 22 23 24 25 N O P Q R S T U V W X Y Z 3. Now add key (for instance, 3 ) and compute modulo 26 J K L M N O P Q R S T U V A B . . . W X Y Z 3 4 . . . 25 0 1 2 4. Finally add 65 to the result Plaintext: HELLO WORLD A B . . . W X Y Z Ciphertext: DAHHK SKNHZ 68 69 . . . 90 65 66 67 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 10 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 11 / 38 Euclidean Division (Modulo Operation) Exercise – Caesar Encryption Using “ % , ” we obtain the residue of the integer division Write a program that Analogously, “ // ” the part before the decimal point runs through a given string decrypts each letter with a key k 10 % 3 = 1 , because 9 = 3 · 3 tries out each key k because 8 = 4 · 2 10 % 4 = 2 , uses the following formula because 10 = 5 · 2 11 // 5 = 2 , e = ( v − 65 − k ) % 26 + 65 23 // 4 = 5 , because 20 = 4 · 5 because 12 = 4 · 3 12 % 3 = 0 , Decrypt the ciphertext DLUUQLTHUKSHBAOLBYLRHYBMANPIALZZJOVNNP If x % y == 0 , x is divided by y Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 12 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 13 / 38

  5. Exercise – Caesar Encryption for k in range(0,26): for item in x: print(chr((ord(item) - 65 - k) % 26 + 65), end="") Changing the Step Size print() for k in range(0,26): for i in range(0, len(x)): print(chr((ord(x[i]) - 65 - k) % 26 + 65), end="") print() Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 14 / 38 Loops over Lists – Larger Steps The Syntax of range Traverse a list with steps of length 2 for i in range(start, end, step) Iteration over all positions from start up to end-1 with step length of step data = [5, 1, 4, 3] for i in range(0,len(data), 2): print(data[i]) Shorthand notation for i in range(start,end) ⇐ ⇒ for i in range(start,end,1) Output All elements at even positions from 0 up to at most len(data) are output Another shorthand notation ➯ 5,4 for i in range(end) ⇐ ⇒ for i in range(0, end) Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 15 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 16 / 38

  6. Improvement of Caesar Encryption Use two keys alternatingly for even and odd positions Logical Values k = int(input("First key: ")) Boolean Values and Relational Operators l = int(input("Second key: ")) x = input("Text (only uppercase, even length): ") for i in range(0, len(x), 2): print(chr((ord(text[i])-65 + k) % 26 + 65), end="") print(chr((ord(text[i+1])-65 + l) % 26 + 65), end="") print() Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 17 / 38 Boolean Values and Variables Relational Operators Boolean expressions can take on one of two values F or T a < b (smaller than) F corresponds to “false” a >= b (greater than) T corresponds to “true” a == b (equals) a != b (unequal to) Boolean variables in Python represent “logical values” Domain { False, True } number type × number type → {False, True} Example b = True # Variable with value True George Boole [Wikimedia] Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 18 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 19 / 38

  7. Boolean Functions in Mathematics Boolean function Logical Values f : { F , T } 2 → { F , T } Boolean Functions and Logical Operators F corresponds to “false” T corresponds to “true” Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 20 / 38 x ∧ y Logical Operator and “logical and” x ∧ y x y a and b (logical and) f : { F , T } 2 → { F , T } F F F F T F {False, True} × {False, True} → {False, True} T F F F corresponds to “false” T corresponds to “true” T T T n = -1 p = 3 b = (n < 0) and (0 < p) # b = True Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 21 / 38 Digital Medicine I: Introduction to Programming – Control Structures Autumn 2019 Böckenhauer, Komm 22 / 38

Recommend


More recommend