satya

Friday, October 23, 2020

Program 28

                                                                         Program 28

Write a class called Time whose only field is a time in seconds. It should have a method called convert_to_minutes that returns a string of minutes and seconds formatted as in the following example: if seconds is 230, the method should return '5:50'. It should also have a method called convert_to_hours that returns a string of hours, minutes, and seconds formatted analogously to the previous method.

class Time:

    def __init__(self,sec):

        self.sec=sec

    def seconds_to_Minutes(self):

            s=self.sec

            minutes = s // 60

            seconds=s%60

            print('Times in Minutes:seconds format',str(minutes)+":",str(seconds))

    def seconds_to_Hours(self):

            s=self.sec

            hours = s // 3600

            minutes=(s//60)%60

            seconds=s%60

            print('Times in Hours:Minutes:seconds format',str(hours)+":",str(minutes)+":",str(seconds))

 

sec=int(input('Enter the number of seconds:'))

obj=Time(sec) 

obj.seconds_to_Minutes()

obj.seconds_to_Hours()


OUTPUT:

======RESTART: D:/old/r19 lab-python/28.py =============

Enter the number of seconds:150

Times in Minutes:seconds format 2: 30

Times in Hours:Minutes:seconds format 0: 2: 30

>>> 

======RESTART: D:/old/r19 lab-python/28.py =============

Enter the number of seconds:4500

Times in Minutes:seconds format 75: 0

Times in Hours:Minutes:seconds format 1: 15: 0

>>> 

Program 27

                                                                             Program 27

Write a class called Product. The class should have fields called name, amount, and price, holding the product’s name, the number of items of that product in stock, and the regular price of the product. There should be a method get_price that receives the number of items to be bought and returns a the cost of buying that many items, where the regular price is charged for orders of less than 10 items, a 10% discount is applied for orders of between 10 and 99 items, and a 20% discount is applied for orders of 100 or more items. There should also be a method called make_purchase that receives the number of items to be bought and decreases amount by that much.


class Product:


    def __init__(self, name, total_items, price):

        self.name = name

        self.total_items = total_items

        self.price = price


    def get_price(self, number_to_be_bought):

        discount = 0

        if number_to_be_bought < 10:

            print("Regular price is charged for your order")

            print("NO Discount\n ")

            cost=self.price * number_to_be_bought

            print('Final costs = ',cost)

            print(" A 10% discount is applied for orders of between 10 and 99 items")

            print(" A 20% discount is applied for orders of 100 or more items")

        elif 10 <= number_to_be_bought < 99:

            Actual_cost=self.price * number_to_be_bought

            print('Actual  cost is = ',Actual_cost)

            discount = 10

            p = (100 - discount) / 100 * self.price

            cost=p * number_to_be_bought

            print('Final costs(Discount cost10%) is  = ',cost)

            print("you save=",Actual_cost-cost,"Rupes")

        else:

            Actual_cost=self.price * number_to_be_bought

            print('Actual  cost is = ',Actual_cost)

            discount = 20

            p = (100 - discount) / 100 * self.price

            cost=p * number_to_be_bought

            print('Final costs(Discount cost20%) = ',cost,)

            print("you save=",Actual_cost-cost,"Rupes")

    

name=input("Enter the Name of the product:\n")

total_items=int(input('Total Number  of items:\n')),

price=int(input('Digit price of each item:\n'))

p=Product(name,total_items,price)

number_to_be_bought=int(input("Enter Number of items you want u buy:"))

p.get_price(number_to_be_bought)


Output:

>>> 
================ RESTART: D:/old/r19 lab-python/27.py ==============
Enter the Name of the product:
pen
Total Number  of items:
200
Digit price of each item:
5
Enter Number of items you want u buy:7
Regular price is charged for your order
NO Discount
 
Final costs =  35
 A 10% discount is applied for orders of between 10 and 99 items
 A 20% discount is applied for orders of 100 or more items
>>> 

==== RESTART: D:/old/r19 lab-python/27.py ===============
Enter the Name of the product:
pen
Total Number  of items:
200
Digit price of each item:
5
Enter Number of items you want u buy:79
Actual  cost is =  395
Final costs(Discount cost10%) is  =  355.5
you save= 39.5 Rupes
>>> 

===================== RESTART: D:/old/r19 lab-python/27.py =====================
Enter the Name of the product:
pen
Total Number  of items:
200
Digit price of each item:
5
Enter Number of items you want u buy:150
Actual  cost is =  750
Final costs(Discount cost20%) =  600.0
you save= 150.0 Rupes
>>> 

Program 26

                                                                 Program 26

Write a program that reads a list of temperatures from a file called temps.txt, converts those temperatures to Fahrenheit, and writes the results to a file called ftemps.txt.


file=open('temps.txt','r')

l=file.readlines()

f=open('ftemps.txt','w')

for line in range(len(l)):

    d=l[line].strip()

    fahrenheit = (float(d) * 9/5) + 32

    f.write(str(fahrenheit)+"\n")

f.close()

OUTPUT


Program 25

                                                                     Program 25

Write a program that reads a file consisting of email addresses, each on its own line. Your program should print out a string consisting of those email addresses separated by semicolons.


file=open(input("Enter the File name:"))

l=file.readlines()

for line in range(len(l)):

    if (l==len(l)-1):

        print('{}'.format(l[line].strip()))

    else:

        print('{}'.format(l[line].strip()),end=";")






OUTPUT:

Enter the File name:cse.txt

satya@gmail.com;raju@gmail.com;roja@gmail.com;isa@gmail.com;koti@gmail.com;

>>> 




Program 24

                                                                 Program 24

Write a program that asks the user for a word and finds all the smaller words that can be made from the letters of that word. The number of occurrences of a letter in a smaller word can’t exceed the number of occurrences of the letter in the user’s word.


from itertools import permutations

w=input("Enter  A Word:")

for i in range(2,len(w)):

    for p in permutations(w,i):

        print(''.join(p),end=' ')

Ouput:

=== RESTART: D:/old/r19 lab-python/24.py ========

Enter  A Word: CSE

CS CE SC SE EC ES 

====== RESTART: D:/old/r19 lab-python/24.py ========

Enter  A Word:crre

cr cr ce rc rr re rc rr re ec er er crr cre crr cre cer cer rcr rce rrc rre rec rer rcr rce rrc rre rec rer ecr ecr erc err erc err 

>>> 

Program 23

                                                                                             Program 23

Write a function called merge that takes two already sorted lists of possibly different lengths, and merges them into a single sorted list.

(a) Do this using the sort method. (b) Do this without using the sort method.

                                     a) Do this using the sort method

