C# : Shortest Solutions

Shortest Solutions In C#

Do not compute 5.
using System.Linq;
public class Program
{
  public static int func_five(int start, int end)
  {
    return Enumerable.Range(start, end-start+1).Count(x => !x.ToString().Contains("5"));
  }
 }

Shortest Word
using System.Linq;
public class Program
{
  public static int func_short(string s)
  {
    return s.Split(' ').Min(x => x.Length);
  }
}

Next Square
using static System.Math;

public class Program
{
  public static long func_square(long num)
  {
    return Sqrt(num) % 1 == 0 ? (long)Pow(Sqrt(num) + 1, 2) : -1;
  }
}

Remove the min number in the List
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
  public static List func_RemoveSmallest(List numbers)
  {
    numbers.Remove(numbers.DefaultIfEmpty().Min());
    return numbers;
  }
}

Validate PIN
using System;
using System.Linq;
using System.Text.RegularExpressions;

public class Program
{
  public static bool func_ValidatePin(string pin)
  {
       return pin.All(n => Char.IsDigit(n)) && (pin.Length == 4 || pin.Length == 6);
  }
}

Parity Outlier
using System.Collections.Generic;
using System.Linq;
using System;

public class Program
{
  public static int funv_find(int[] integers)
  {
       return integers.Count(a=>a%2==1)>1?integers.Single(a=>a%2==0):integers.Single(a=>a%2==1);

  }
}

Isograms
using System;
using System.Linq;
public class Program
{
  public static bool func_isogram(string str) 
  {
    return str.ToLower().Distinct().Count()==str.Length;
  }
}

Sum of odd numbers
using System;
public static class Program
{
  public static long func_SumOddNumbers(long n) => (long)Math.Pow(n, 3);
}

Disemvowel
using System;
using System.Text.RegularExpressions;

public static class Program
{
  public static string func_disemvowel(string str)
  {  
    return Regex.Replace(str, @"(?i)[aeiou]", "");
  }
}

If string length longer than four(4),reverse it.
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System;

public class Program
{
  public static string func_reverse(string sentence)
  {
    return Regex.Replace(sentence, @"\b\S{5,}\b", (x) => string.Concat(x.Value.Reverse()));
  }
}

Take a ten minute walk
using System.Linq;

public class Program
{
  public static bool func_Walk(string[] walk)
  {
    return walk.Count(x => x == "n") == walk.Count(x => x == "s") && walk.Count(x => x == "e") == walk.Count(x => x == "w") && walk.Length == 10;
  }
}

Vowels Count
using System;
using System.Linq;
using System.Text.RegularExpressions;
public static class Program
{
public static int func_count(string word) =>
   new Regex("[aeiouy]", RegexOptions.IgnoreCase).Matches(word).Cast().Count();
}

Base Conversion

It convert given base to target base(binary,hexadecimal,decimal,octal,alpha and alpha_(lower,upper,numeric) types).

using System;
using System.Linq;
public class Program{
  public string Convert(string input, string source_base, string target_base)
  {
        var a= input.Select((x,n)=>source_base.IndexOf(x)*(long)Math.Pow(source_base.Length,input.Length-1-n)).Sum();
        string rs;
        for (rs="";a>0;a/=target_base.Length) rs=target_base[(int)(a%target_base.Length)]+rs;
        return input=="0" ? target_base[0]+"" : rs;
  }
}

Comments

Popular posts from this blog

Tech Duos For Web Development

CIFAR-10 Dataset Classification Using Convolutional Neural Networks (CNNs) With PyTorch

Long-short-term-memory (LSTM) Word Prediction With PyTorch