Professional Documents
Culture Documents
Practice Problems On Function and String
Practice Problems On Function and String
#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
#write it yourself
#print all the divisors/factors of positive integer n
#def all_factors(n):
#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):
#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):
#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):
#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):
#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):
#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):
#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):
main()