satya

Friday, October 23, 2020

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 30

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