More on Strings & Arrays 02-201
Indexing Strings Strings work like arrays in some ways: Strings have fixed length. You can find the length of string s with len(s). You can access elements of string s with s[i]. s := Hi There! fmt.println(s[0]) // prints H fmt.println(s[len(s)-1]) // prints! if s[0] == H // single quotes for single symbols H i T h e r e! 0 1 2 3 4 5 6 7 8
Substrings in Go s := Hi There! fmt.println(s[3:5]) // prints Th fmt.println(s[1:]) // prints i There! fmt.println(s[:4]) // prints Hi T var str string = s[3:6] fmt.println(str) fmt.println(str[0]) // prints The // prints T H i T h e r e! 0 1 2 3 4 5 6 7 8
Recall: Counting Pattern in Text 1. Start from the first position of Text and check whether Pattern appears in Text starting at its first position. 2. If yes, draw a dot on a piece of paper. 3. Move to the second position of Text and check whether Pattern appears in Text starting at its second position. 4. If yes, draw another dot on the same piece of paper. 5. Continue until you reach the end of Text. 6. Count the number of dots on the piece of paper.
Example: Implement PatternCount() Pattern Counting Problem: Count the number of times that a pattern appears in a longer string. Input: Strings Text and Pattern. Output: The number of times that Pattern appears in Text. fmt.println(strings.count( ATATA, ATA )) // prints 1, not 2. Exercise: Write a PatternCount() function handling overlapping instances of Pattern solving this problem.
Example: Implement PatternCount() // Count number of times that Pattern appears // in Text as a substring. func PatternCount(Text, Pattern string) int { count := 0 n := len(text) k := len(pattern) for i:=0; i<n-k+1; i++ { if Text[i:i+k] == Pattern { count++ return count
Example: Reverse Complementing a String Reverse Complement Problem: Input: A DNA string Text. Output: The reverse complement of Text. 5 3 A G T C G C A T A G T T C A G C G T A T C A 3 5 Think: How could we solve this problem top-down?
Example: Reverse Complementing a String func ReverseComplement(s string) string { return Reverse(Complement(s)) ReverseComplement() Complement() Reverse()
(Unlike Arrays) Strings are Not Editable! func Complement(s string) string { for i:=0; i<len(s); i++ { switch s[i] { case 'A': s[i] = 'T //ERROR! case 'T': s[i] = 'A //ERROR! case 'C': s[i] = 'G //ERROR! case 'G': s[i] = 'C //ERROR! return s
Reverse Complementing a String func Complement(s string) string { s2 := "" for i:=0; i<len(s); i++ { switch s[i] { case 'A': s2 += 'T // type! case 'T': s2 += 'A // type! case 'C': s2 += 'G // type! case 'G': s2 += 'C // type! return s2
Reverse Complementing a String func Complement(s string) string { s2 := "" for i:=0; i<len(s); i++ { switch s[i] { case 'A': s2 += T case 'T': s2 += A case 'C': s2 += G case 'G': s2 += C return s2 Exercise: Write a Reverse() function reversing a string.
Reverse Complementing a String func Reverse(s string) string { s2 := n := len(s) for i:=0; i<n; i++ { s2 += string(s[n-i-1]) return s2
A Note on Range If s is an array or a string, then the for loop syntax for i:=0; i<len(s); i++ { //... is equivalent to for i:=range s { //...
Slices
Who First Computed Earth s Circumference?
Eratosthenes of Cyrene (276 195 BC) Also: first algorithm for identifying prime numbers
Sieve of Eratosthenes https://en.wikipedia.org/wiki/sieve_of_eratosthenes#/media/file:sieve_of_eratosthenes_animation.gif!
Sieve of Eratosthenes Prime Number Problem: Input: An integer n. Output: All integers p < n such that p is prime. Exercise: Write a program in pseudocode that takes n as input and returns an array iscomposite such that: iscomposite[i] = true if i is composite iscomposite[i] = false if i is prime
Pseudocode for Sieve of Eratosthenes PrimeSieve(n) iscomposite ß array of n false boolean values biggestprime ß 2 while biggestprime < n for i ß 1 to n/biggestprime iscomposite[i*biggestprime] = true biggestprime++ while biggestprime < n and iscomposite[biggestprime] biggestprime++ return iscomposite Think: Is there anything that we can t do here in Go?
Issue with Implementing PrimeSieve() in Go func PrimeSieve(n int) [n+1]bool { var iscomposite [n+1]bool //ERROR! biggestprime := 2 for biggestprime < n for i:=1; i<=n/biggestprime; i++ { iscomposite[i+biggestprime] = true biggestprime++ for biggestprime<n &&iscomposite[biggestprime]{ biggestprime++ return iscomposite Go only allows us to create an array of constant size.