def setup():
print(myCount("peejalovelove","e"))
print(myFind("Thailand", "i"))
print(myReplace("Thailand", "Thai", "Eng"))
print(myStrip("The Land of Smile"))
assert(startWith("peejalovelove","pee")) == True
assert(endWith("peejalovelove","ja")) == False
def myCount(fullword,char):
count = 0
i = 0
while(i < len(fullword)):
if(fullword[i] == char):
count += 1
i += 1
return count
def myFind(fullword,char):
index = 0
i = 0
while(i < len(fullword)):
if(fullword[i] == char):
index = i
i += 1
return index
def myReplace(fullword,oldword,replace):
newWord = ""
i = 0
while(i < len(fullword)):
if(fullword[i] != oldword[0]):
newWord += fullword[i]
i += 1
else:
num = 0
while(num < len(oldword) and oldword[num] == fullword[i+num]):
num += 1
if(num == len(oldword)):
newWord += replace
i += len(oldword)
return newWord
def myStrip(word):
newWord = ""
i = 0
while(i < len(word)):
if(word[i] != " "):
newWord += word[i]
i += 1
return newWord
def startWith(fullword,startword):
wordCheck = list(fullword)
startCheck = list(startword)
i = 0
while(i < len(startCheck)):
if(startCheck[i] != wordCheck[i]):
return False
i += 1
return True
def endWith(fullword,endword):
wordCheck = list(fullword)
endCheck = list(endword)
i = 0
while(i < len(endCheck)):
if(endCheck[i+(len(endCheck)-1)] != wordCheck[i+(len(wordCheck)-1)]):
return False
i += 1
return True
setup()
ไม่มีความคิดเห็น:
แสดงความคิดเห็น