# String Processing --- CS 65 // 2021-03-11 ## Extra Credit Activities - [Study Abroad 101 Info Session](https://calendar.drake.edu/event/study_abroad_101_info_session?utm_source=newsletter&utm_medium=email&utm_content=DETAILS&utm_campaign=Faculty-March-9#.YEe9CxBKh7M) + Friday, March 12 from 10:00am to 10:30am + Virtual Event ## Extra Credit Activities - [The Anderson Gallery](https://linktr.ee/theandersongallery) + 50th Annual Juried Student Exhibition + [Virtual Opening and Awards Ceremony](https://www.youtube.com/watch?v=yvlpDFYudVg) + [Virtual Performance and Lecture](https://www.youtube.com/channel/UCPCCKI0rPk97vpY61BvCK9w/videos) ## Extra Credit Activities - MathCS Special Interest Group + Microsoft Team to help foster community between the students and faculty interested in topics related to Mathematics and Computer Science # Exam 2 ## Exam 2 - Exam 2 is going to be released...later tonight - Be sure to carefully read the [exam procedures]( ../../resources/exam-procedures.html) before starting the exam - It is an **individual**, take-home exam + No discussing with peers, mentors, or tutors + You may only discuss the exam with me ## Tips for the Exam - Program little by little, saving often, and getting one piece to work at a time - Ask for help! + Come talk to me if you're stuck for a while - Test your programs on a variety of inputs - Start early # String Exercises ## Exercise 1 - Write a function `lowercase(s)` that takes a string as a parameter and returns a copy of `s` except each uppercase character is replaced with a lowercase one. - Now write `uppercase(s)`. ## Exercise 2 - Write a function called `substring(s, start, end)` that returns `s[start:end]`---but without using Python's built-in string slicing notation # Files ## Reading from a File - It is possible to read input **from a file** rather than directly from the user using `input(..)` - You can **open** a file for reading by calling ```py f = open("path/to/file", "r") ``` + The `"r"` opens the file for **reading** and the path is relative to the directory of your program ## Reading from a File - Once a file is open, you can **read** text from it using the following methods ```py f = open("my_file.txt", "r") f.read() # Returns the ENTIRE file as a long string f.read(n) # Returns the next n characters as a string f.readline() # Returns the next LINE as a string ``` ## Looping Over a File - A file can be considered a sequence of **lines** separated by newline characters `"\n"` - Therefore, we can loop over a file just like any other sequence: ```py my_file = open("my_file.txt", "r") for line in my_file: print(line) ``` ## Looping Over a File - This `for`-loop notation is shorthand for: ```py my_file = open("my_file.txt", "r") line = my_file.readline() while line != "": print(line) line = my_file.readline() ``` # Exercises ## Exercise 1 - Write a program that reads `words.txt` and prints only the words with more than 20 characters (not counting whitespace). ## Exercise 2 - Write a function called `has_no_e` that returns `True` if the given word doesn’t have the letter `"e"` in it. - Write a program that reads `words.txt` and prints only the words that have no `"e"`. - Compute the percentage of words in the list that have no `"e"`. ## Exercise 3 - Write a function named `avoids` that takes a word and a string of forbidden letters, and that returns `True` if the word doesn’t use any of the forbidden letters. - Write a program that prompts the user to enter a string of forbidden letters and then prints the number of words that don’t contain any of them. ## Exercise 4 - Write a program that reads `words.txt` and prints the **longest word** in the list of words ## Exercise 5 - Write a function called `count_vowels` that returns the number of vowels in a string. - Write a program that reads `words.txt` and prints the word that contains the most vowels. # Looping with Indices ## Looping with Indices - In these previous examples, it was possible to iterate over the strings using a `for` loop such as ```py for ch in s: print(ch) ``` - This can also be written in the following way: ```py for i in range(len(s)): # Loops over indices of s print(s[i]) # s[i] is the current character ``` ## Exercise 6 - Write a function called `is_abecedarian` that returns `True` if the letters in a word appear in alphabetical order (double letters are ok). - How many abecedarian words are there in `words.txt`? # Other Exercises