Sunday, 20 December 2020

Python : Calulator

 import math



def add(a, b):
return a + b


def Sub(a, b):
return a - b


def Mul(a, b):
return a * b


def div(a, b):
return a / b


print("Please select operation -\n" \
"1. add \n" \
"2. sub \n" \
"3.mul \n" \
"4. div")

Select = int(input("Select operation form list 1 2 3 4:"))

Number_1 = int(input("Enter First Number :"))
number_2 = int(input("Enter 2nd number:"))


if Select == 1:
print(Number_1, number_2, add(Number_1, number_2))


if Select == 2:
print(Number_1, number_2, Sub(Number_1, number_2))


if Select == 3:
print(Number_1, number_2, Mul(Number_1, number_2))

if Select == 4:
print(Number_1, number_2, div(Number_1, number_2))

else:
print("Invalid Entry")

Tuesday, 4 August 2020

Find the Runner-Up Score!


Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given  scores. Store them in a list and find the score of the runner-up.

Input Format

The first line contains . The second line contains an array   of  integers each separated by a space.

Constraints

Output Format

Print the runner-up score.

Sample Input 0

5
2 3 6 6 5

Sample Output 0

5

Explanation 0

Given list is . The maximum score is , second maximum is . Hence, we print  as the runner-up score.





if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
arr =list(arr)
arr.sort(reverse = True)
arr =  [i for i in arr if i != max(arr)]
print(max(arr))

Tuesday, 3 March 2020

String Characteristics

String Characteristics:
  • It is a reference type.
  • It’s immutable( its state cannot be altered).
  • It can contain nulls.
  • It overloads the operator(==).

Different Ways for Creating a String:
  • Create a string from a literal
  • Create a string using concatenation
  • Create a string using a constructor
  • Create a string using a property or a method
  • Create a string using formatting

Wednesday, 12 April 2017

C# Dictionary


C# Dictionary

How to implement singleton design pattern in C#?

In singleton pattern, a class can only have one instance and provides access point to it globally.

Public sealed class Singleton
{

Private static readonly Singleton _instance = new Singleton();


}

What is the difference between directcast and ctype?

 DirectCast is used to convert the type of an object that requires the run-time type to be the same as the specified type in DirectCast.
Ctype is used for conversion where the conversion is defined between the expression and the type.
 DirectCast is twice as fast for value types (integers, etc.), but identical for reference types.

Dim MyInt As Integer = 123 Dim MyString1 As String = CType(MyInt, String)Dim MyString2 As String = DirectCast(MyInt, String) ' This will not work
Dim MyObject As Object = "Hello World"Dim MyString1 As String = CType(MyObject, String)Dim MyString2 As String = DirectCast(MyObject, String) ' This will work
A way to check in code if DirectCast will work is by using the TypeOf operator:If TypeOf MyObject Is String Then

DirectCast
ctype
DirectCast is generally used to cast reference types.Ctype is generally used to cast value types.
When you perform DirectCast on arguments that don't match then it will throw InvalidCastException.Exceptions are not thrown while using ctype.
If you use DirectCast, you cannot convert object of one type into another. Type of the object at runtime should be same as the type that is specified in DirectCast. Consider the following example:
Dim sampleNum as Integer
Dim sampleString as String
sampleNum = 100
sampleString = DirectCast(sampleNum, String)
This code will not work because the runtime type of sampleNum is Integer, which is different from the specified type String. 
Ctype can cast object of one type into another if the conversion is valid. Consider the following example:
Dim sampleNum as Integer
Dim sampleString as String
sampleNum = 100
sampleString = CType(sampleNum, String)
This code is legal and the Integer 100 is now converted to a string.
To perform DirectCast between two different classes, the classes should have a relationship between them.To perform ctype between two different value types, no relationship between them is required. If the conversion is legal then it will be performed.
Performance of DirectCast is better than ctype. This is because no runtime helper routines of VB.NET are used for casting.Performance wise, ctype is slow when compared to DirectCast. This is because ctype casting requires execution of runtime helper routines of VB.NET.
DirectCast is portable across many languages since it is not very specific to VB.NETCtype is specific to VB.NET and it is not portable.

Wrapper classes

A wrapper class is any class which "wraps" or "encapsulates" the functionality of  another class or component. These are useful by providing a level of abstraction from the implementation of the underlying class or component;

Not much clear , Right ?

Ok, let me give example from my experience. I have a class for TAX calculation containing method to calculate TAX, and my another class called SALARY calculator is there. Now to calculate salary I want to use TAX calculator class. And my interest is that I will not disclose to other class “How I am calculating TAX”?. Then I will simply wrap my TAX calculator class by SALARY class. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

namespace BlogProject
{

    class Wrapper //Wrapper Class
    {
        class InnerClass //Inner Class
        {
            public Int32 ProcessData(int Val)
            {
                return Val + 1;
            }
        }

        public Int32 CallRapper()
        {
            Wrapper.InnerClass wpr = new Wrapper.InnerClass(); // Internally call to Wrapper class.
            return wpr.ProcessData(100);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Wrapper w = new Wrapper();
            Console.WriteLine("Data Process by inner class and show by Wrapper Class:-" + w.CallRapper());
            Console.ReadLine();
        }
    }
}