Programming Data Structures And Algorithms Using Python Coding Assignment 2 Answers 2022 Week 2 Programming Assignment

 

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 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])
    
Answer Code:

def intreverse(n):
  ans = 0
  while n > 0:
    (d,n) = (n%10,n//10)
    ans = 10*ans + d
  return(ans)

def matched(s):
  nested = 0
  for i in range(0,len(s)):
    if s[i] == "(":
       nested = nested+1
    elif s[i] == ")":
       nested = nested-1
       if nested < 0:
          return(False)    
  return(nested == 0)

def factors(n):
  factorlist = []
  for i in range(1,n+1):
    if n%i == 0:
      factorlist = factorlist + [i]
  return(factorlist)

def isprime(n):
  return(factors(n) == [1,n])


def sumprimes(l):
  sum = 0
  for i in range(0,len(l)):
    if isprime(l[i]):
      sum = sum+l[i]
  return(sum)



No comments

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...