def merge(l1,l2):

    l=l1+l2

    l.sort()

    return l


l1=list(map(int,input("Enter the Sorted list 1:").split()))

l2=list(map(int,input("Enter the Sorted list 2:").split()))

s=merge(l1,l2)

print("After merge the  list is:",s)


OUTPUT:

========= RESTART: D:\old\r19 lab-python\22.py ===============

Enter the Sorted list 1:1 4 6 8 9

Enter the Sorted list 2:2 6 8 9 90

After merge the  list is: [1, 2, 4, 6, 6, 8, 8, 9, 9, 90]


                                              without using the sort method


def merge_lists(L1, L2):


    # When one of them is an empty list, returns the other list

    if not L1:

        return L2

    elif not L2:

        return L1


    result = []

    i = 0

    j = 0


    for k in range(len(L1) + len(L2)):

        if L1[i] <= L2[j]:

            result.append(L1[i])

            if i < len(L1) - 1:

                i += 1

            else:

                result += L2[j:]  # When the last element in L1 is reached,

                break             # append the rest of L2 to result.

        else:

            result.append(L2[j])

            if j < len(L2) - 1:

                j += 1

            else:

                result += L1[i:]  # When the last element in L2 is reached,

                break             # append the rest of L1 to result.


    return result

l1=list(map(int,input("Enter the Sorted list 1:").split()))

l2=list(map(int,input("Enter the Sorted list 2:").split()))

s=merge_lists(l1,l2)

print("After merge the  list is:",s)


Output:

============= RESTART: D:/old/r19 lab-python/22-2.py ==============

Enter the Sorted list 1:2 3 4 78

Enter the Sorted list 2:1 89 23 90

After merge the  list is: [1, 2, 3, 4, 78, 89, 23, 90]

>>> 


Tuesday, October 13, 2020

Program 22

                                             Program 22

Write a function called primes that is given a number n and returns a list of the first n primes. Let the default value of n be 100.


def printPrime(n=100):
    l=[]
    x=2
    while(len(l)<n):    
        for i in range(2,int(x**0.5)+1):
            if(x%i==0):
                break
        else:
            l.append(x)
        x=x+1
    return(l)
n=int(input("enter the number of prime numbers:")) 
s=printPrime(n)
print("List of first",n,"primes:",s)
j=printPrime()
print("List of first 100 primes:",j)

      OUTPUT:

>>> 

======= RESTART: C:/Users/crrcse/Desktop/22.py ========

enter the number of prime numbers:10

List of first 10 primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

List of first 100 primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]

>>> 


Wednesday, October 7, 2020

Program 21

                                                                              Program 21

Write a function called root that is given a number x and an integer n and returns x1/n. In the function definition, set the default value of n to 2.


