NPTEL >> Joy Of computing Using Python 2021 (Solved Programming Assignments)




The Joy of Computing exploitation Python This course is an associate introduction to programming and problem-solving in Python. It doesn't assume any previous information on programming. A fun-filled windstorm tour of thirty hrs, covering everything you wish to grasp to fall loving with the foremost sought-after talent of the twenty-first century. The course brings programming to your table with anecdotes, analogies, and illustrious examples. Turning abstractions to insights and engineering to art, the course focuses primarily to inspire the learner’s mind to suppose logically and attain an answer programmatically.

The Joy of Computing exploitation Python could be a MOOC-based course that's twelve weeks in period and may fulfill the standards of four credits during a year. you'll visit the NPTEL SWAYAM platform and register yourself for the course. This course is delivered to you by academics. Sudarshan Iyengar from the Indian Institute of Science associated is presently operating as a professor at IIT Ropar and has been teaching this course for the past four years.


Who Can Join: Any discipline PREREQUISITES:  Minimum: Tenth standard/high faculty Ideal: laptop design, Basic OS and Networking ideas

INDUSTRY SUPPORT: All industries

CRITERIA to induce A CERTIFICATEAverage assignment score = twenty-fifth of the type of the simplest vi assignments out of the entire eight assignments given within the course. Exam score = seventy-fifth of the proctored certification test wipe off of one hundred Final scores = Average assignment score + test score

