Sunteți pe pagina 1din 3

CMPT 120, Fall 2017 Page 1 of 3

Instructor: Diana Cukierman


Solutions exercises Assignment 4.2, Codewrite #49

def separate(st):
res = ""
for ch in st:
res = res + ch + "**"
return res

def count_digits(st):
count = 0
for i in range(len(st)):
if st[i].isdigit():
count = count + 1
return count

def maxim(st):
mx = -1 # notice that the first digit visited will be greater than -1
for ch in st:
if int(ch) > mx:
mx = int(ch)
return mx

def sumOddCont(st):
tot = 0
for i in range(len(st)):
if st[i].isdigit():
digitAsInt = int(st[i])
if (digitAsInt % 2 == 1):
tot = tot + digitAsInt
return tot

def twoPosTogether(st):
res = ""
for k in range(1,len(st)):
res = res + st[k-1] + st[k] + ".."
return res
CMPT 120, Fall 2017 Page 2 of 3
Instructor: Diana Cukierman
Solutions exercises Assignment 4.2, Codewrite #49

def countVowelsOdd(st):
vowels = "aeiouAEIOU"
count = 0
for i in range(len(st)):
if (i%2 == 1) and (st[i] in vowels):
count = count +1
return count

def strWchangedChar(orig,ch1,ch2):
res = ""
for ch in orig:
if ch == ch1:
res += ch2
else:
res += ch
return res

def remainPos(st,ch,dig):
res = ".."
for i in range(len(st)):
if (ch == st[i]):
remain = i % dig
res += str(remain)+ ".."
return res

def one_each(st1,st2):
minlen = min(len(st1),len(st2))
res = ""
for k in range(minlen):
res = res + st1[k]+st2[k]
return res
CMPT 120, Fall 2017 Page 3 of 3
Instructor: Diana Cukierman
Solutions exercises Assignment 4.2, Codewrite #49

def max_even_digs(st):
maxi = -1
for i in range(len(st)):
if st[i].isdigit():
digI = int(st[i])
if digI %2 == 0:
if digI > maxi:
maxi = digI
return maxi

def sum_products(st):
res = 0
for i in range(1,len(st)):
current = int(st[i])
prev = int(st[i-1])
res = res + current*prev
return res

def max_even_pos(st):
max_so_far = -1
for i in range(0,len(st),2):
current = int(st[i])
if (current > max_so_far):
max_so_far = current
return max_so_far

End of solutions Assignment 4.2

S-ar putea să vă placă și