Please use the Python Language, preferabally 2.7, to solve this two probem set a
ID: 3874403 • Letter: P
Question
Please use the Python Language, preferabally 2.7, to solve this two probem set about while loops. The parameters and answer template for the solution is also given beneath the problem descriptions.
#PROBLEM 1
def removeOdd(l1):
list1 = l1
#YOUR CODE GOES HERE (indented)
return l1
#END YOUR CODE
#PROBLEM 2
def getSecond(l1,l2):
string1 = l1
string2 = l2
#YOUR CODE GOES HERE (indented)
return string1
#END YOUR CODE
Explanation / Answer
1.
def removeOdd(l1):
list1 = l1
#YOUR CODE GOES HERE (indented)
l1 = [] #making l1 empty
i = 0 #variable to represent index in list
while i < len(list1): #looping through entire list
if list1[i] % 2 == 0: #if it is odd number
l1.append(list1[i]) #appending to list
i += 1 #incrementing index by 1
return l1 #returning list
#END YOUR CODE
print removeOdd([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
output:
2.
def getSecond(l1,l2):
string1 = l1
string2 = l2
index = -1
#YOUR CODE GOES HERE (indented)
n = len(string2) #finding length of string
i = 0 #variable to represent index
while i < len(string1)-(n-1): #looping though string1 and it should atleast contain n more charcters after i
if string1[i:i+n] == string2: #if string is found
index = i #updating index value
i += 1 #incrementing index by 1
return index
#END YOUR CODE
s1 = raw_input("Enter first String")
s2 = raw_input("Enter second String")
print getSecond(s1,s2)
output: