Friday, 14 September 2018

Fill up the blanks in the constructor to get the output as per the test cases.

Fill up the blanks in the constructor to get the output as per the test cases. Sample Test Cases
InputOutput
Test Case 1.5 D6 C A
6 C A
Test Case 2.9 I10 H A
6 C A
Test Case 3.6 A7 @ A
6 C A
Test Case 4.5 F6 E A
6 C A

#include <iostream>
using namespace std;

class Sample {
    public:
    int data_ ;
    char  graph_, data_or_graph_;

Sample(int x =6,char y='C',char z='A'): data_(x), data_or_graph_(y),graph_(z) {
        cout << data_ << " " << data_or_graph_ << " " << graph_ << " " << endl;
    }
}; // End of the class

int main() {
    int x; char y;
    cin >> x >> y ;
    Sample s1(x+1, y-1), s3;
    return 0;
}

Wednesday, 20 June 2018

Python Program To demonstrate inheritance.

# Python Program To demonstrate inheritance.

# Parent Class
class parentCls:
    def __init__(self):
        print("Parent Class Constructor.")

    # definition   of Parent class methods
    def prntMethd_1(self):
        print("I am parent class method 1.")
    def prntMethd_2(self):
        print("I am parent class method 2.")

# Child Class
class childCls(parentCls):  # inherit to parent class
    def __init__(self):
        print("Child class constructor.")

    # definition   of child class methods
    def childMethd_1(self):
        print("I am child class method 1")
    def childMethd_2(self):
        print("I am child class method 2")

c = childCls()  # Child class instance

c.childMethd_1()    # child call it's method
c.childMethd_2()    # Again child call it's method
c.prntMethd_1()     # child call it's Parent class method
c.prntMethd_2()     # Again child call it's Parent class method

input("Press Enter to Exit")

Tuesday, 19 June 2018

Python program to demonstrate constructors

#Python program to demonstrate constructors
# declaration of class

class myClass:
    def __init__(self,fName,lName): # Constructor
    # initialization of variable
        self.FirstName = fName
        self.LastName = lName

    def fName(self):
        return self.FirstName

    def lName(self):
        return self.LastName

mcls = myClass("blk","CapHax")
print("First Name : " + mcls.fName())
print("Last Name : " + mcls.lName())

print()
input("Press Enter to exit.")

Friday, 15 June 2018

To demonstrate working of classes and objects using python programming.

# To demonstrate working of classes and objects

#definition of class
class check:
    def func_A(self):
        print("I am Function A.")
    def func_B(self):
        print("I am function B.")

c = check()
c.func_A()
c.func_B()

input("Press Enter to exit.")

To read and write from a file using python programmin.

# To read and write from a file

str = input("Enter file name to open : ")


while(True):
    print("Choose one option : ")
    print("1. Write to file.")
    print("2. Read from file")
    print("e. exit")

    op = input();
    if op == '1':
        file = open(str+".txt","w")
        ele = input("Enter a string to write to file : ")
        file.write(ele+" ")
        file.close()
    elif op =='2':
        try:
            file = open(str+".txt","r")
            fd = file.read()
            print("file data : ")
            print(fd)
            file.close()
        except (FileNotFoundError):
            print("File not found.")
    elif op == 'e':
        break
    else:
        print("Choose correct option.")

To implement Queue using list using python programming.

# To implement Queue using list

queueList = []

def enQueue(ele='\0'):
    if ele!='\0':
        queueList.append(ele)
        print("enQueue operation successfully completed.")
    else:
        print("Operation failled.")

def deQueue():
    if(len(queueList)>0):
        queueList.remove(queueList[0])
        print("deQueue operation successfully completed.")
    else:
        print("Operation failled.")
        print("List is empty.")

while(True):
    print("\n\nChoose one option : ")
    print("1. enQueue operation,")
    print("2. deQueue operation.")
    print("3. Display List element.")
    print("e. Exit")
    op = input()
    if op=='1':
        ele = input("Enter element for PUSH operation.")
        enQueue(ele)
    elif op == '2':
        deQueue()
    elif op == '3':
        if(len(queueList)>0):
            print(queueList)
        else:
            print("List is Empty")
    elif op == 'e':
        break
    else:
        print("Choose correct option.")

To implement stack using list using python programming.

# To implement stack using list

stackList = []

def nrPush(ele='\0'):
    if ele!='\0':
        stackList.append(ele)
        print("PUSH operation successfully completed.")
    else:
        print("Operation failled.")
        print("Entered element is empty.")

def nrPop():
    if(len(stackList)>0):
        stackList.remove(stackList[-1])
        print("POP operation successfully completed.")
    else:
        print("Operation failled.")
        print("List is empty.")

while(True):
    print("\n\nChoose one option : ")
    print("1. PUSH operation,")
    print("2. POP operation.")
    print("3. Display List element.")
    print("e. Exit")
    op = input()
    if op=='1':
        ele = input("Enter element for PUSH operation.")
        nrPush(ele)
    elif op == '2':
        nrPop()
    elif op == '3':
        if(len(stackList)>0):
            print(stackList)
        else:
            print("List is Empty")
    elif op == 'e':
        break
    else:
        print("Choose correct option.")

