# Template file for Homework 10, CS 151, Fall 2010, ISU
#
# Name: (put your name here)
#


# This first line has JES load all the functions from the
# cs151functions.py file so we can use them in this file too.
# So we will have this as the first line in our Python files from
# now on.
#
# Make sure to download the latest version of cs151functions.py from the 
# course webpage (make sure to right-click and "Save target as" or 
#  "Save link as" or whatever your browser calls it.)
#
# And then do the following in the command area, and select
# the folder that has cs151functions.py in it:
#
# >>> setLibPath(pickAFolder())
from cs151functions import *



#
# Problem 1 (-3 points if you do not fill this in.)
#
def collaborators():
  # put into s who you worked with on this homework, "none" if none.
  s = ""
  print "I collaborated with:", s
# ** Note on collaboration: You should not turn in work that is not your own.
#    To make sure you are not violating this, if you work with someone, 
#    you should destroy the file once you are done working together, go 
#    your separate ways, and then you should be able to recreate the file
#    on your own, then turn that in.  If you cannot recreate the file on your
#    own, then it is not your work, and you should not turn it in.
# ** Note on sources: if you use some other source, the web or whatever, you
#    better cite it!  Not doing so is plagiarism.




# Problem 2 (5 Points)
#
# This function should find the index of the first space, and 
#  should return -1 if there is no space in the string.
#
# So the function would behave like this:
# >>> findFirstSpace("Hello there.")
#  5
# >>> findFirstSpace("Why must we do this?") 
#  3
# >>> findFirstSpace("Red")
#  -1
def findFirstSpace(s):
  index = -1
  
  # put code here to set index to the first space in the string s.
  
  return index


# Problem 3 (5 Points)
#
# This function should print out the first word in the string.  It 
# should use the findFirstSpace function to find the first space, and 
# then pick out the letters in the string up until that character.
#
# So the function would behave like this:
# >>> firstWord("Hello there.")
#  'Hello'
# >>> firstWord("Red")
#  'Red'
def firstWord(s):
  index = findFirstSpace(s)
  word = ""
  
  # put code here to finish the function
  
  return word
  
  
# Problem 4 (15 Points)
#
# In this function, you should improve the function we did in 
# class that counts the number of times a certain word occurs in the 
# string.  You should fix the following two problems:
# (1) The code from class is case sensitive, so it counts "Is" as a different
#     word than "is".  To fix this, I suggest changing the string and the word
#     to both be lowercase at the beginning of the function.
# (2) The code from class counts "this is" as having the word is twice.
#     To fix this, I suggest that when you find the word, you check to see if 
#     the previous character in the string is not a letter.  If the previous letter
#     before the word is a space, period, etc., then you really have a word.
#
def numTimesBetter(s, word):
  # put some code here to change s and word to be lowercase.
  stillToLookIn = s
  count = 0
  while (stillToLookIn != ""):
    index = stillToLookIn.find(word)
    if (index < 0):
      stillToLookIn = ""
    else:
      # put some code here to check if the previous character is a non-letter character, 
      # and only increase count if so.
      count = count + 1
      stillToLookIn = stillToLookIn[index+len(word):]
  print "Found the word this many times:", count
  return count