Students are eligible for CERTIFICATE as long as AVERAGE ASSIGNMENT SCORE =10/25 AND test SCORE = 30/75. If any of the two criteria don't seem to be met, the coed won't get the certificate although the ultimate score = 40/100.



    The Joy Of Computing Using Python Programming Assignment Week 11 Answers:-

    Q1. Let ‘n’ be a positive even integer. Print the total number of ways to express ‘n’ as a sum of only even positive integers. 

    Input format :
    The positive even integer
    Output format :
    the total number of ways to express ‘n’ as a sum of only even positive integers else invalid

    Code:-

    n = int(input())
    if n==0:
      print(1,end='')
      exit()
    if n%2 ==0 and n>0:
      n = 2**((n/2)-1)
      print(int(n),end='')
    else:
      print('invalid',end='')

    Q2. Give a string, remove all the punctuations in it and print only the words in it. 

    Input format : the input string with punctuations

    Output format : the output string without punctuations


    Code:-

    import re
    string = input()
    string = re.sub(r'[^\w\s]', '', string)
    print(string,end='')
    Q3. print the number of binary sequences of length ‘n’ that have no consecutive 0’s

    Input format : A number 'n'

    Output format : number of binary sequences of length 'n' with no consecutive 0's else invalid

         

    Code:-

    n = int(input())
    if n>0:
      def countStrings(n):
        a=[0 for i in range(n)] 
        b=[0 for i in range(n)] 
        a[0] = b[0] = 1
        for i in range(1,n): 
            a[i] = a[i-1] + b[i-1] 
            b[i] = a[i-1] 
        return a[n-1] + b[n-1] 
      print(countStrings(n),end='')
    else:
      print('invalid',end='')

    The Joy Of Computing Using Python Programming Assignment Week 10 Answers:-

    Q1. Consider a directed graph. It can be represented by an adjacency matrix. The nodes are numbered 1 to n. If there is an edge from node i to node j, there will be a 1 in the (i-1,j-1) position in the adjacency matrix. There are no self loops in the graph. For a node, the number of head ends adjacent to a node is called the indegree of the node and the number of tail ends adjacent to a node is its outdegree. Print ‘yes’  and the node number if  in the given graph there is a node with indegree 2, else print ‘no’. 

    Code:-

    N,n,f=int(input()),input().split(),0
    ans=[n[i-N:i]for i in range(1,len(n)+1)if i%N==0]
    for a in range(len(ans)):
      if ans[a].count('1')==2:
        f=a+1
    if f!=0:
      print("yes",f,end="")
    else:
      print("no",end="")

    Q2. Consider a directed graph. It can be represented by an adjacency matrix. The nodes are numbered 1 to n. If there is an edge from node i to node j, there will be a 1 in the (i-1,j-1) position in the adjacency matrix. There are no self loops in the graph. For a node, the number of head ends adjacent to a node is called the indegree of the node and the number of tail ends adjacent to a node is its outdegree. Print ‘yes’  and the node number if  in the given graph there is a node with outdegree 2, else print ‘no’.

    Code:-

    N,n,f,Ans=int(input()),input().split(),0,[]
    ans=[n[i-N:i]for i in range(1,len(n)+1)if i%N==0]
    for k in range(N):
      p=[]
      for j in range(N):
        p.append(ans[j][k])
      Ans.append(p)
    for a in range(N):
      if Ans[a].count('1')==2:
        f=a+1   
    if f!=0:
      print("yes",f,end="")
    else:
      print("no",end="")
    Q3. Consider a directed graph. It can be represented by an adjacency matrix. The nodes are numbered 1 to n. If there is an edge from node i to node j, there will be a 1 in the (i-1,j-1) position in the adjacency matrix. There are no self loops in the graph. print yes if the given graph is a complete graph (connection from one node to all other node) else print no

         

    Code:-

    N,n,f=int(input()),input().split(),0
    ans=[n[i-N:i]for i in range(1,len(n)+1)if i%N==0 and n.count('0')!=N*N]
    for a in range(N):
      for b in range(N):
        if len(ans)>1 and ans[a][b]==ans[b][a] :
          f=f+1
    if f==N*N:
      print("yes",end="")
    else:
      print("no",end="")

    The Joy Of Computing Using Python Programming Assignment Week 9 Answers:-

    Q1. Given a list of integers and a value k, you are required to print an integer which appears in the list exactly k times. It is guaranteed that only one integer repeats k times.

    Code:-

    a,k=list(map(int, input().split())) ,int(input())
    print([q  for q in a if a.count(q)==k][0],end="" )

    Q2. Given an English sentence, check whether it is a panagram or not. A panagram is a sentence containing all 26 letters in the English alphabet.

    Code:-

    import string as st
    s=list(input().upper())
    if list(st.ascii_uppercase) == sorted(list(set(sorted(s)[sorted(s).index('A'):]))):
        print("Yes",end="")
    else:
        print('No',end="")
    

    Q3. Given a string s, remove all the vowels in s and reprint the string. The order of other characters in the string should be maintained\

    Code:-

    S,a=list(input()),""
    ans=[y for y in S if y!='A'and y!='E'and y!='O'and y!='U'and y!='I'and y!='a'and y!='e'and y!='i'and y!='o'and y!='u' ]
    print(a.join(ans),end="")

    The Joy Of Computing Using Python Programming Assignment Week 8 Answers:-

    Q1. Given a matrix with M rows and N columns, you are required to check if the matrix is a Zero-One Matrix. A Zero-One or a Binary matrix is a matrix in which all the elements are either 0 or 1.

    Code:-

    M,N= input().split()
    M,N=int(M),int(N)
    mat,check=[],0
    for  i in range(M):
        mat.append(input().split())
    for x in mat:
        for y in x:
            if y=="0":
                check=check+1
            elif y=="1":
                check=check+1
    if check==M*N:
        print("Yes",end="")
    else:
        print("No",end="")

    Q2. Given a N X N square matrix, determine if it is a Symmetric Matrix.

    Code:-

    N,check,mat=int(input()),0,[]
    for  i in range(N):
        mat.append(input().split())
    for r in range(N):
        for s in range(N):
            if mat[r][s]!=mat[s][r]:
                check=1
                break
    if check==0:
        print("Yes",end="")
    else:
        print("No",end="")
                

    Q3. Given an integer N, print a N X N square matrix, consisting of numbers from 1 to N^2, in the row-wise order

    Code:

    N,check,mat=int(input()),0,[]
    for  p in range(N):
        m=[]
        for q in range(N):
            check=check+1
            m.append(check)
        mat.append(m)
    for r in mat[:-1]:
       print(*r)
    print(*mat[-1],end="")

    The Joy Of Computing Using Python Programming Assignment Week 7 Answers:-

    Q1. List Prefix Given two lists, write a program to check if one list is a prefix of the other.

    Code:-

    a,b=input().split(),input().split()
    if (a[:]==b[:len(a)]):
      print("Yes",end="")
    else:
      print("No",end="")
    
    

    Q2. Snakes and Ladders-1
    Given a configuration of a Snakes and Ladders board, determine the number of snakes and ladders.

    Code:-

    config,d,s,l=input().split(","),{},0,0
    for c in config:
      r=c.split(":")
      d[int(r[0])]=int(r[1])
    for k in d:
      if d[k]>k:
        l=l+1
      else:
        s=s+1
    print(*[s,l],end="") 


    Q3. Snakes and Ladders-2

    Given a configuration of a Snakes and Ladders board and a series of numbers obtained when the die is rolled, determine if the sequence of the die rolls leads to a win.

    Code:-

    bc,dr,d=input().split(","),input().split(","),{}
    pos=int(dr[0])
    for c in bc:
      r=c.split(":")
      d[int(r[0])]=int(r[1])
    for a in range(len(dr)):
      if pos in list(d.keys()) :
        pos=d[pos]
      try:
        pos=pos+int(dr[a+1])
      except:
        pass
    if pos>100 or pos==100:
      print("Yes",end="")
    else:
      print("No",end="")
    

    The Joy Of Computing Using Python Programming Assignment Week 6 Answers:-

    Q1. Number Triangle-2Given an integer input ‘n’, print a palindromic number triangle of n lines as shown in the example.

    Code:-

    n=int(input())
    for i in range(1,n+1):
      for j in range(1,i+1):
          print(j,end="")
          if i==j:
            for k in range(i-1,0,-1):
                print(k,end="") 
      if i!=n:
        print("")

    Q2. Anagrams
    Given two strings as input, determine if they are anagrams are not. (Ignore the spaces, case and any punctuation or special characters)Note: Anagrams are the strings which are made up of the same set of letters. For example : Mary and Army are anagrams.

    Code:-

    (s1,s2,x,w)=(input(),input(),[],[])
    R=sorted(list(s1.upper()))
    S=sorted(list(s2.upper()))
    for f in R:
    	if f.isalnum():
    		w.append(f)
    for f in S:
    	if f.isalnum():
    		x.append(f)
    if x==w:
      print("Yes",end="")
    else:
      print("No",end="")


    Q3. Break the Secret

    You are hired by a secret service agency. Your job is to decode messages. You figure out the algorithm used to encrypt messages and it turns out to be as follows:The message will consist of only uppercase alphabets. The positional values are assigned to the characters as A=0, B=1, …, Y=24 and Z=25. In the original message, a character at position i is replaced by a character using the shift formula (i+3)%26.
    For example A will be replaced by D, B by E an so on. After replacement, the string is reversed and sent as a message.You are required to decode this message through your code.(Assuming No Space in the message)

    Code:-

    (s,a,b,c)=(input(),"","","")
    for i in list(s):
    	a=a+chr((ord(i)-65+3)%26+65)
    for g in list(a)[::-1]:
        b=b+g 
    for t in list(b):
          if t=='A':
            c=c+'U'
          elif t=='B':
            c=c+'V'
          elif t=='C':
            c=c+'W'
          elif t=='D':
            c=c+'X'
          elif t=='E':
            c=c+'Y'
          elif t=='F':
            c=c+'Z'
          else:
            c=c+chr(ord(t)-6)
    print(c,end="")

    The Joy Of Computing Using Python Programming Assignment Week 5 Answers:-

    Q1. Given an integer input ‘n’, print a number triangle of n lines as shown in the example.

    Code:-

    n=int(input())
    for i in range(1,n+1):
      for j in range(1,i+1):
          print(i,end="")
      if i!=n:
        print("")

    Q2. Bubble Sort
    You are required to sort a given list of integers in ascending order using Bubble sort algorithm. Determine the number of swaps that occur in the process.

    Code:-array=input().split()

    swap=0
    for i in range(len(array)):
      for j in range(i+1,len(array)):
        if(int(array[i])>int(array[j])):
          temp=array[i]
          array[i]=array[j]
          array[j]=temp
          swap=swap+1
    #print(*array)
    print(swap,end="")
          

    Q3. Maximal and Minimal Element
    Given a list of integers and a value k, print the sum of kth maximum element and kth minimum element in the list.

    Code:-

    numbers=input().split()
    k=int(input())
    q=[]
    for y in numbers:
      q.append(int(y))
    maximum=sorted(q, reverse=True)
    minimum=(sorted(q))
    maxi=[]
    mini=[]
    for m in maximum:
      if m not in maxi:
        maxi.append(m)
    for z in minimum:
      if z not in mini:
        mini.append(z)
    print(maxi[k-1]+mini[k-1],

    The Joy Of Computing Using Python Programming Assignment Week 4 Answers:-

    Q1. Two friends Suresh and Ramesh have m red candies and n green candies respectively. They want to arrange the candies in such a way that each row contains equal number of candies and also each row should have only red candies or green candies. Help them to arrange the candies in such a way that there are maximum number of candies in each row.

    Code:-

    def arrange_number(a, b):
    
       if a>b:
    
           s=b
    
       else:
    
           s=a
    
       for i in range(1, s+1):
    
           if a%i==0 and b%i==0:
    
               result=i
    
       return result
    
     
    
    m=int(input())
    
    n=int(input())
    
    print(arrange_number(m,n))

    Q2. Mr. Roxy has arranged a party at his house on the New Year’s Eve. He has invited all his friends – both men and women (men in more number). Your task is to generate the number of ways in which the invitees stand in a line so that no two women stand next to each other. 

    Code:-

    def fact(num):
    
     product=1
    
     while(num>1):
    
       product*=num
    
       num-=1
    
     return product
    
    m=int(input())  #m=men
    
    n=int(input())  #n=women
    
    if  m+n>20 or m<=n:
    
     print('invalid',end="")
    
    else:
    
      print(fact(m)*fact(n)*fact(m+1)//(fact(n)*fact(m+1-n)),end="")

    Q3. Given n – indicating the number of rows, print a right angle triangle of squares of numbers with n rows.

    Code:-

    n =int(input())
    
    count = 1
    
    for i in range(1, n+1):
      
      for j in range(1, i+1):
        
        print(count*count, end=" ")
        
        count+=1
      
      print()

    0 Comments