Wednesday, 13 June 2018

Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically using Python Programming.

# Write a program that accepts a sequence of whitespace separated words as input and
# prints the words after removing all duplicate words and sorting them
# alphanumerically

str = input("Enter a whitespace seprated string : ")

if len(str)>0:
    sstr=""
    wordList = str.split(" ")
    wordList = sorted(wordList)
    for w in wordList:
        if sstr.count(w)==0:
            sstr +=w
            sstr +=" "
    sstr = sstr[:-1]
    print("Sorted String : ",sstr)
else:
    print("String is empty.")

input("Press Enter to exit.")

Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically Using Python Programming.

# Write a program that accepts a comma separated sequence of words as input
# and prints the words in a comma-separated sequence after sorting them
# alphabetically

str = input("Enter a comma seprated string : ")

if len(str)>0:
    sstr=""
    wordList = str.split(",")
    wordList = sorted(wordList)
    for w in wordList:
        sstr +=w
        sstr +=","
    sstr = sstr[:-1]
    print("Sorted String : ",sstr)
else:
    print("String is empty.")

input("Press Enter to exit.")

To compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.

# To compute the frequency of the words from the input. The output should
# output after sorting the key alphanumerically.

str = input("Enter a string : ")

if len(str)>0:
    dic = dict()
    wordList = str.split(" ")
    for w in wordList:
        dic.update({w:str.count(w)})
    dic = sorted(dic.items())
    print(dic)
else:
    print("String is empty.")

input("Press enter to exit.")

To get a string from a given string where all occurrences of its first char have been changed to '$', except the first char itself.

# To get a string from a given string where all occurrences of its first char have
# been changed to '$', except the first char itself.

str = input("Enter a String : ")

if len(str)>0:
    fChar = str[0]
    str = str.replace(fChar,"$")
    str = fChar + str[1:]
    print(str)
else:
    print("String is empty.")

input("Press Enter to exit.")

Sunday, 3 June 2018

To add 'ing' at the end of a given string (length should be at least 3). If the given string already ends with 'ing' then add 'ly' instead. If the string length of the given string is less than 3, leave it unchanged. Sample String : 'abc' Expected Result : 'abcing' Sample String : 'string' Expected Result : 'stringly'

#To add 'ing' at the end of a given string (length should be at least 3). If the
#given string already ends with 'ing' then add 'ly' instead. If the string length of
#the given string is less than 3, leave it unchanged.
#   Sample String : 'abc'
#   Expected Result : 'abcing'
#   Sample String : 'string'
# Expected Result : 'stringly'

def isConSpace(s):
    for n in s:
        if n == " ":
            return True
    return False
str = input("Enter a string : ")

if(len(str)>0):
    if(isConSpace(str)==False):
        if(str.isalpha()):
            if(len(str)>2):
                if(str[-3:] == "ing"):
                    str+=("ly")
                    print(str)
                else:
                    str+=("ing")
                    print(str)
            else:
                print(str)
        else:
            print("String must be contain only alphabet.")
    else:
        print("Please enter a string without space.")
else:
    print("String is empty.")

input("Press Enter to exit.")

Tuesday, 29 May 2018

To demonstrate use of slicing in string using python programming.

#Slicing in String

print("SI = Starting index; EI = Ending index")

nrStr = "I am Narendra Rajpoot."

print("Length of Streing : ",len(nrStr))

print("String : ",nrStr)

print("Charcter at index 3 : ",nrStr[3])

print("String [SI = 3; EI = end of the sting] : ",nrStr[3:])

print("String [SI = 3; EI = 13] : ",nrStr[3:13])

print("String [SI = 0; EI = 10] ",nrStr[:10])

print("String [SI = 3; EI = 12; charcter differnce = 2]",nrStr[3:12:2])

input("\n\nPress Enter to exit.")

To print ‘n terms of Fibonacci series using iteration using python programming.


n = int(input("Enter no. of terms in Fibonacci Series : "))

fTerm = 0
nTerm = 1

print("\n")

for i in range(n):
    if i<2:
        print(i,end=",")
    else:
        temp = nTerm
        nTerm = fTerm + nTerm
        print(nTerm,end=",")
        fTerm = temp
input("\n\nPress Enter to Exit.")





To find all prime numbers within a given range using python programming.

# To find all prime numbers within a given range.

import math

n1 = int(input("Enter lower limit of range : "))
n2 = int(input("Enter upper limit of range : "))

print("\n")

if n1==0:
    n1+=1
if n1>0:
    for i in range(n1,n2):
        flag=0
        sqr = math.sqrt(i)
        for j in range(2,int(sqr)+1):
            if j!=0:
                if (i%j)==0.0:
                    flag=1
                    break
        if flag==0:
            print(i,end=",")
else:
    print("Pleae enter positive limit: ")

input("\n\nPress Any Key to Closse.")

Popular Posts