## Question :-

- A positive integer n is said to be perfect if the sum of the factors of n, other than n itself, add up to n. For instance 6 is perfect since the factors of 6 are {1,2,3,6} and 1+2+3=6. Likewise, 28 is perfect because the factors of 28 are {1,2,4,7,14,28} and 1+2+4+7+14=28.Write a Python function
`perfect(n)`that takes a positive integer argument and returns`True`if the integer is perfect, and`False`otherwise.Here are some examples to show how your function should work.>>> perfect(6) True >>> perfect(12) False >>> perfect(28) True

- For an expression with parentheses, we can define the nesting depth as the maximum number of parentheses that are open when reading the expression from left to right. For instance, the nesting depth of “(33+77)(44-12)” is 1, while the nesting depth of “(a(b+c)-d)(e+f)” is 2.Write a Python function
`depth(s)`that takes a string containing an expression with parentheses and returns an integer, the nesting depth of`s`. You can assume that`s`is well-parenthesized: that is, that is, every “(” has a matching “)” after it and every “)” has a matching “(” before it.Here are some examples to show how your function should work. - Write a function
`sumsquares(l)`that takes as input a list of integers and retuns the sum of all the perfect squares in`l`.Here are some examples to show how your function should work.

# Solution :-

def perfect(n): sum=0 #initializing sum to 0 for i in range(1, int(n/2) + 1): # for loop for finding factor if n % i ==0: #if factor found, add it sum+=i if n==sum: return True #if addition is equal to number return true. else: return False #if addition is not equal to number return false. def depth(S): current_max = 0 max = 0 n = len(S) for i in range(n): if S[i] == '(': current_max += 1 if current_max > max: max = current_max elif S[i] == ')': if current_max > 0: current_max -= 1 else: return -1 if current_max != 0: return -1 return max def sumsquares(lst1): maxx = max(lst1) #find max number from the list lst1 sum = 0 #initializing sum to 0 i=1 while(i*i <= maxx): #Finding all numbers which are perfect squares. z = i*i if z in lst1: #If the perfect square is in list, add it to sum sum+= z i+=1 return sum #returning the value we want!

Can be tested with the following code :-

print(perfect(10)) print(perfect(6)) print(perfect(28)) print(sumsquares([1,4,9])) print(sumsquares([10,11,12,15])) print(sumsquares([16,20,25,30,625]))

## 23 Comments

## abhineet · August 10, 2017 at 7:14 PM

In the solution of the 3rd question, an error is coming

“UnboundLocalError: local variable ‘max’ referenced before assignment”

## yashrs · August 11, 2017 at 4:48 AM

Error solved. It was due to same name. Changed name of variable to maxx

## abhineet · August 11, 2017 at 9:37 PM

still same error is coming .

## abhineet · August 11, 2017 at 9:41 PM

local variable”maxx” referenced before assignment.

## sami · August 11, 2017 at 4:14 AM

Thank you very much !

## rohan · August 11, 2017 at 7:36 AM

In 2nd question the output is giving wrong answer

## yashrs · August 11, 2017 at 5:57 PM

What output are you getting and what should the output be ?

## zeel · August 11, 2017 at 9:56 AM

Thank you hackademic for this free online cource assignment solutions of NPTEL

## yashrs · August 11, 2017 at 6:09 PM

Thank you Zeel! Keep Supporting!

-Hackademic Team

## Mayuresh Vartak · August 11, 2017 at 3:31 PM

Getting too much of error please help

## yashrs · August 11, 2017 at 5:58 PM

What error are you getting ? I have fully checked it, and it works!

## Mayuresh Vartak · August 11, 2017 at 6:44 PM

Indentation error:expected an indented block

line 5

## Mit Patel · August 11, 2017 at 6:51 PM

There might be some errors due to formating of spacing from your side. Please check it again. It will definitely work.

## Mayuresh Vartak · August 12, 2017 at 2:33 AM

yes sir it was a font mistake but still getting error in testcase 5 and 6

## Mayuresh Vartak · August 12, 2017 at 2:41 AM

Thanks YOU Sir for helping me.

## Rohan · August 12, 2017 at 9:15 AM

For 2nd question i m getting all output as -1 but the actual outputs are 0 , 1 and 3.plz help!

## patil · August 12, 2017 at 4:57 PM

getting error as unident does not match any outer identation level

## shiv · August 16, 2017 at 5:05 PM

sir when are providing solution for programming assingment week 3.

## Mit Patel · August 17, 2017 at 7:31 PM

Hello Vishali, python week 3 programming assignment was posted. You can check out Week 3 Python Programming Assignment

## Mit Patel · August 17, 2017 at 7:34 PM

Hello Shiv, python week 3 programming assignment was posted. You can check out Week 3 Python Programming Assignment

## shiv · August 19, 2017 at 12:44 PM

thank you very much

## vishali · August 17, 2017 at 5:18 PM

could not find week 3 programming assignment solutions

## Mit Patel · August 17, 2017 at 5:35 PM

Hello Vishali, python week 3 programming assignment was posted. You can check out Week 3 Python Programming Assignment