def where(s,c): '''Given string s and single-character string c, return the position of the first occurrence of c in s. If c is not in s, return -1.''' # Do not use any string methods, but you can use functions in __builtins__ if not c in s: return -1 else: for i in range(len(s)): if s[i] == c: return i def every_ith(l,i): '''Given list l and int i > 0, return a new list that contains every ith element of l, starting with index 0.''' # You can assume that i is > 0. You don't need to perform error checking on that. new_l = [] pos = 0 while pos < len(l): new_l.append(l[pos]) pos += i return new_l def duplicates(l): '''Return True if list l contains adjacent elements with the same value, and False otherwise''' # Use a while loop rather than a for-loop i = 0 while i+1 < len(l): if l[i] == l[i+1]: return True i += 1 return False print where("abcdeddw x",'d'), "sb", 3 print where("a",'a'), "sb", 0 print where("abcdeddw x",'x'), "sb", 9 print where("abcdeddw x",'z'), "sb", -1 print where("",'z'), "sb", -1 print every_ith([10,11,12,13,14,15,16,17],3), "sb", [10,13,16] print every_ith([10,11,12,13,14,15,16,17],10), "sb", [10] print every_ith([],2), "sb", [] print every_ith([1,2,3],1), "sb", [1,2,3] # You decide on a good set of test cases here for duplicates print duplicates([0,1,2,3,"hello","hello",34.5]),"sb", True print duplicates([0,1,2,3,"hello","hello"]),"sb", True print duplicates([0,0,1,2,3,"hello","hello"]),"sb", True print duplicates([0,0]),"sb", True print duplicates([0]),"sb", False print duplicates([]),"sb", False print duplicates([0,1,2,3]),"sb", False