NPTEL The Joy of Computing Using Python Week9 Assignment

The Joy Of Computing Using Python Week 9.jpg

NPTEL Course a fun filled whirlwind tour of 30 hrs, covering everything you need to know to fall in love with the most sought after skill of the 21st century. The course brings programming to your desk 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 think logically and arrive at a solution programmatically. As part of the course, you will be learning how to practice and culture the art of programming with Python as a language. At the end of the course, we introduce some of the current advances in computing to motivate the enthusiastic learner to pursue further directions. The Joy of Computing using python is a fun filled course offered by NPTEL. The Joy Of Computing Using Python Week 9 Assignment.

INTENDED AUDIENCE :  Any interested audience
PREREQUISITES :  10th standard/high school
INDUSTRY SUPPORT :  Every software company is aware of the potential of a first course in computer science. Especially of a first course in computing, done right.

Course Layout

  • Motivation for Computing
  • Welcome to Programming!!
  • Variables and Expressions : Design your own calculator
  • Loops and Conditionals : Hopscotch once again
  • Lists, Tuples and Conditionals : Lets go on a trip
  • Abstraction Everywhere : Apps in your phone
  • Counting Candies : Crowd to the rescue
  • Birthday Paradox : Find your twin
  • Google Translate : Speak in any Language
  • Currency Converter : Count your foreign trip expenses
  • Monte Hall : 3 doors and a twist
  • Sorting : Arrange the books
  • Searching : Find in seconds
  • Substitution Cipher : What’s the secret !!
  • Sentiment Analysis : Analyse your Facebook data
  • 20 questions game : I can read your mind
  • Permutations : Jumbled Words
  • Spot the similarities : Dobble game
  • Count the words : Hundreds, Thousands or Millions.
  • Rock, Paper and Scissor : Cheating not allowed !!
  • Lie detector : No lies, only TRUTH
  • Calculation of the Area : Don’t measure.
  • Six degrees of separation : Meet your favourites
  • Image Processing : Fun with images
  • Tic tac toe : Let’s play
  • Snakes and Ladders : Down the memory lane.
  • Recursion : Tower of Hanoi
  • Page Rank : How Google Works !!

Programming Assignment 1

Question 1 : Swap the CaseGiven a string, you need to convert all lowercase letters to uppercase letters and vice versa.

				
					s = input()

def convert(ss):
    # Convert it into list and then change it
    newSS = list(ss)
    for i,c in enumerate(newSS):
        newSS[i] = c.upper() if c.islower() else c.lower()
    # Convert list back to string
    return ''.join(newSS)

print(convert(s))
				
			

Question 2 : PalindromeGiven a string, write a program to check if it is palindrome or not. A string is said to be a palindrome if the reverse of the string is the same as string. 

For example, “NITIN” is a palindrome but “AMIT” is not.
				
					# function to check string is  
# palindrome or not 
def isPalindrome(s): 
      
    # Using predefined function to  
    # reverse to string print(s) 
    rev = ''.join(reversed(s)) 
  
    # Checking if both string are  
    # equal or not 
    if (s == rev): 
        return True
    return False
  
# main function 
s = input()
ans = isPalindrome(s) 
  
if (ans): 
    print("YES") 
else: 
    print("NO")
				
			

Programming Assignment 2

Question 1 : First and Last: Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. If the string length is less than 2, return instead of the empty string.

				
					s=input()
if len(s)<2:
    print("",end="")
else:
    print(s[:2]+s[len(s)-2:len(s)],end="")
				
			

Question 2 : Smallest Palindrome: Given a string S having characters from English alphabets [‘a’ – ‘z’] and ‘.’ as the special character (without quotes). 

Write a program to construct the lexicographically smallest palindrome by filling each of the faded character (‘.’) with a lower case alphabet.
				
					numbers = []


li = []
x = input()
li = list(str(x))
if(len(li)%2 != 0):
    if(li[int(len(li)/2)] == '.'):
        li[int(len(li)/2)] = 'a'
s=0
p=len(li)-1
count = 0
flag = []
while((s < int(len(li)/2)) and (p >= int(len(li)/2))):
    if(li[s]!='.' and li[p]!='.'):
        if(li[s] == li[p]):
            flag.append(1)
            count+=1
            s += 1
            p -= 1
        else:                
            break
    else:
        if(li[s]=='.' and li[p]!='.'):
            li[s]=li[p]
            count+=1
            s += 1
            p -= 1
        else:
            if(li[s]!='.' and li[p]=='.'):
                li[p]=li[s]
                count+=1
                s += 1
                p -= 1
            else:
                li[s] = 'a'
                li[p] = 'a'
                count+=1
                s += 1
                p -= 1

if(count == int(len(li)/2)):
    print(''.join(li))
else:
    print(-1)
				
			

Programming Assignment 3

Question 1 : Rotate the MatrixGiven a square matrix with n rows and n columns, you have to write a program to rotate this matrix such that each element is shifted by one place in a clockwise manner.

				
					# Python program to rotate a matrix
 
# Function to rotate a matrix
def rotateMatrix(mat):
 
    if not len(mat):
        return
     
    """
        top : starting row index
        bottom : ending row index
        left : starting column index
        right : ending column index
    """
 
    top = 0
    bottom = len(mat)-1
 
    left = 0
    right = len(mat[0])-1
 
    while left < right and top < bottom:
 
        # Store the first element of next row,
        # this element will replace first element of
        # current row
        prev = mat[top+1][left]
 
        # Move elements of top row one step right
        for i in range(left, right+1):
            curr = mat[top][i]
            mat[top][i] = prev
            prev = curr
 
        top += 1
 
        # Move elements of rightmost column one step downwards
        for i in range(top, bottom+1):
            curr = mat[i][right]
            mat[i][right] = prev
            prev = curr
 
        right -= 1
 
        # Move elements of bottom row one step left
        for i in range(right, left-1, -1):
            curr = mat[bottom][i]
            mat[bottom][i] = prev
            prev = curr
 
        bottom -= 1
 
        # Move elements of leftmost column one step upwards
        for i in range(bottom, top-1, -1):
            curr = mat[i][left]
            mat[i][left] = prev
            prev = curr
 
        left += 1
 
    return mat
 
# Utility Function
def printMatrix(mat,n):
    for i in range(n):
        for j in range(n):
            if(j==n-1):
                print(mat[i][j],end="")
            else:
                print(mat[i][j],end=" ")
        
        if(i!=n-1):
            print()

n = int(input())

matrix = []
for i in range(1,n+1):    
    l = list(map(int, input ().split ()))
    matrix.append(l)

matrix = rotateMatrix(matrix)
# Print modified matrix
printMatrix(matrix,n)
				
			

Question 2 : Holes: Let us assume paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For example letters “A“, “D“, “O“, “P“, “R” divide the plane into two regions so we say these letters each have one hole. Similarly, letter “B” has two holes and letters such as “C“, “E“, “F“, “K” have no holes. We say that the number of holes in the text is equal to the total number of holes in the letters of the text. Write a program to determine how many holes are in a given text.

				
					list_st = []

zero_hole = ["C","E","F","G","H","I","J","K","L","M","N","S","T","U","V","W","X","Y","Z"]
one_hole = ["A","D","O","P","Q","R"]
two_hole = ["B"]

x = input()

c = 0
for j in x:        
    if(j in one_hole):
        c = c+1
    if(j in two_hole):
        c=c+2
print(c)
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *