5 ตุลาคม 2558

Lab5 Final

def setup():
   text1 = "Thailand  "
   print(text1)
   assert my_count(text1) ==2
   print("my_count",my_count(text1))
   assert my_find(text1) ==3
   print("my_find",my_find(text1))
   assert my_replace(text1) == "Thaithai"
   print("my_replace",my_replace2(text1))
   print("my_strip",my_strip(text1))
   print("my_startswith",my_startswith(text1))
   print("my_endswith",my_endswith(text1))

def my_count(text):
   find = "a"
   countFind = 0
   i = 0
   while(i<len(text)):
      if(find == text[i]):
         countFind +=1
      i+=1
   return countFind

def my_find(text):
   find = "i"
   indexFind = 0
   i = 0
   while(i<len(text)):
      if(find == text[i]):
         indexFind = i
      i+=1
   return indexFind

def my_replace(text):
   find = "land"
   replace = "thai"
   newText = ""
   i = 0
   i_find = 0
   while(i<len(text)):
      if(text[i]==find[i_find]):
         checkAll = 0
         while (checkAll < len(find)):
            if(find[i_find]==text[i+i_find]):
               i_find+=1
               checkAll+=1
            else:
               newText = newText+text[i]
               i_find = 0
               checkAll = len(find)
            if(i_find==len(find)):
               n = 0
               m = len(replace)
               i_replace = 0
               while(n < m):
                  newText = newText+replace[i_replace]
                  i_replace +=1
                  n+=1
               i +=len(find)-1
               i_find = 0
      else :
         if(text[i] != " "):
             newText = newText+ text[i]
      i+=1
   return newText

def my_strip(text):
   newText = ""
   i=0
   while(i < len(text)):
      if(text[i] != " "):
            newText = newText+ text[i]
      i+=1
   return newText

def my_startswith(text):
   find = "Sun"
   i = 0
   while(i < len(text)):
      if(find[i] == text[i]):
         j =0
         while(j < len(find)):
            if(text[i+j] == find[j]):
               j +=1
               if (j == len(find)):
                  return True
            else :
               return False
      else :
          return False

def my_endswith(text):
   find = "and"
   i = len(text)-1
   i_find = len(find)-1
   while(i > 0):
      if(find[i_find] == text[i]):
         checkAll = 0
         while(checkAll < len(find)):
            if(text[i-checkAll] == find[i_find-checkAll]):
               checkAll +=1
               if (checkAll == len(find)):
                  return True
            else :
               return False
      else :
          return False

setup()
       

ไม่มีความคิดเห็น:

แสดงความคิดเห็น