List Comprehension - count set bits

 # List Comprehension 


"""

Given a list of n integers, count the number of set bits (1’s) in the binary representation of each number present in the list

"""


# Read List

L = eval(input("Enter list of numbers : "))


# Convert each number in List to its binary equivalent and type cast to string

Lbinstr = [str(bin(N)) for N in L]


# Join the binary string and count number of 1's

print("Number of 1's = ",("".join(Lbinstr)).count("1"))


"""

Sample output

>python Listcomprehenstion.py

Enter list of numbers : [1,2,3,4,5]

Number of 1's =  7

"""


List manipulation - membership operator

# List manipulation


"""

The second year of engineering offers the following subjects in two departments. 

CSE = ["PQT", "ADC", "SE", "DPSD", "DS", "OOP"]

IT = ["OS", "PQT", "DS", "SE", "OOP", "SS"]


Write a program to create subject list for the two departments.  

Also find 

1. Number and names of subjects in CSE

2. Number and names of subjects in IT

3. Names of subjects in CSE only

4. Names of subjects in IT only

5. Number and names of subjects in both CSE and IT

6. Number and names of subjects for either CSE or IT, but not in both

7. Number and names of all subjects.

"""


# Create List

# CSE = ["PQT", "ADC", "SE", "DPSD", "DS", "OOP"]

# IT = ["OS", "PQT", "DS", "SE", "OOP", "SS"]


CSE = []

print("Enter subjects for CSE : ")

for count in range(0,6):

sub = input()

CSE.append(sub)

IT = eval(input("Enter subjects for IT : "))


# 1. Number and names of subjects in CSE

print("Number of subjects in CSE = ", len(CSE))

print("Subjects in CSE : ")

for sub in CSE:

print(sub, end=" ")

print()


# 2. Number and names of subjects in IT

print("Number of subjects in IT = ", len(IT))

print("Subjects in IT : ")

print(", ".join(IT))


# 3. Names of subjects in CSE only

print("Names of subjects in CSE only")

for sub in CSE:

if sub not in IT:

print(sub)


# 4. Names of subjects in IT only

print("Names of subjects in IT only")

for sub in IT:

if sub not in CSE:

print(sub)


# 5. Number and names of subjects in both CSE and IT

print("Number and names of subjects in both CSE and IT")

for sub in CSE:

if sub in IT:

print(sub)


# 6. Number and names of subjects for either CSE or IT, but not in both

List1=[]


for sub in IT:

if sub not in CSE:

List1.append(sub)

for sub in CSE:

if sub not in IT:

List1.append(sub)

print("Number of subjects for either CSE or IT, but not in both = ", len(List1))

print(List1)


# 7. Number and names of all subjects.

Allsub=CSE

for sub in IT:

if sub not in Allsub:

Allsub.append(sub)

print("Number and names of all subjects : ", len(Allsub))

string1 = ", ".join(Allsub)

print(string1)



"""

Sample output

>python 4_List.py

Enter subjects for CSE :

PQT

ADC

SE

DPSD

DS

OOP

Enter subjects for IT : ['OS','PQT','DS','SE','OOP','SS']

Number of subjects in CSE =  6

Subjects in CSE :

PQT ADC SE DPSD DS OOP

Number of subjects in IT =  6

Subjects in IT :

OS, PQT, DS, SE, OOP, SS

Names of subjects in CSE only

ADC

DPSD

Names of subjects in IT only

OS

SS

Number and names of subjects in both CSE and IT

PQT

SE

DS

OOP

Number of subjects for either CSE or IT, but not in both =  4

['OS', 'SS', 'ADC', 'DPSD']

Number and names of all subjects :  8

PQT, ADC, SE, DPSD, DS, OOP, OS, SS

""" 

Weight of Steel bar

 # Weight of Steel bar


"""

unit weight of steel bars is D²/162 kg/m. D is the Diameter Of steel bars, 162 is a constant value. 

"""


D = eval(input("Enter diameter of steel bar (in meter) : "))

wt = D*D/162

print("Weight of steel bar = ",round(wt,2),"kg/m")


"""

Sample output

>python wtofsteelbar.py

Enter diameter of steel bar (in meter) : 20

Weight of steel bar =  2.47 kg/m

"""

Electrical Current in Three Phase AC Circuit

 # Electrical Current in Three Phase AC Circuit


import math


P = eval(input("Enter power (in Watts) : "))

pf = eval(input("Enter power factor (pf<1): "))

VL = eval(input("Enter Line Voltage (in Volts) : "))


CL = P/(math.sqrt(3)*VL*pf)

