Nptel Programming Data Structures And Algorithms Using Python Week 2 Assignment Answer

ABOUT THE COURSE :
This course is an introduction to programming and problem solving in Python.  It does not assume any prior knowledge of programming.  Using some motivating examples, the course quickly builds up basic concepts such as conditionals, loops, functions, lists, strings and tuples.  It goes on to cover searching and sorting algorithms, dynamic programming and backtracking, as well as topics such as exception handling and using files.  As far as data structures are concerned, the course covers Python dictionaries as well as classes and objects for defining user defined datatypes such as linked lists and binary search trees.
INTENDED AUDIENCE: Students in any branch of mathematics/science/engineering, 1st year
PREREQUISITES: School level mathematics.
INDUSTRY SUPPORT: This course should be of value to any company requiring programming skills.

Nptel Programming Data Structures And Algorithms Using Python Week 2 Assignment Answer

Course layout

Week 1:
Informal introduction to programmin, algorithms and data structures viagcd
Downloading and installing Python
gcd in Python: variables, operations, control flow – assignments, condition-als, loops, functions

Week 2:
Python: types, expressions, strings, lists, tuples
Python memory model: names, mutable and immutable values
List operations: slices etc
Binary search
Inductive function denitions: numerical and structural induction
Elementary inductive sorting: selection and insertion sort
In-place sorting

Week 3:
Basic algorithmic analysis: input size, asymptotic complexity, O() notation
Arrays vs lists
Merge sort
Quicksort
Stable sorting

Week 4:
Dictionaries
More on Python functions: optional arguments, default values
Passing functions as arguments
Higher order functions on lists: map, lter, list comprehension

Week 5:
Exception handling
Basic input/output
Handling files
String processing

Week 6:
Backtracking: N Queens, recording all solutions
Scope in Python: local, global, nonlocal names
Nested functions
Data structures: stack, queue
Heaps

Week 7:
Abstract datatypes
Classes and objects in Python
“Linked” lists: find, insert, delete
Binary search trees: find, insert, delete
Height-balanced binary search trees

Week 8:
Effcient evaluation of recursive definitions: memoization
Dynamic programming: examples
Other programming languages: C and manual memory management
Other programming paradigms: functional programming

Nptel Programming Data Structures And Algorithms Using Python Week 2 Assignment Answer

Week 2 Quiz

Due date: 2025-02-05, 23:59 IST.
Assignment not submitted

All questions carry equal weightage. All Python code is assumed to be executed using Python3. You may submit as many times as you like within the deadline. Your final submission will be graded.Note:

  • If the question asks about a value of type string, remember to enclose your answer in single or double quotes.
  • If the question asks about a value of type list, remember to enclose your answer in square brackets and use commas to separate list items.

In the following 10 statements, which is the first one that generates an error? (Your answer should be a number between 1 and 10.)

x = ["hair",[23,4],2,"pin",[5]] # Statement 1
y = x[0:8]                      # Statement 2
z = x                           # Statement 3
w = y                           # Statement 4
z[4] = 10                       # Statement 5
y[3] = y[3][0:3] + 'k'          # Statement 6
y[1][1:3] = [5,8]               # Statement 7
w[4][0] = 1000                  # Statement 8
x[4][0] = 0                     # Statement 9
a = (x[4][1] == 4)              # Statement 10
7
2.5 points

Consider the following lines of Python code.

x = ['super',397,'king',43]
y = x[2:]
u = x
w = y
w = w[0:]
w[1] = 357
x[3:4] = [723]

After these execute, which of the following is correct?

 
 
 
 

What is the value of second after executing the following lines?

first = "pterodactyl"
second = ""
for i in range(len(first)-1,-1,-2):
  second = first[i]+second
prdctl
2.5 points

What is the value of list1 after the following lines are executed?

def mystery(l):
  l[1:3] = l[4:6]
  return()

list1 = [34,69,12,78,23,91,42]
mystery(list1)

[34,23,91,78,23,91,42]

Week 2 Programming Assignment

Due on 2025-02-06, 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 15 private test cases, 5 for each function below, 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. A positive integer m can be partitioned as primes if it can be written as p + q where p > 0, q > 0 and both p and q are prime numbers.

    Write a Python function primepartition(m) that takes an integer m as input and returns True if m can be partitioned as primes and False otherwise. (If m is not positive, your function should return False.)

    Here are some examples of how your function should work.

    >>> primepartition(7)
    True
    
    >>> primepartition(185)
    False
    
    >>> primepartition(3432)
    True
    
  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. A list rotation consists of taking the first element and moving it to the end. For instance, if we rotate the list [1,2,3,4,5], we get [2,3,4,5,1]. If we rotate it again, we get [3,4,5,1,2].

    Write a Python function rotatelist(l,k) that takes a list l and a positive integer k and returns the list l after k rotations. If k is not positive, your function should return l unchanged. Note that your function should not change l itself, and should return the rotated list.

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

    >>> rotatelist([1,2,3,4,5],1)
    [2, 3, 4, 5, 1]
    
    >>> rotatelist([1,2,3,4,5],3)
    [4, 5, 1, 2, 3]
    
    >>> rotatelist([1,2,3,4,5],12)
    [3, 4, 5, 1, 2]
 

Related Posts