Python Tutorial: CS Assignment

Thursday, 14 July 2022

CS Assignment

1.     Given  below is a hypothetical table showing the rate of income tax for an Indian citizen, who is below or set up to 60 years.

 

Taxable Income(TI) in rupees

 Income Tax in rupees

Above upto rupees 5 lacs

 Nil

More than Rs 5,00,000 and less than or equal to 7,50,000

(TI-5,00,000)*10%

More than rupees 7,50,000 and less than or equal to rupees 10,00,000.

(TI-7,50,000)*20%+30,000

More than 10,00,000.

(TI-10,00,000)*30%+90,000

 

Write a python code to input the name age and taxable income of a person if the age is more than 60 years then display a message wrong category if the age is less than or equal to 60 years then compute and display the payable Income Tax along with the name of taxpayer pair as per the table given above.

Solution:

tax=0

name=input('Enter your name : ')

age=int(input('Enter your age : '))

sal=int(input('Enter Salary : '))

if age>60:

    print('Wrong Category')

else:

    if sal<=500000:

        tax=0

    if sal>500000 and sal<=750000:

        tax=(sal-500000)*10/100

    if sal>750000 and sal<=1000000:

        tax=30000+(sal-750000)*20/100

    if sal>1000000:

        tax=90000+(sal-750000)*30/100

print('Name : ', name)

print('Age : ',age)

print('Tax Payable : ', tax)

 

2.     School displays a notice on the school notice board regarding admission in class 11th for choosing streams according to the marks obtained in English Maths and Science in class 10th Council examination.

 

Marks obtained in different subject

 Stream

English and Science >= 80%

Pure Science

English and Science >= 80%, Maths >= 60%

 Biology Science

 Otherwise

 Commerce

Write  a python code to accept marks in English, Maths and Science from the console and display the appropriate stream allotted to the candidate.

Solution:

m=int(input('Enter Marks is Maths : '))

e=int(input('Enter Marks is English : '))

s=int(input('Enter Marks is Science : '))

if(m>=80 and e>=80 and s>=80):

    print("Stream : Pure Science")

elif(m>=60 and e>=80 and s>=80):

    print('Stream : Bio Science')

else:

    print('Stream : Commerce')

 

3.     Write a python code to enter a number containing 3 digits or more. Arrange the digit of the entered number in ascending order and display the result.

Sample input : Enter number  :4972

Sample output :  2,4,7,9

Solution:

OrgNum =int(input("Enter a number having 3 or more digits: "))

for i in range(10):

    num = OrgNum;

    c = 0;

    while (num != 0):

        if (num % 10 == i):

            c+=1

        num //= 10;

    for j in range(1,c+1):

        print(i , end=", ")

 

4.     Write a python code to input a number and check whether it is a ‘Magic number’ or not display the message accordingly.

Sample Input: 55

Here. 5+5=10, 1+0=1

Sample output : 55 is a magic number

Similarly, 289 is a magic number

Solution:

import math

num = int(input("Enter a Number \n"))

digitCount = int(math.log10(num))+1

sumOfDigits = 0

temp = num #copying num

 

#calculating sum of digits of temp(i.e num) until

#sumOfDigits is a single digit

while( digitCount > 1):

 

  sumOfDigits = 0

 

  while(temp > 0):

    sumOfDigits += temp%10

    temp = temp//10

 

  temp = sumOfDigits

  #count the digits of sumOfDigits

  digitCount = int(math.log10(sumOfDigits))+1

  

   

#check whether sumOfDigits == 1

if(sumOfDigits == 1):

    print("Magic number")

else:

    print("Not a magic number")

 

5.     A prime number is said to be ‘Twisted Prime’. If  the number obtained after reversing the digits is also a prime number. Write  a python code to accept a number and check whether the number is ‘Twisted prime’ or not.

Sample Input : 167

Sample Output : 761

167 is a Twisted Prime

Solution:

n = int(input('Enter the prime number? ')) 

sum=0

while(n!=0)  :

 

    reverse = n%10 

    sum = sum*10 + reverse 

    n= n//10 

 

flag = 0 

