Homework 3 covers exercises in String Manipulation.
For a list of features supported in the string module, please refer to this URL https://docs.python.org/2/library/string.html
You will need to download this notebook and use this as a starting point for your homework. You will just need to fill in the content of each code-block (cell) and execute. Once you have completed all the exercises, you will need to save and upload this to your github repository under a folder called hw3.
Note also the exercises build on top of one another so you might be able to do the next exercise if you have not completed the previous exercise.
Post any questions you have on our Slack at cis-024c1.slack.com
https://github.com/cis024c/fall2017classwork/blob/master/week3/week3_classwork.ipynb
Slides for Week 3 can be found at https://docs.google.com/presentation/d/16z1-Ln71MiXMRfgnZJB60mnXWnwCiUn0gHURe9KMMWg/edit?usp=sharing
Please refer back to hw1 and slack for instructions on how to setup your computer for developing using Python.
Below are some useful commands to know when using Jupyter
In [ ]:
!python --version
Refer to Week 2 classwork 2 for sample exercises - https://github.com/cis024c/fall2017classwork/blob/master/week2/week2.ipynb
Get the ages of three persons Harry, Sally and Mary from the user. Check the below conditions and display the results
Remember that to do this you will need to use different variables to store the respective ages and then evaluate those ages using the if statement and logical operators.
In [1]:
### YOUR CODE GOES
harryAge = int(raw_input("Enter Harry's Age: "))
sallyAge = int(raw_input("Enter Sally's Age: "))
maryAge = int(raw_input("Enter Marry's Age: "))
if harryAge < 20 and sallyAge < 20:
print "Harry and Sally are less than 20 years old"
else:
if sallyAge > 30 or maryAge > 30:
print "Either Sally or Mary is older than 30"
### END CODE
In [2]:
### YOUR CODE GOES BELOW
userName = raw_input("Enter your First Name: ")
lengthString = len(userName)
print lengthString
### END CODE
In [82]:
### YOUR CODE GOES BELOW
favMovie = raw_input("Enter the name of your favorite movie: ")
print favMovie
reversedStr = favMovie[::-1]
print reversedStr
### END CODE
Ask the user to enter a line of text and a search string. Convert the line of text that the user entered to lower case. Search the resulting text for the search string. Print "Search String Found" if the search string was found, otherwise, print "Search String not found"
For example, the user could enter "Jack and Jill went up the Hill" and the search string "jill". You first need to convert the input string to lower case like so - "jack and jill went up the hill".
Next you will need to look for the search string in the input string. You can use the "if searchString in text" form of query to determine if the text contains the search string. See week 3 classowork for an example
In [18]:
### YOUR CODE GOES BELOW
stringName = raw_input("Enter string:")
searchString = raw_input("Enter a search string: ")
stringNameLower = stringName.lower()
print "This the user string in lowercase:", stringNameLower
stringLen = len(stringNameLower)
searchStringLen = len(searchString)
found = False
for index in range(0,stringLen-searchStringLen+1):
#print("substring:",stringName[index:index+searchStringLen])
subString = stringNameLower[index:index+searchStringLen]
if searchString == subString:
print "We have found our searchString:",searchString
found = True
break
if found == False:
print "Sorry, could not find our searchString:",searchString
### END CODE
Ask the user to type in a grocery list. Ensure that each item in the grocery list is separated by a comma. Use the split command to extract each token (item) in the grocery list. Print the last item in the list.
For example, if the user enters "milk,bananas,sugar,eggs,cheese", you will need to read this into a variable, parse the contents using the split command and print "cheese"
In [46]:
### YOUR CODE GOES BELOW
userList = raw_input("Enter your grocery list: ")
myList = userList.split(",")
print theType
lengthList = len(myList)
print myList[-1]
#print myList
### END CODE
Ask the user to type in a grocery list and a search item. Ensure that each item in the grocery list is separated by a comma and an arbitrary number of spaces. Use the split command to search for the search item in this list.
For example, let us say that the user enters ""milk , bananas, sugar, eggs, cheese " (notice the arbitrary spaces between items) and the search term is "eggs". You will need to look for "eggs" in the grocery list and if found, print the message "Item found", otherwise, print "Item not found"
In [9]:
### YOUR CODE GOES BELOW
### END CODE
In [13]:
### YOUR CODE GOES BELOW
### END CODE
Accept a line of text from the user with some repeating words. Ask the user to enter a search term (one of the repeating words). Count the number of times the search term repeats in the text.
For example, if the sentence is - "She sells sea shells on the sea shore" and the search term is "sea", then the program should print the result 2, indicating that two occurrences of the word "sea" were found in the text
In [14]:
### YOUR CODE GOES BELOW
### END CODE
In [15]:
### YOUR CODE GOES BELOW
### END CODE
In [ ]:
### YOUR CODE GOES BELOW
### END CODE