Download Here

Note : The Entire's Copyright And Credits Of the Above Material, Guide Or Question Paper is Owned by the Respected Owner and author of the Material. We had Uploaded This Only For Educational.

Top Corner Ads

Below Of Download Button

February 09, 2024

NPTEL Programming, Data Structures And Algorithms Using Python (Week 2 Programming Assignment) 2024

4.5 Share It

Material Code -

NPTEL Week 2 Programming Assignment Solutions

NPTEL Week 2 Programming Assignment Solutions

Problem 1: threesquares(m)

A positive integer m can be expressed as the sum of three squares if it is of the form p + q + r where p, q, r ≥ 0, and p, q, r are all perfect squares. Write a Python function threesquares(m) that takes an integer m as input and returns True if m can be expressed as the sum of three squares and False otherwise. (If m is not positive, your function should return False.)

Examples:


>>> threesquares(6)
True

>>> threesquares(188)
False

>>> threesquares(1000)
True
                

Problem 2: repfree(s)

Write a function repfree(s) that takes as input a string s and checks whether any character appears more than once. The function should return True if there are no repetitions and False otherwise.

Examples:


>>> repfree("zb%78")
True

>>> repfree("(7)(a")
False

>>> repfree("a)*(?")
True

>>> repfree("abracadabra")
False
                

Problem 3: hillvalley(l)

A list of numbers is said to be a hill if it consists of an ascending sequence followed by a descending sequence, where each of the sequences is of length at least two. Similarly, a list of numbers is said to be a valley if it consists of a descending sequence followed by an ascending sequence. You can assume that consecutive numbers in the input sequence are always different from each other. Write a Python function hillvalley(l) that takes a list l of integers and returns True if it is a hill or a valley, and False otherwise.

Examples:


>>> hillvalley([1,2,3,5,4])
True

>>> hillvalley([1,2,3,4,5])
False

>>> hillvalley([5,4,1,2,3])
True

>>> hillvalley([5,4,3,2,1])
False
                

Programming Assignment Solutions

Python


def threesquares(n):
    while n > 0 and n % 4 == 0:
        n =n// 4
    return(n % 8 != 7)

def repfree(s):
    return(len(set(s))==len(s))
  
  
def hillvalley(l):
    dect = False
    inc = False
    c = 0
    for i in range(len(l)-1):
        if c > 1:
            return False
        right = l[i+1]
        middle = l[i]
        diff = right - middle
        if diff > 0:
            if dect:
                c += 1
            inc = True
            dect = False
        elif diff < 0:
            if inc:
                c += 1
            dect = True
            inc = False
    if c == 1:
        return True
    return False  
import ast

def tolist(inp):
  inp = "["+inp+"]"
  inp = ast.literal_eval(inp)
  return (inp[0],inp[1])

def parse(inp):
  inp = ast.literal_eval(inp)
  return (inp)

fncall = input()
lparen = fncall.find("(")
rparen = fncall.rfind(")")
fname = fncall[:lparen]
farg = fncall[lparen+1:rparen]

if fname == "threesquares":
   arg = parse(farg)
   print(threesquares(arg))
elif fname == "repfree":
   arg = parse(farg)
   print(repfree(arg))
elif fname == "hillvalley":
   arg = parse(farg)
   print(hillvalley(arg))
else:
   print("Function", fname, "unknown")

                

Rating and Reviews

No comments:

Post a Comment