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();
        }
    }
}

Is C# code is managed or unmanaged code?

C# is managed code because Common language runtime can compile C# code to Intermediate language.



Imp points -

.Net supports two type of coding-

Managed Code
Unmanaged Code

Managed Code-
The resource, which is with in your application domain is, managed code. The resources that are within domain are faster.

The code, which is developed in .NET framework, is known as managed code. This code is directly executed by CLR with help of managed code execution. Any language that is written in .NET Framework is managed code.

Managed code uses CLR which in turns looks after your applications by managing memory, handling security, allowing cross - language debugging, and so on.

Unmanaged Code-

The code, which is developed outside .NET, Framework is known as unmanaged code.
Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.
Unmanaged code can be unmanaged source code and unmanaged compile code.
Unmanaged code is executed with help of wrapper classes.
Wrapper classes are of two types: CCW (COM Callable Wrapper) and RCW (Runtime Callable Wrapper).
Wrapper is used to cover difference with the help of CCW and RCW.