1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | def setup(): print(my_replace("Thailand","land","thai")) def my_replace(t,s,r): i=0 text="" while i<len(t): if check(t,s,i): text+=r i+=len(s) elif i<len(t): text+=t[i] i+=1 return text def check(t,s,index): i=0 count=0 while i<len(s) and index+i<len(t): if t[index+i]==s[i]: count+=1 if count==len(s): return True i+=1 return False setup() |
วันพุธที่ 30 กันยายน พ.ศ. 2558
Lab05 : String Replace
วันจันทร์ที่ 28 กันยายน พ.ศ. 2558
Lab05 : String related
def setup():
assert my_count("Thailand","a")==2
assert my_find("Thailand","i")==3
print(my_strip("Thailand"))
print(my_startswith("Thailand","Sun"))
print(my_endswith("Thailand","and"))
def my_count(s,a):
i=0
count=0
while i<len(s):
if s[i]==a:
count+=1
i+=1
return count
def my_find(s,a):
i=0
while i<len(s):
if s[i]==a:
return i
i+=1
def my_strip(s):
i=0
rs=""
while i<len(s):
if s[i]!=" ":
rs+=s[i]
i+=1
return rs
def my_startswith(s,a):
i=0
count=0
while i<len(a):
if s[i]==a[i]:
count+=1
i+=1
if count==len(a):
return True
else:
return False
def my_endswith(s,a):
i=len(s)-1
j=len(a)-1
count=0
while j>=0:
if s[i]==a[j]:
count+=1
i-=1
j-=1
if count==len(a):
return True
else:
return False
setup()
assert my_count("Thailand","a")==2
assert my_find("Thailand","i")==3
print(my_strip("Thailand"))
print(my_startswith("Thailand","Sun"))
print(my_endswith("Thailand","and"))
def my_count(s,a):
i=0
count=0
while i<len(s):
if s[i]==a:
count+=1
i+=1
return count
def my_find(s,a):
i=0
while i<len(s):
if s[i]==a:
return i
i+=1
def my_strip(s):
i=0
rs=""
while i<len(s):
if s[i]!=" ":
rs+=s[i]
i+=1
return rs
def my_startswith(s,a):
i=0
count=0
while i<len(a):
if s[i]==a[i]:
count+=1
i+=1
if count==len(a):
return True
else:
return False
def my_endswith(s,a):
i=len(s)-1
j=len(a)-1
count=0
while j>=0:
if s[i]==a[j]:
count+=1
i-=1
j-=1
if count==len(a):
return True
else:
return False
setup()
สมัครสมาชิก:
บทความ (Atom)
commit เพิ่มเติม : เพื่ม userstory, bookmark
>> add userstory ทำการเพิ่ม userstory ใน functional tests >> add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...
-
1 2 3 4 5 6 7 8 9 def setup (): print (d2b( 8 )) def d2b (num): x = "" while num > 0 : x = str ( int (n...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 def test_layout_and_styling ( self ): # Edith goes to the home page self . brow...
-
การใช้ template inheritence คือการสืบทอดเทมเพลต เพื่อให้ไม่ต้องเขียนโค้ดหน้าเว็บๆซ้ำๆ เราจะมีหน้าหลักอันเดียว (base.html or parent) ภายในนั...