Tag

Tag Archive: C#

Log4net “it is being used by another process” Problem

If you use log4net for logging and try to download log files from the server, sometimes “file is being used” problem occurs. There is a configuration for minimal locking the log file.

<log4net>
<appender…
<lockingModel type=”log4net.Appender.FileAppender+MinimalLock” />
</appender>
</log4net>

 There is some performance impact because this means that log4net will lock the file, write to it, and unlock it for each write operation.

If you use RollingFileAppender things become even worse as several process may try to start rolling the log file concurrently. RollingFileAppender completely ignores the locking model when rolling files, rolling files is simply not compatible with this scenario.

Read more...

HackerRank Minimum Swaps 2 Problem [Solved]

https://www.hackerrank.com/challenges/minimum-swaps-2/problem No worries! It is as simple as HELL!!

    static int minimumSwaps(int[] arr) {
            int swapCount = 0;
            for (int index = 0; index < arr.Length - 1; index++)
            {
                if (arr[index] != (index + 1))
                {
                    for (int swapIndex = index + 1; swapIndex < arr.Length; swapIndex++)
                    {
                        if (arr[swapIndex] == (index + 1))
                        {
                            int aux = arr[swapIndex];
                            arr[swapIndex] = arr[index];
                            arr[index] = aux;
                            swapCount++;
                            break;
                        }
                    }
                }
            }
            return swapCount;   
}

Read more...

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...