COMPSCI 101 Principles of Programming
210 likes | 393 Views
COMPSCI 101 Principles of Programming. Lecture 19 – Strings as Sequences Dr. Patricia J. RIddle. Who am I?. My research area is Artificial Intelligence Girls are as good as boys at Computer Science research
COMPSCI 101 Principles of Programming
E N D
Presentation Transcript
COMPSCI 101Principles of Programming Lecture 19 – Strings as Sequences Dr. Patricia J. RIddle
Who am I? • My research area is Artificial Intelligence • Girls are as good as boys at Computer Science research • If you want to talk about either of these topics (or just talk about Computer Science 101) my office hours are: • Friday 1-3pm COMPSCI 101 - Principles of Programming
Learning outcomes • At the end of this lecture, students should be able to: • understand that strings are just sequences • use an index to obtain a character from a string • use loops to iterate through a string • make good test cases COMPSCI 101 - Principles of Programming
Review of return from test Do these two functions print the same output? Why or why not? deffunction1(): print(["fred"]) print(["sally"]) return(["mary"]) def function2(): print(["fred"]) return(["mary"]) print(["sally"]) COMPSCI 101 - Principles of Programming
Review of Strings • Strings and Characters • A string is a SEQUENCE of characters. • String literals can be enclosed in matching single quotes (') or double quotes ("). • Python does not have a data type for characters. • A single-character string represents a character COMPSCI 101 - Principles of Programming
Lists as Sequences • List of strings • fruit_list = ["apple", "banana", "orange"] • fruit_list[3] • len(fruit_list) • List of strings • my_word_list= ["s", "t", "r", "I", "n", "g"] • my_word_list[3] • len(my_word_list) COMPSCI 101 - Principles of Programming
Strings as Sequences • String • my_word= "string” • my_word[3] • len(my_word) COMPSCI 101 - Principles of Programming
Sequences • Lists and strings are examples of a more basic Python type: the sequence • Sequences can be: • Indexed • Iterated over with for loops COMPSCI 101 - Principles of Programming
How to get the first letter of a string? • Just like a list • Which one will work? • my_word[0] • my_word[1] COMPSCI 101 - Principles of Programming
How to get the last letter of a string? • Just like a list • Can use len(my_word) • What does my_word[ len(my_word) ] return? • Can use my_word[ len(my_word)-1 ] • Or can use my_word[-1] COMPSCI 101 - Principles of Programming
Testing • isinstance(x,str) • Returns True if variable x is a string (i.e., is of type “str”) and False otherwise COMPSCI 101 - Principles of Programming
Exercise Write a function named substring() that accepts 2 strings as parameters and prints True if the first string is a substring of the second string and False otherwise. >>> substring("ab", "ab fab") True >>> substring("ab", "fact") False COMPSCI 101 - Principles of Programming
Answer def substring(my_word,my_string): if isinstance(my_word,str) == False: print("my_word is not a string") return False if isinstance(my_string,str) == False: print("my_string is not a string") return False if my_word in my_string: return True return False COMPSCI 101 - Principles of Programming
Exercise Write a function named palindrome() that accepts a string as a parameter and prints True if the string is a palindrome and False otherwise. A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Wikipedia >>> palindrome("otto") True >>> palindrome("fact") False COMPSCI 101 - Principles of Programming
Answer def palindrome(word): if isinstance(word,str) == False: print("word is not a string") return False for index in range(len(word)//2): if word[index] != word[len(word)-1-index]: return False return True COMPSCI 101 - Principles of Programming
Exercise Write a function named heterogram() that accepts a string as a parameter and prints True if it is a heterogram and False otherwise. • A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. • Wikipedia >>> heterogram("otto") False >>> heterogram("fact") True COMPSCI 101 - Principles of Programming
Answer defheterogram(sentence): if not isinstance(sentence,str): print("sentence is not a string") return False found_letters = [] for letter in sentence: if letter in found_letters: return False found_letters += letter return True COMPSCI 101 - Principles of Programming
More Test Cases heterogram("iam") heterogram("iam boy") heterogram("iam a boy") COMPSCI 101 - Principles of Programming
Answer def heterogram2(sentence): if isinstance(sentence,str) == False: print("sentence is not a string") return False found_letters = [] punctuation = [" ", ",", ".", ";", ":", "?", "!"] for letter in sentence: if letter in found_letters: if letter not in punctuation: return False found_letters += letter return True COMPSCI 101 - Principles of Programming
Summary • Learned that Strings and Lists are analogous in many ways, both are the basic Python type Sequence • Learned how to iterate through strings • Learned how to make good test cases COMPSCI 101 - Principles of Programming
Tomorrow • More fun with Boolean Conditions COMPSCI 101 - Principles of Programming