# Some functions that uses strings #Strings versus arrays. A = ["a","b","c"] S = "abc". S[2] # ok len(S) # ok # S[2] = "e" # not ok. # S.append("d") # not ok S = S+"c" S = S+str(3) S = "aa"+S # OK. "+" is non-destructive. # Strings as objects S = "oldold" S = S.replace("old","new") # gives "newnew" # Strings, unlike arrays, are immutable. # function to reverse a string def revstr(S): R = "" # string to be built i = 0 while i=65 and asc<=90): # upper case asc = asc+32 # asc now code for a lower-case character lc = chr(asc) #convert code back to character L = L+lc # add char to string #while return L #lowercase # function to test if two strings are case-insensitively equal: # (semi-class exercise) def ciequals(A,B): la = lowercase(A) lb = lowercase(B) return la==lb # same as if la==lb: return True # else: return False #ciequals ##### This is an example of a BOOLEAN FUNCTION: returns True or False # Sometimes, it is convenient to translate a string into an array of # chars, change it, then convert it back. We will use these functions # a lot: def strToArray(S): # convert (return) array of chars to represent S A = [] i = 0 while i0: d = x % 2 S = str(d) + S x = x/2 #while return S #intTobin # try it on 23 def binToint(S): answer = 0 i = len(S)-1 power = 0 while i>=0: if S[i]=="1": answer += 2**power power +=1 i = i-1 #while return answer #btoi