print("Line Current =", round(CL,2),"A")


"""

Sample output

>python current.py

Enter power (in Watts) : 5000

Enter power factor (pf<1): 0.839

Enter Line Voltage (in Volts) : 400

Line Current = 8.6 A

"""

Weight of a motor bike

 # Weight of a motor bike


Bike = {"Chopper":315, "Adventure bike":250, "Dirtbike":100, "Touring bike":400, "Sport bikes":180, "Bagger":340, "Cruiser":250, "Cafe racer":200, "Scooter":115, "Moped":80 }


B = input("Enter bike : ")

print("Weight of", B, "is", Bike[B],"kg.")


"""

Sample output

>python wtofbike.py

Enter bike : Scooter

Weight of Scooter is 115 kg.

"""

String manipulation

 # String manipulation


"""

Implementing programs using Strings. (reverse, palindrome, character count, replacing characters,

remove characters)

"""


def fnReverse(s):

return s[::-1]


def fnPalindrome(s):

if(s==s[::-1]):

return "String is a palindrome"

else:

return "String is not a palindrome"


def fnCharcount(s):

charcount = 0

for ch in s:

if(ch.isalpha()):

charcount+=1

return charcount


def fnWordCount(s):

L=s.split()

return len(L)

def fnReplace(s,ss,rs):

if(ss in s):

return s.replace(ss,rs)

else:

return "Search string not found. Cannot replace"


def fnRemove(s, rs):

if(rs in s):

return s.replace(rs,"")

else:

return "String not found. Cannot remove" 



S = input("Enter string : ")

print("Reverse of the string : ",fnReverse(S))

print("Palindrome check : ")

print(fnPalindrome(S))

print("Character count =",fnCharcount(S))

print("Word count =",fnWordCount(S))

searchstr = input("Enter search string : ")

replacestr = input("Enter string to replace : ")


print("Replaced string :")

print(fnReplace(S,searchstr,replacestr))



removestr = input("Enter string to remove : ")


print("Remove string :")

print(fnRemove(S,removestr))


"""

Sample output

>python 7_stringmanip.py

Enter string : HELLO IT

Reverse of the string :  TI OLLEH

Palindrome check :

String is not a palindrome

Character count = 7

Word count = 2

Enter search string : IT

Enter string to replace : CSE

Replaced string :

HELLO CSE

Enter string to remove : IT

Remove string :

HELLO

"""

Electricity bill calculation

 

# Electricity bill calculation

"""

Tariff rates in TN

Scheme

Unit

Per unit(₹)

0 to 100

0-100

0

0 to 200

0-100

0

101-200

1.5

0 to 500

0-100

0

101-200

2

201-500

3

> 500

0-100

0

101-200

3.5

201-500

4.6

>500

6.6

 

"""

 

Unit = int(input("Enter number of units : "))

L = [[0],[0,1.5],[0,2,3],[0,3.5,4.6,6.6]]

Bill = 0

 

if(Unit>=0 and Unit<=100):

          Bill = 0

elif(Unit<=200):

          Bill = (Unit-100)*L[1][1]

elif(Unit<=500):

          Bill = 100*L[2][1]+(Unit-200)*L[2][2]

else:

          Bill = 100*L[3][1]+300*L[3][2]+(Unit-500)*L[3][3]

 

print("Unit :", Unit)

print("Bill : Rs.", Bill)

 

"""

Sample output

>python EBBill.py

Enter number of units : 510

Unit : 510

Bill : Rs. 1796.0

"""

 

Largest number in a List

 # Function - largest number in a list


def fnMax(L):

return max(L)


L1 = eval(input("Enter a List of numbers : "))

print("Largest number in given List = ", fnMax(L1))


Function overloading in Python - Area of a shape

# Functions - Area of a shape

# Function overloading 


"""

install multipledispatch as below from command prompt


pip install multipledispatch


"""


from multipledispatch import dispatch


@dispatch(int)

def fnShape(x):

print("Area of square = ",(x*x))

@dispatch(float)

def fnShape(x):

print("Area of circle = ",(3.142*x*x))


@dispatch(int,int)

def fnShape(x,y):

print("Area of rectangle = ",(x*y))


# The three function calls differ by number and datatype


fnShape(5) # int

fnShape(2.1)         # float

fnShape(5,6)         # int,int



Functions - factorial of a number

 # Functions - factorial of a number 


def fnfact(N):

if(N==0):

return 1

else:

return(N*fnfact(N-1))


Num = int(input("Enter Number : "))

print("Factorial of", Num, "is", fnfact(Num))