def root(x,n=2):

    s=(x**(1/n))

    return(s)


x=int(input("enter the X value:"))

n=int(input("enter the nvalue:"))

res1=root(x)

res2=root(x,n)

print("Root value With Default 'n' value 2 is=",res1)

print("Root value With 'n' value  is=",res2)

OUTPUT:


==== RESTART: D:/old/r19 lab-python/21.py ======

enter the X value:18

enter the nvalue:2

Root value With Default 'n' value 2 is= 4.242640687119285

Root value With 'n' value  is= 4.242640687119285

>>> 

==== RESTART: D:/old/r19 lab-python/21.py =====

enter the X value:12

enter the nvalue:4

Root value With Default 'n' value 2 is= 3.4641016151377544

Root value With 'n' value  is= 1.8612097182041991

>>> 

===RESTART: D:/old/r19 lab-python/21.py =====

enter the X value:16

enter the n value:4

Root value With Default 'n' value 2 is= 4.0

Root value With 'n' value  is= 2.0

>>> 

Program 20

                                                                              Program 20

Write a function called is_sorted that is given a list and returns True if the list is sorted and False otherwise.


def is_sorted(l):

    k=l[:]

    k.sort()

    print("The K List elements are",k)

    if(l==k):

        return True

    else:

        return False

        

        

n=int(input("Enter the number elements in the list:"))

l=[]

for i in range(1,n+1):

    l.append(int(input("Enter the list %d Element:"%(i))))

    

print("The L List elements are",l)


print(is_sorted(l))



OUTPUT:
============RESTART: D:/old/r19 lab-python/20.py ===========
Enter the number elements in the list:5
Enter the list 1 Element:1
Enter the list 2 Element:2
Enter the list 3 Element:3
Enter the list 4 Element:4
Enter the list 5 Element:5
The L List elements are [1, 2, 3, 4, 5]
The K List elements are [1, 2, 3, 4, 5]
True
>>> 
============ RESTART: D:/old/r19 lab-python/20.py ===========
Enter the number elements in the list:5
Enter the list 1 Element:3
Enter the list 2 Element:2
Enter the list 3 Element:1
Enter the list 4 Element:5
Enter the list 5 Element:6
The L List elements are [3, 2, 1, 5, 6]
The K List elements are [1, 2, 3, 5, 6]
False
>>> 


Program 19

                                                                          Program 19

Write a function called number_of_factors that takes an integer and returns how many factors the number has.



def number_of_factors(num):

    count=0

    for i in range(1,num+1):

        if(num%i==0):

            print(i,end=" ")

            count=count+1

    return(count)


num=int(input("Enter the integer:"))

c=number_of_factors(num)

print("\n factors the %d number has =%d"%(num,c))



output:

>>> 
======= RESTART: D:\old\r19 lab-python\19.py ===============
Enter the integer:25
1 5 25 
 factors the 25 number has =3
>>> 
=========== RESTART: D:\old\r19 lab-python\19.py ============
Enter the integer:16
1 2 4 8 16 
 factors the 16 number has =5
>>> 

Program 18

 Program 18

Write a function called first_diff that is given two strings and returns the first location in which the strings differ. If the strings are identical, it should return -1.



def first_diff(s1,s2):

    if(s1==s2):

        return(-1)

    else:

        if len(s1)==len(s2):

            for i in range(len(s2)):

                if s1[i]!=s2[i]:

                    return(i+1)

s1=input("Enter the string1:")

s2=input("Enter the string2:")

c=first_diff(s1,s2)

if(c==-1):

    print("Both Strings are Identical")

else:

    print("first Difference occurs at location: ",c)




OUTPUT:
========= RESTART: D:\old\r19 lab-python\18.py ================
Enter the string1:crr

Enter the string2:crr

Both Strings are Identical
>>> 
============ RESTART: D:\old\r19 lab-python\18.py =============
Enter the string1:crrcoe

Enter the string2:crrcse
first Difference occurs at location: 5
>>> 

Program 17

                                                                         Program 17

17.Write a function called sum_digits that is given an integer num and returns the sum of the digits of num.


def sum_digit(num):

    sum1=0

    while(num!=0):

        sum1=sum1+num%10

        num=num//10

    return(sum1)

num=int(input("enter the integer number:"))

sum1=sum_digit(num)

print("the sum of %d digits are =%d"%(num,sum1))

output:

=======RESTART:D:/old/r19lab-python/18.py  =====================

enter the integer number:164

the sum of 164 digits are =11

>>> 

=======RESTART:D:/old/r19lab-python/18.py =====================

enter the integer number:124

the sum of 124 digits are =7

>>> 



Program 30

                                                                         Program 30 Write a Python class to implement pow(x, n). class Pow1:...