Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Programming, Data Structures And Algorithms Using Python July 2022

 

NPTEL » Programming, Data Structures And Algorithms Using Python



Week 2 Programming Assignment

Due on 2022-08-11, 23:59 IST
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 get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
  • 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 s are 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.

     
    >>> matched("zb%78")
    True
    >>> matched("(7)(a")
    False
    >>> matched("a)*(?")
    False
    >>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)")
    True
    
  3. Write a function sumprimes(l) that takes as input a list of integers l and retuns the sum of all the prime numbers in l.

    Here are some examples to show how your function should work.

    >>> sumprimes([3,3,1,13])
    19
    >>> sumprimes([2,4,6,9,11])
    13
    >>> sumprimes([-3,1,6])
    0
    


Your last recorded submission was on 2022-08-10, 19:52 IST
Select the Language for this assignment. 
1
def intreverse(n):
  r=0
  while(n != 0):
    r=r*10+n%10
    n=n//10
  return(r)

def matched(s):
  sum=0
  for i in range(len(s)):
    if(s[i]=='('):
      sum+=1
    elif(s[i]==')'):
      sum-=1
    if(sum<0):
      break
  if(sum==0):
    return True
  else:
    return False
  
  
def factors(n):
  factorlist = []
  for i in range(1,n+1):
    if n%i == 0:
      factorlist.append(i)
  return(factorlist)

def isprime(n):
    return(factors(n) == [1,n])
  
def sumprimes(l):
  sum=0
  for i in range(len(l)):
    if(isprime(l[i])):
       sum+=l[i]
  return (sum)
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
intreverse(368)
863\n
863\n
Passed
Test Case 2
intreverse(798798)
897897\n
897897\n
Passed
Test Case 3
intreverse(7)
7\n
7\n
Passed
Test Case 4
matched("(7)(a")
False\n
False\n
Passed
Test Case 5
matched("a)*(?")
False\n
False\n
Passed
Test Case 6
matched("((jkl)78(A)&l(8(dd(FJI:),):)?)")
True\n
True\n
Passed
Test Case 7
sumprimes([17,51,29,39])
46\n
46\n
Passed
Test Case 8
sumprimes([-3,-5,3,5])
8\n
8\n
Passed
Test Case 9
sumprimes([4,6,15,27])
0\n
0\n
Passed


















































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.