Posts

Showing posts from October, 2021

Reverse words in a given String in Python

str="I love you" new=str.split(" ") resverse_list=[] for word in range(len(new)):     entity=new[-word-1]     resverse_list.append(entity) Reversed=' '.join(resverse_list) print Reversed

Iterview question sample 1

string="Sample" j="" for i in range(len(string)):     j+=string[5-i]     new=string.strip(j)     print(new)

constructor vs method oops 2

Image

OOPS concepts

class Test:     def __init__(self):         print "construcctor" t1=Test() t2=Test() Output: construcctor construcctor Got printed two times because constructor executes automatically at the time of object creation

Program to Get Data Items From a List Appearing Odd Number of Times

x = [ 1 , 2 , 3 , 4 , 5 , 1 , 3 , 3 , 4 ] l1=[] for i in x: if x.count(i)% 2 != 0 and i not in l1: l1.append(i) print (l1)

Interchange First and Last Element of a List

list3 = [1, 2, 4, 6] a=list3[0] list3[0]=list3[-1] list3[-1]=a print (list3)

Program to fid if a list is sorted or not

list1 = [1, 2, 3, 4, 5, 7, 8, 9] temp_list=list1[:] list1.sort() if temp_list==list1:     print ("True") else:     print ("False")

Second highest number program

list=[ 2 , 27 , 25 , 5 , 18 ] list.sort() print (list) def max_number (list): max_num=list[ 0 ] for i in list: if i>max_num: max_num=i return max_num max_num = (max_number(list)) print (max_num) max_num_count=list.count(max_num) print (max_num_count) for j in range (max_num_count): list.remove(max_num) second_max_num =max_number(list) print (second_max_num)

Reverse a string

string = "abcd" out ="" for i in range(len(string)):     out+= string[-i-1] print(out)