Week 4 Programming Assignment

Write two Python functions as specified below. Paste the text for both 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 Python function frequency(l) that takes as input a list of integers and returns a pair of the form (minfreqlist,maxfreqlist) where
    • minfreqlist is a list of numbers with minimum frequency in l, sorted in ascending order
    • maxfreqlist is a list of numbers with maximum frequency in l, sorted in ascending
    For instance
    >>> frequency([13,12,11,13,14,13,7,11,13,14,12])
    ([7], [13])
    
    >>> frequency([13,12,11,13,14,13,7,11,13,14,12,14,14])
    ([7], [13, 14])
    
    >>> frequency([13,12,11,13,14,13,7,11,13,14,12,14,14,7])
    ([7, 11, 12], [13, 14])
    

  2. An airline has assigned each city that it serves a unique numeric code. It has collected information about all the direct flights it operates, represented as a list of pairs of the form (i,j), where i is the code of the starting city and j is the code of the destination.
    It now wants to compute all pairs of cities connected by one intermediate hop --- city i is connected to city j by one intermediate hop if there are direct flights of the form (i,k) and (k,j) for some other city k. The airline is only interested in one hop flights between different cities --- pairs of the form (i,i) are not useful.
    Write a Python function onehop(l) that takes as input a list of pairs representing direct flights, as described above, and returns a list of all pairs (i,j), where i != j, such that i and j are connected by one hop. Note that it may already be the case that there is a direct flight from i to j. So long as there is an intermediate k with a flight from i to k and from k to j, the list returned by the function should include (i,j). The input list may be in any order. The pairs in the output list should be in lexicographic (dictionary) order. Each pair should be listed exactly once.
    For instance
    >>> onehop([(2,3),(1,2)])
    [(1, 3)]
    
    >>> onehop([(2,3),(1,2),(3,1),(1,3),(3,2),(2,4),(4,1)])
    [(1, 2), (1, 3), (1, 4), (2, 1), (3, 2), (3, 4), (4, 2), (4, 3)]
    
    >>> onehop([(1,2),(3,4),(5,6)])
    []
Ans:


def frequency(l):
  dl1=set(l)
  dl=list(dl1)
  newl=[]
  for i in dl:
    newl.append(l.count(i))
  mi=min(newl)
  ma=max(newl)
  mil=[]
  mal=[]
  for j in range(len(newl)):
    if newl[j]==mi:
      mil.append(dl[j])
    if newl[j]==ma:
      mal.append(dl[j])
     
  mil.sort()
  mal.sort()
  return(mil,mal)

def onehop(l):
  new=[]
  l.sort()
  for i in range(len(l)):
    for j in range(len(l)):
      if l[i]!=l[j]:
        if l[i][1]==l[j][0]:
          q=l[i][0]
          w=l[j][1]
          if q!=w:
            t=[q,w]
            t=tuple(t)
            if t not in new:
              new.append(tuple(t))
  new.sort()
  return(new)

netaji gandi Wednesday, August 28, 2019
Programming, Data Structures And Algorithms Using Python Week 4 Quiz

Consider the following Python function.
  def mystery(l):
     if l == []:
        return (l)
     else:
        return (l[-1:] + mystery(l[:-1]))
What does mystery([31,32,71,18,51]) return?


Ans: [51,18,71,32,31] 
2.5 points
What is the value of pairs after the following assignment?
 pairs = [ (x,y) for x in range(3,0,-1) for y in range(2,0,-1) if (x+y)%3 == 0 ]


Ans: [(2,1),(1,2)] 
2.5 points
2.5 points
Consider the following dictionary.
   marks = {"Quizzes":{"Mahesh":[3,5,7,8],"Suresh":[9,4,8,8],"Uma":[9,9,7,6]},"Exams":{"Mahesh":[37],"Uma":[36]}}
Which of the following statements does not generate an error?

2.5 points
Assume that d has been initialized as an empty dictionary:
  d = {}
Which of the following generates an error?


You may submit any number of times before the due date. The final submission will be considered for grading.

netaji gandi
NPTEL Programming, Data Structures And Algorithms Using Python



Week 3 Programming Assignment



def expanding(l):
  a=0
  for i in range(1,len(l)):
    if a>=abs(l[i]-l[i-1]):
      return False
    a=abs(l[i]-l[i-1])
  else:
    return True

def rotate(m):
  n=len(m)
  new=[]
  for i in range(n):
    temp=[]
    for j in range(n-1,-1,-1):
      temp.append(m[j][i])
    new.append(temp)
  return new


def accordian(l):
  if len(l)<3:
    return False
  new=[]
  for q in range(len(l)-1):
    k=abs(l[q]-l[q+1])
    new.append(k)
  tep=[]
  for i in range(0,len(new)-1):
    if new[i]>new[i+1]:
      tep.append("L")
    if new[i]<new[i+1]:
      tep.append("H")
    if new[i]==new[i+1]:
      tep.append("E")
  if "E" in tep:
    return False
  else:
    for g in range(len(tep)-1):
      if tep[g]==tep[g+1]:
        return False
    else:
      return True
  

netaji gandi Wednesday, August 21, 2019
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)
    
    

netaji gandi Monday, August 12, 2019
Programming, Data Structures And Algorithms Using Python NPTEL

Week 2 Quiz


1. One of the following 10 statements generates an error. Which one? (Your answer should be a number between 1 and 10.)

x = [1,"abcd",2,"efgh",[3,4]]  # Statement 1
  y = x[0:50]                    # Statement 2
  z = y                          # Statement 3
  w = x                          # Statement 4
  x[1] = x[1] + 'd'              # Statement 5
  y[2] = 4                       # Statement 6
  x[1][1] = 'y'                  # Statement 7
  z[0] = 0                       # Statement 8
  w[4][0] = 1000                 # Statement 9
  a = (x[4][1] == 4)             # Statement 10
Ans:7
2. Consider the following lines of Python code.
x = [13,4,17,1000]
  w = x[1:]
  u = x[1:]
  y = x
  u[0] = 50
  y[1] = 40
Ans:x[1] == 40, y[1] == 40, w[0] == 4, u[0] == 50
3.What is the value of endmsg after executing the following lines?
startmsg = "hello"
  endmsg = ""
  for i in range(0,len(startmsg)):
    endmsg = startmsg[i] + endmsg
Ans:olleh
4.What is the value of mylist after the following lines are executed?
def mystery(l):
    l = l + l
    return()

  mylist = [22,34,57]
  mystery(mylist)
Ans:[22,34,57]

netaji gandi
Programming, Data Structures And Algorithms Using Python NPTEL

Week 1 Quiz


What does h(3231) return for the following function definition?

def h(x):
    (m,a) = (1,0)
    while m <= x:
        (m,a) = (m*2,a+1)
    return(a)

Ans:12


What is g(24) - g(23), given the definition of g below?

def g(n): 
    s=0
    for i in range(1,n+1):
        if n%i == 0:
           s = s+1
    return(s)
Ans:6
def f(n): 
    s=0
    for i in range(1,n+1):
        if n%i == 0:
           s = s+1
    return(s%2 == 1)
Ans:n is a perfect square.
def f(m):
    if m == 0:
      return(0)
    else:
      return(m+f(m-1))
Ans:The function terminates for non­negative n with f(n) = n(n+1)/2

netaji gandi

NPTEL Programming in Java Jan 2024 Week 11

  Week 11 : Programming Assignment 1 Due on 2024-04-11, 23:59 IST The following code is missing some information needed to run the code. Add...