Categories for

Software Development

HackerRank Solutions Grading Students Problem [SOLVED]

It is as simple as HELL!!

https://www.hackerrank.com/challenges/grading/problem

SO EASY!!

    public static List<int> gradingStudents(List<int> grades)
    {
         for (int i = 0; i < grades.Count; i++)
        {
            var item = grades[i];
            if (item >= 38)
            {
                var diff = 5 - (item % 5);
                if (diff < 3)
                    grades[i] = item + diff;
            }
        }
      return grades;
    }

Read more...

HackerRank Solutions Special String Again Problem [Solved]

CONGRATS SPECIAL STRING AGAIN PROBLEM SOLVED !!!

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

    // Complete the substrCount function below.
    static long substrCount(int n, string str) {
          long result = 0L;  
   int[] sameChar = new int[n]; 
    for(int v = 0; v < n; v++) 
    sameChar[v] = 0;   
    int i = 0;  
    // traverse string character 
    // from left to right 
    while (i < n)  
    {  
        // store same character count 
        int sameCharCount = 1; 
        int j = i + 1; 
        // count smiler character 
        while (j < n && str[i] == str[j]) 
        { 
            sameCharCount++; 
            j++; 
        }  
        // Case : 1 
        // so total number of  
        // substring that we can 
        // generate are : K *( K + 1 ) / 2 
        // here K is sameCharCount 
        result += (sameCharCount *  
                  (sameCharCount + 1) / 2); 
  
        // store current same char  
        // count in sameChar[] array 
        sameChar[i] = sameCharCount;  
        // increment i 
        i = j; 
    } 
  
    // Case 2: Count all odd length 
    //         Special Palindromic 
    //         substring 
    for (int j = 1; j < n; j++) 
    { 
        // if current character is  
        // equal to previous one  
        // then we assign Previous  
        // same character count to 
        // current one 
        if (str[j] == str[j - 1]) 
         sameChar[j] = sameChar[j - 1];  
        // case 2: odd length 
if(j > 0 && j < (n - 1) && (str[j - 1] == str[j + 1] && str[j] != str[j - 1])) 
             result += Math.Min(sameChar[j - 1], 
                              sameChar[j + 1]); 
    } 
     return result;
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());
        string s = Console.ReadLine();
        long result = substrCount(n, s);
        textWriter.WriteLine(result);
        textWriter.Flush();
        textWriter.Close();
    }
}

Read more...

Unique ID Creation in C#

We face so many times the same issue as we need to create a random unique ID. Here is the sample code

   
                int idgenerated = 0;
                foreach (var item in items)
                {
                    try
                    {
                        int index = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);
                        index = Math.Abs((int)index);
                        idgenerated = index;
                        idgenerated++;
                    }
                    catch (Exception Ex)
                    {
                        Console.WriteLine(Ex);
                    }
                }
                Console.WriteLine(idgenerated);
 //OR
 long ticks = DateTime.Now.Ticks;
 byte[] bytes = BitConverter.GetBytes(ticks);
 string id = Convert.ToBase64String(bytes)
                         .Replace('+', '_')
                         .Replace('/', '-')
                         .TrimEnd('=');
 Console.WriteLine (id);

 

Read more...

Difference between Abstract Class and Interface in C#

An abstract class is a way to achieve the abstraction in C#. An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class definition. The Abstract classes are typically used to define a base class in the class hierarchy. An Interface member cannot contain code bodies. Type definition members are forbidden. Properties are defined in an interface with the help of an access block get and set, which are permitted for the property.

Difference Between An Abstract Class And An Interface

  1. An Abstract class doesn’t provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
  2. Using Abstract we cannot achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
  3. We can not declare a member field in an Interface.
  4. We can not use any access modifier i.e. public, private, protected, internal etc. because within an interface by default everything is public.
  5. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
// C# program to illustrate the
// concept of abstract class
using System;

// abstract class 'Test'
public abstract class Test {

// abstract method 'harunu()'
public abstract void harunu();
}

// class 'Test' inherit
// in child class 'C1'
public class C1 : C {

// abstract method 'harunu()'
// declare here with
// 'override' keyword
public override void harunu()
{
Console.WriteLine("Class name is C1");
}
}

// class 'Test' inherit in
// another child class 'C2'
public class C2 : Test {

// same as the previous class
public override void harunu()
{
Console.WriteLine("Class name is C2");
}
}

// Driver Class
public class main_method {

// Main Method
public static void Main()
{

// 'obj' is object of class
// 'Test' class 
// 'Test' cannot
// be instantiate
C obj;

// instantiate class 'C1'
obj = new C1();

// call 'harunu()' of class 'C1'
obj.harunu();

// instantiate class 'C2'
obj = new C2();

// call 'harunu()' of class 'C2'
obj.harunu();
}
}

// C# program to illustrate the 
// concept of interface 
using System;

// A simple interface 
interface harunu{

// method having only declaration 
// not definition 
void show(); 
}

// A class that implements the interface. 
class MyClass : harunu{

// providing the body part of function 
public void show() 
{ 
Console.WriteLine("Welcome to Harunu!!!"); 
}

// Main Method 
public static void Main(String[] args) 
{

// Creating object 
MyClass objharunu = new MyClass();

// calling method 
objharunu.show(); 
} 
}

Read more...