for j in range(2,sum// 2,1):

    if ((sum % j) == 0)  :

        flag = 1 

        break 

 

if (flag == 0): 

    print("Twisted Prime") 

else: 

    print("Not Twisted Prime") 

 

6.     A number is said to be ‘Happy Number’ if eventual sum of square of its digits results is 1. Write a program code to input a number and check whether it is Happy Number or not.

Sample Input : 31

Sample Output : 31=32+12=10=12+1=1

Solution:

s=d=0

num=int(input('Enter a number : '))

s=num

while s>9:

    num=s

    s=0

    while num>0:

        d=num%10

        s=s+d**2

        num//=10

if s==1:

    print("Happy Number")

else:

    print("Not Happy Number ")

 

7.     Automorphic number : An automorphic number is  the number which is contained in the last digit(s) of its square

Example : 25 is an automorphic number as its square is 625 and 25 is present as its last two digits.

Solution:

#A Python code check Automorphic number

import math

m=int(input("Enter a number : "))

m1=m

c=0

p=m*m

while(m!=0):

    m=m//10

    c=c+1

k=int(math.pow(10,c))

a=p%k

if a==m1:

    print(m1," is an Automorphic number")

else:

    print(m1," is not an Automorphic number")

   

8.     Write a python code to accept present sentences. Display sentence in reverse order of its words

Sample Input :  Computer is Fun

Sample Output : Fun is Computer

Solution:

wd='';rwd=''

str=input("Enter a string")

str=str+' '

p=len(str)

for i in range(0,p):

    if(str[i]==' '):

        rwd=wd+' '+rwd

        wd=''

    else:

        wd=wd+str[i]

print("Words in reversed order :", rwd)

9.     Write a python code to enter a sentence and display the longest word present in the sentence along with its length.

Sample Input : We are learning python

Sample Output : The longest word :  learning

                            The length of the word :8

Solution:

wd='';nwd='';c=a=0

str=input("Enter a string :")

str=str+' '

p=len(str)

for i in range(0,p):

    if(str[i]==' '):

        a=len(wd)

        if(a>c):

            nwd=wd

            c=a

            a=0

            wd=''

    else:

        wd=wd+str[i]

print('Longest word : ',nwd)

print("Length of the word :",c)

10. Write a python code to accept a sentence in lowercase. Find the frequency of vowel in each word and display the words along with frequency of vowel.

Sample Input : understanding computer science

Sample output :           Word                          Noof vowels

understanding                                     4

computer                                 3

science                                     3

Solution:

#To display no. of vowels in each word

wd='';nwd='';b=v=0

str=input("Enter a string in lowercase :")

str=str+' '

p=len(str)

print("Word\t\t\tNo. of vowels")

for i in range(0,p):

   

    if(str[i]==' '):

        wd=str

       

        b=len(wd)

        v=0

        for j in range(0,b):

            if(wd[j]=='a' or wd[j]=='e' or wd[j]=='i' or wd[j]=='o' or wd[j]=='u'):

                v=v+1

        print(wd,"\t\t\t",v)

        wd=''

    else:

        wd+wd+str[i]

11.  Write a python code to accept some words in lowercase in a list. Mark all the vowels of each word with “*” and display the words.

Solution:

#To mark all the vowels of each word with '*' present in the list

L1=eval(input("Enter some words in lowercase in the list "))

print("Words after replacing vowels with '*':")

for m in L1:

    wd=m

    k=len(wd)

    nwd=''

    for n in range(0,k):

        if wd[n] in 'aeiou':

            wd1='*'

            nwd=nwd+wd1

        else:

            nwd=nwd+wd[n]

    print(nwd)

12. A number is a set to be unique if the digits are not repeated in it. Write a code in Python to accept a set of numbers in a tuple and display all unique numbers.

Solution:

#To display unique number

num=eval(input("Enter a set of numbers :"))

flag=False

print("The unique nubers ")

for a in num:

    n=a

    while(n>0):

        count=0

        p=a

        d1=n%10

        while(p>0):

            d2=p%10

            if(d1==d2):

                count=count+1

            p=int(p/10)

        if count>1:

            break

        n=int(n/10)

    if (count==1):

        print(a)

13. Write a python code to accept the names and marks secured in three subjects by using different tuples. Display the name along with average marks of the student who has secured the highest average.

Solution:

#To display name with the highest average

avg=0

name=eval(input('Enter names in tuple : '))

phy=eval(input('Enter physics marks in tuple : '))

chem=eval(input('Enter chemistry marks in tuple : '))

bio=eval(input('Enter biology in tuple : '))

for i in range(0,len(name)):

    if((phy[i]+chem[i]+bio[i])/3>avg):

        avg=(phy[i]+chem[i]+bio[i])/3

        nm=name[i]

    print('Name :', nm, ' and average marks', avg)

14. Write a python code to input a sentence in lowercase convert the first letter of each word of the sentence in uppercase and display the new sentence

            Sample Input : computer science with Python

            Sample Output : Computer Science With Python

Solution:

 

 

15. Write a python code to input a word and display the same in  Piglatin form. A word is framed in ‘Piglatin’ form by using the following steps:

(a)    Shift all the letters before the initial vowel at the end of the word.

(b)    Finally,   add  ‘AY’ at the end of the new word sentence.

Sample Input : TROUBLE

Sample Output : OUBLETRAY

# Python program to encode a word to a Pig Latin.

def isVowel(c):

            return (c == 'A' or c == 'E' or c == 'I' or

                                    c == 'O' or c == 'U' or c == 'a' or

                                    c == 'e' or c == 'i' or c == 'o' or

                                    c == 'u');

 

def pigLatin(s):

            # the index of the first vowel is stored.

            length = len(s);

            index = -1;

            for i in range(length):

                        if (isVowel(s[i])):

                                    index = i;

                                    break;

 

            # Pig Latin is possible only if vowels

            # is present

            if (index == -1):

                        return "-1";

            # Take all characters after index (including  index).

            # Append all characters which are before # index. Finally append "ay"

            # return s[index:] + s[0:index] + "ay";

 

s=input("Enter a string : ").upper()

str = pigLatin(s);

if (str == "-1"):

            print("No vowels found. Pig Latin not possible");

else:

            print(str);

 

16. Write a python code to accept a word in lowercase. Display the new world by replacing only revolver with the letter following by it

Sample Input : computers

Sample Output : cpmvtfr

Solution:

str=input("Enter a word: ")

newStr = ""

length = len(str)

for i in range(length):

    ch = str[i]

    if (str == 'a' or   str[i] == 'e' or   str[i] == 'i' or   str[i] == 'o' or  str[i] == 'u'):

        nextChar = chr(ord(ch)+1)

        newStr = newStr +  nextChar

    else:

                newStr = newStr + ch

print(newStr)

No comments:

Post a Comment