NPTEL » Programming, Data Structures And Algorithms Using Python

Week 2 Programming Assignment



Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.
  • You may define additional auxiliary functions as needed.
  • In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
  • For each function, there are normally some public test cases and some (hidden) private test cases.
  • "Compile and run" will evaluate your submission against the public test cases.
  • "Submit" will evaluate your submission against the hidden private test cases. There are 10 private test cases, with equal weightage. You will not get any feedback about which private test cases pass or fail.
  • Ignore warnings about "Presentation errors".

1. Write a function intreverse(n) that takes as input a positive integer n and returns the integer obtained by reversing the digits in n.
Here are some examples of how your function should work.
 >>> intreverse(783)
  387
  >>> intreverse(242789)
  987242
  >>> intreverse(3)
  3
2.Write a function matched(s) that takes as input a string s and checks if the brackets "(" and ")" in sare matched: that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it. Your function should ignore all other symbols that appear in s. Your function should return True if s has matched brackets and False if it does not.
Here are some examples to show how your function should work.
  1. >>> matched("zb%78")
      True
      >>> matched("(7)(a")
      False
      >>> matched("a)*(?")
      False
      >>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)")
      True
    >>> sumprimes([3,3,1,13])
      19
      >>> sumprimes([2,4,6,9,11])
      13
      >>> sumprimes([-3,1,6])
      0
    
    
    Ans:
    def intreverse(n):
      rev=0
      while(n>0):
       rem=n%10
       rev=(rev*10)+rem
       n=n//10
      return(rev)
    
    def matched(s):
        nesting = 0
        for c in s:
            if c == '(':
                nesting += 1
            elif c == ')':
                nesting -= 1
            if nesting < 0:
                return(False)
        return(nesting == 0)
    
      
    def sumprimes(l):
      c=0
      for j in range(0,len(l)):
        if l[j]>1:
          for k in range(2,l[j]):
            if l[j]%k==0:
              break
          else:
            c=c+l[j]
      return(c)
    
    

3 comments

  1. Thanks for posting such a Useful information .You have done a great job.
    Python Training in Hyderabad

    Python Training

    ReplyDelete
  2. Appreciate on very helpful sharing. Codemic is a YouTube channel with videos on gaming, programming, and vintage tech enthusiast. I make all sort of fun videos on just about everything that comes to mind. Why don't you stay awhile?

    ReplyDelete
  3. Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge as updated, keep blogging. Thank you for sharing wonderful information with us to get some idea about that content.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...