#write it yourself #return the equivalent temperature of c degree celcius in fahrenheit scale #c is a floating point number #for example, temperature_conversion(50.0) should return 122.0 and so on #def temperature_conversion(c): #write it yourself #return the distance between two points (x1,y1) and (x2,y2) in cartesian coordinate space #x1, x2, y1 and y2 are floating point numbers #def distance(x1,x2,y1,y2): #write it yourself #return the area of the traingle having vertices at (x1,y1), (x2,y2) and (x3,y3) in cartesian space #determine the length of the three sides of the triangle using the distance() function above #then use the formula area=sqrt(s*(s-a)*(s-b)*(s-c)) #x1, x2, x3, y1, y2 and y3 are floating point numbers #def area(x1,x2,,x3,y1,y2,y3): #primality testing #n is an integer greater than 0 #returns true if n is a prime, false if not def isPrime(n): if n==1: return False #1 is not prime by convention for i in range(2,n): if n%i==0: return False return True #prints the prime numbers between 1 to n #uses isPrime() to test for primes #n must be an integer def print_prime(n): for i in range(1,n+1): if isPrime(i)==True: print(i) #write it yourself #print all the divisors/factors of positive integer n #def all_factors(n): #prints the prime factorization of positive integer n #prime factorization means expressing a positive integer as product of power of primes #prime factorization of an integer is unique def prime_factorization(n): div=2 while n>1: cnt=0 while n%div==0: cnt+=1 n//=div if cnt>0: print(div,"^",cnt,sep="",end="") if n!=1: print(" * ",end="") div+=1 print() #write it yourself #return the number of factors of positive integer n #modify the above prime_factorization() function to implement the algorithm described in the link below # http://www.wikihow.com/Find-How-Many-Factors-Are-in-a-Number #dont print anything, just return the number of factors of n #def num_factors(n): #write it yourself #return the gcd(greatest common divisor) of positive integer a and b #def gcd(a,b): #write it yourself #return the lcm(least common multiple) of positive integer a and b #def lcm(a,b): #returns the factorial of positive integer n #iterative version def factorial_1(n): prod=1 for i in range(1,n+1): prod*=i return prod #returns the factorial of positive integer n #recursive version def factorial_2(n): if n<2: return 1 return n*factorial_2(n-1) #write it yourself #return the n-th fibonacci number #google if you are not familiar with fibonacci numbers #def fibonacci(n): #write it yourself #binary to decimal conversion #implement several versions #try both iterative and recursive version #one version may print the binary form but return nothing #another version may return the binary number in integer or string format without printing anything #def decimal_to_binary(n): #write it yourself #generalized number base conversion #convert n represented in base src_base to the equivalent number in base dst_base #for example, base_conversion(123,4,5) should return or print 102 as an integer or string #def base_conversion(n,src_base,dst_base): #implements the slicing operator #returns src_str[start:end] without actually using the slicing operator #src_str is a string, start and end are integers def slicer(src_str,start,end): res="" for i in range(start,end): res+=src_str[i] return res #implements the count() function #returns the number of occurence of the character qry_char in the string src_str def count(src_str,qry_char): cnt=0 for i in src_str: if i==qry_char: cnt+=1 return cnt #write it yourself #implement the upper() method without using the builtin lower() function (for all the functions below, do not use the builtin function provided for the task; write your own version) #return the modified src_str replacing all lower case characters of src_str by their upper case equivalent #for example, upper("Upper123") should return "UPPER123" #closely observe the ordinal values of different characters to convert lowercase characeters to uppercase #the ord() and chr() functions may come handy #def upper(src_str): #implements the find() function using the slicing operator #returns the index of the first occurence of qry_str in src_str #returns -1 if qry_str is not a substring of src_str def find_1(src_str,qry_str): for i in range(0,len(src_str)-len(qry_str)+1): if qry_str==src_str[i:i+len(qry_str)]: return i return -1 #implements the find() function without using the slicing operator #returns the index of the first occurence of qry_str in src_str #returns -1 if qry_str is not a substring of src_str def find_2(src_str,qry_str): for i in range(0,len(src_str)-len(qry_str)+1): flag=True for j in range(len(qry_str)): if qry_str[j]!=src_str[i+j]: flag=False break if flag==True: return i return -1 #write it yourself #implement the in() function #return true if qry_str is a substring of src_str, false otherwise #def in(src_str,qry_str): #removes the first occurence of qry_str in src_str using the slicing operator #returns the modified source string #returns the original source string if qry_str is not found anywhere within src_str def remove_first_1(src_str,qry_str): index=find_1(src_str,qry_str) if index!=-1: return src_str[:index]+src_str[index+len(qry_str):] return src_str #removes the first occurence of qry_str in src_str without using the slicing operator #returns the modified source string #returns the original source string if qry_str is not found anywhere within src_str def remove_first_2(src_str,qry_str): index=find_2(src_str,qry_str) if index!=-1: res="" for i in range(index): res+=src_str[i] for i in range(index+len(qry_str),len(src_str)): res+=src_str[i] return res return src_str #removes all occurences of qry_str in src_str using the remove_first_1() function #returns the modified source string #returns the original source string if qry_str is not found anywhere within src_str def remove_all_1(src_str,qry_str): res=remove_first_1(src_str,qry_str) while res!=src_str: src_str=res res=remove_first_1(src_str,qry_str) return res #removes all occurences of qry_str in src_str by iterative matching using the slicing operator #returns the modified source string #returns the original source string if qry_str is not found anywhere within src_str def remove_all_2(src_str,qry_str): for i in range(0,len(src_str)-len(qry_str)+1): if qry_str==src_str[i:i+len(qry_str)]: src_str=src_str[:i]+src_str[i+len(qry_str):] return src_str #write it yourself #replace the first occurence of qry_str in src_str by dst_str #for example, replace_first("hi this is nothing","is","are") should return "hi thare is nothing" #return the original source string if query string is not found in the source string #def replace_first(src_str,qry_str,dst_str): #write it yourself #replace all occurences of qry_str in src_str by dst_str #for example, replace_all("hi this is nothing","is","are") should return "hi thare are nothing" #return the original source string if query string is not found in the source string #def replace_all(src_str,qry_str,dst_str): #returns the reversed form of the source string #creates the reversed string incrementally by scanning the source string backward def reverse_1(src_str): res="" for i in range(len(src_str)-1,-1,-1): res+=src_str[i] return res #returns the reversed form of the source string #recursive version def reverse_2(src_str): if len(src_str)<2: return src_str return reverse_2(src_str[1:len(src_str)])+src_str[0] #write it yourself #reverse the portion of src_str from index start to end #return the modified source string #for example, range_reverse("abcdefghij",3,6) should return "abcfedghij" #src_str is a string, start and end are integers #def range_reverse(src_str,start,end): #returns true if src_str is a palindrome, false otherwise #palindrome is a string that reads the same forward and backward #this version uses reverse_1() function to check for palindrome def isPalindrome_1(src_str): return src_str==reverse_1(src_str) #returns true if src_str is a palindrome, false otherwise #recursive version def isPalindrome_2(src_str): if len(src_str)<2: return True if src_str[0]!=src_str[len(src_str)-1]: return False return isPalindrome_2(src_str[1:len(src_str)-1]) #returns true if src_str is a palindrome, false otherwise #iterative version def isPalindrome_3(src_str): for i in range(len(src_str)//2): if src_str[i]!=src_str[len(src_str)-1-i]: return False return True #write it yourself #return the src_str rotated right by pos steps #for example, right_rotate("abcdef",2) should return "efabcd", right_rotate("abc",4) should return "cab" #src_str is a string and pos is an integer #def right_rotate(src_str,pos): #write it yourself #transform/encode strings #assume that src_str consists of only lower case characters #replace each character in src_str by the pos-th next character in the alphabet #return the modified source string #for example, encode("afczx",3) returns "difca" (the next character of 'z' is 'a') #the ord() and chr() functions may come handy #src_str is a string and pos is an integer #def encode(src_str,pos): #write it yourself #return the length of the initial portion of src_str which consists only of characters that are part of qry_str #for example, strspn("auibde","aeiou") should return 3 #def strspn(src_str,qry_str): #returns true if qry_str is a subsequence of src_str, false otherwise #a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements #for example, "asd" is a subsequence of "aaasssddd", but "dsa" is not #uses two index variables to track the matched portion of the two strings def subseq(src_str,qry_str): src_index=0 qry_index=0 while True: if qry_index==len(qry_str): return True if src_index==len(src_str): return False while src_str[src_index]!=qry_str[qry_index]: src_index+=1 if src_index==len(src_str): return False src_index+=1 qry_index+=1 #tester of some string funcitons def main(): while True: src=input() if src=="halt": break qry=input() #print(count(src,qry)) #print(find_1(src,qry)) #print(find_2(src,qry)) #print(remove_first_1(src,qry)) #print(remove_first_2(src,qry)) #print(remove_all_1(src,qry)) #print(remove_all_2(src,qry)) #print(reverse_1(src)) #print(reverse_2(src)) #print(isPalindrome_1(src)) #print(isPalindrome_2(src)) #print(isPalindrome_3(src)) #print(subseq(src,qry)) main()