Ad
Code
Diff
  • using System;
    using System.Linq;
    
    public class Basic
    {  
        public static string GetLoweredFirstLetter(string text) 
        {
            return text.Select(x => x.ToString().ToLower())
              .Where(x => char.IsLetter(x[0])).First();
        }
      
        public static string CalculateStudentPoints(string[] answers) 
        {
          int tickCount = (answers != null) ? answers.Where(x => x.Equals("O")).Count() : 0; 
          int errorCount = (answers != null) ? answers.Where(x => x.Equals("X")).Count() : 0;
          int score = tickCount - errorCount;
          return (answers != null && answers.Length > 0) ? answers
            .Select(x => $"{(errorCount > 5 ? "eliminated" : score > 5 ? score + 1 : score)}"
                + ((errorCount <= 5) ?
                $"{(Math.Abs(score) > 1 || Math.Abs(score) == 0 ? " points" : " point")}" 
                : ""))
            .First() : "-1 point";
        }
    }
    • using System;
    • using System.Linq;
    • public class Basic
    • {
    • public static string GetLoweredFirstLetter(string text)
    • {
    • return null;
    • return text.Select(x => x.ToString().ToLower())
    • .Where(x => char.IsLetter(x[0])).First();
    • }
    • public static string CalculateStudentPoints(string[] answers)
    • {
    • return null;
    • int tickCount = (answers != null) ? answers.Where(x => x.Equals("O")).Count() : 0;
    • int errorCount = (answers != null) ? answers.Where(x => x.Equals("X")).Count() : 0;
    • int score = tickCount - errorCount;
    • return (answers != null && answers.Length > 0) ? answers
    • .Select(x => $"{(errorCount > 5 ? "eliminated" : score > 5 ? score + 1 : score)}"
    • + ((errorCount <= 5) ?
    • $"{(Math.Abs(score) > 1 || Math.Abs(score) == 0 ? " points" : " point")}"
    • : ""))
    • .First() : "-1 point";
    • }
    • }
Code
Diff
  • using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    public class Basic
    {
        public static string[] ToLowerCaseArray(string text) 
        {        
            return (!string.IsNullOrWhiteSpace(text)) 
              ? text.Split(' ').Select(x => x.ToLower())
              .Where(x => !string.IsNullOrWhiteSpace(x))
              .ToArray()
              : new string[] { "" };
        }
      
        public static string ToUpperCaseString(string[] words) 
        {        
          string upperCaseWord = "";
          for (int i = 0; i < words.Length; i++) 
          {
             upperCaseWord = (i != words.Length - 1) 
               ? upperCaseWord + words[i].ToUpper() + " " 
               : upperCaseWord + words[i].ToUpper();  
          }
          return upperCaseWord;
        }
      
        public static string RemoveUnnecessarySpaces(string text) 
        {
            return Regex.Replace(text, "([\\s]^|[\\s]$|[\\s]+)", " ").Trim();
        }
      
        public static string GetTheFirstLetter(string text) 
        {
            return $"{text.Where(x => char.IsLetter(x)).First()}";
        }
      
        public static string UpperCaseFirstLetter(string text) 
        {
            return RemoveUnnecessarySpaces(
              new string(
                text
                .Select(x => x.ToString().ToUpper().ToCharArray()[0])
                .Take(1)
                .Concat
                (
                    text
                    .Select(x => x.ToString().ToLower().ToCharArray()[0])
                    .Skip(1)
                )
                .ToArray()
              )
            );       
        }
      
        public static string ToCamelCase(string text) 
        {
            return string.Join("", RemoveUnnecessarySpaces(text)
                    .Split(' ').Select(x => UpperCaseFirstLetter(x)));
        }
    }
    • using System;
    • using System.Linq;
    • using System.Text.RegularExpressions;
    • public class Basic
    • {
    • public static string[] ToLowerCaseArray(string text)
    • {
    • return null;
    • return (!string.IsNullOrWhiteSpace(text))
    • ? text.Split(' ').Select(x => x.ToLower())
    • .Where(x => !string.IsNullOrWhiteSpace(x))
    • .ToArray()
    • : new string[] { "" };
    • }
    • public static string ToUpperCaseString(string[] words)
    • {
    • return null;
    • string upperCaseWord = "";
    • for (int i = 0; i < words.Length; i++)
    • {
    • upperCaseWord = (i != words.Length - 1)
    • ? upperCaseWord + words[i].ToUpper() + " "
    • : upperCaseWord + words[i].ToUpper();
    • }
    • return upperCaseWord;
    • }
    • public static string RemoveUnnecessarySpaces(string text)
    • {
    • return null;
    • return Regex.Replace(text, "([\\s]^|[\\s]$|[\\s]+)", " ").Trim();
    • }
    • public static string GetTheFirstLetter(string text)
    • {
    • return null;
    • return $"{text.Where(x => char.IsLetter(x)).First()}";
    • }
    • public static string UpperCaseFirstLetter(string text)
    • {
    • return null;
    • return RemoveUnnecessarySpaces(
    • new string(
    • text
    • .Select(x => x.ToString().ToUpper().ToCharArray()[0])
    • .Take(1)
    • .Concat
    • (
    • text
    • .Select(x => x.ToString().ToLower().ToCharArray()[0])
    • .Skip(1)
    • )
    • .ToArray()
    • )
    • );
    • }
    • public static string ToCamelCase(string text)
    • {
    • return null;
    • return string.Join("", RemoveUnnecessarySpaces(text)
    • .Split(' ').Select(x => UpperCaseFirstLetter(x)));
    • }
    • }
Code
Diff
  • using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    public class Basic
    {  
        public static long ConvertTextToLong(string numberAsText)
        {
            return long.Parse(numberAsText);
        }
      
        public static decimal ConvertTextToDecimal(string numberAsText)
        {
          return decimal.Parse(numberAsText);
        }
      
        public static long ConvertIntToLong(int number)
        {
          return Convert.ToInt64(number);
        }  
        
        public static string[] ToUpperCaseArray(string text) 
        {
          string[] wordList = 
            text.Split(' ').Select(x => x.ToUpper())
            .Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
          return wordList.Any() ? wordList : new string[] { "" };
        }
      
        public static string ToLowerCaseString(string[] words) 
        {        
          string lowerCaseWord = "";
          for (int i = 0; i < words.Length; i++) 
          {
             lowerCaseWord = (i != words.Length - 1) 
               ? lowerCaseWord + words[i].ToLower() + " " 
               : lowerCaseWord + words[i].ToLower();  
          }
          return lowerCaseWord;
        }
      
        public static int IsUpperCase(string text)
        {
          int count = (text != null) ? text.Where(x => char.IsUpper(x)).Count() : -1;
          int strLen = (text != null) ? text.Where(x => !char.IsWhiteSpace(x)).Count() : -1;
          return count >= 0 && strLen > 0 ? count == strLen ? 1 : 0 : -1;
        }
      
        public static string RemoveUnnecessarySpaces(string text) 
        {
          return Regex.Replace(text, "([\\s]^|[\\s]$|[\\s]+)", " ").Trim();
        }
    }
    • using System;
    • using System.Linq;
    • using System.Collections.Generic;
    • using System.Text.RegularExpressions;
    • public class Basic
    • {
    • public static long ConvertTextToLong(string numberAsText)
    • {
    • return 0;
    • return long.Parse(numberAsText);
    • }
    • public static decimal ConvertTextToDecimal(string numberAsText)
    • {
    • return 0;
    • return decimal.Parse(numberAsText);
    • }
    • public static long ConvertIntToLong(int number)
    • {
    • return 0;
    • return Convert.ToInt64(number);
    • }
    • public static string[] ToUpperCaseArray(string text)
    • {
    • return null;
    • string[] wordList =
    • text.Split(' ').Select(x => x.ToUpper())
    • .Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
    • return wordList.Any() ? wordList : new string[] { "" };
    • }
    • public static string ToLowerCaseString(string[] words)
    • {
    • return null;
    • string lowerCaseWord = "";
    • for (int i = 0; i < words.Length; i++)
    • {
    • lowerCaseWord = (i != words.Length - 1)
    • ? lowerCaseWord + words[i].ToLower() + " "
    • : lowerCaseWord + words[i].ToLower();
    • }
    • return lowerCaseWord;
    • }
    • public static int IsUpperCase(string text)
    • {
    • return 0;
    • int count = (text != null) ? text.Where(x => char.IsUpper(x)).Count() : -1;
    • int strLen = (text != null) ? text.Where(x => !char.IsWhiteSpace(x)).Count() : -1;
    • return count >= 0 && strLen > 0 ? count == strLen ? 1 : 0 : -1;
    • }
    • public static string RemoveUnnecessarySpaces(string text)
    • {
    • return null;
    • return Regex.Replace(text, "([\\s]^|[\\s]$|[\\s]+)", " ").Trim();
    • }
    • }
Fundamentals
Logic
Code
Diff
  • public class Math
    {
      public int Max(int a, int b)
      {
        return a.CompareTo(b) > 0 ? a : b;
      }
    }  
    • public class Math
    • {
    • public int Max(int a, int b)
    • {
    • return (a > b) ? a : b;
    • return a.CompareTo(b) > 0 ? a : b;
    • }
    • }
Strings
Code
Diff
  • using System.Collections.Generic;
    
    public class ColorAdder
    {
        public static string AddColor(string color, string text)
        {
          string baseCode = "\033[";
          string resetCode = baseCode + "0m";
          Dictionary<string, int> colourCodes = new() 
          {
            { "Red", 31 },
            { "Green", 32 },
            { "Yellow", 33 },
            { "Blue", 34 },
            { "Magenta", 35 },
            { "Cyan", 36 },
            { "White", 37 }
          };
          return colourCodes.TryGetValue(color, out int code) ? $"{baseCode}{code}m{text}{resetCode}" : "";
        }
    }
    
    • using System.Collections.Generic;
    • public class ColorAdder
    • {
    • public static string AddColor(string color, string text)
    • {
    • string Reset = "\033[0m";
    • string Red = "\033[31m";
    • string Green = "\033[32m";
    • string Yellow = "\033[33m";
    • string Blue = "\033[34m";
    • string Magenta = "\033[35m";
    • string Cyan = "\033[36m";
    • string White = "\033[37m";
    • switch(color) {
    • case "Red":
    • return string.Concat(string.Concat(Red, text), Reset);
    • break;
    • case "Green":
    • return string.Concat(string.Concat(Green, text), Reset);
    • break;
    • case "Yellow":
    • return string.Concat(string.Concat(Yellow, text), Reset);
    • break;
    • case "Blue":
    • return string.Concat(string.Concat(Blue, text), Reset);
    • break;
    • case "Magenta":
    • return string.Concat(string.Concat(Magenta, text), Reset);
    • break;
    • case "Cyan":
    • return string.Concat(string.Concat(Cyan, text), Reset);
    • break;
    • case "White":
    • return string.Concat(string.Concat(White, text), Reset);
    • break;
    • default:
    • return "Invalid Color";
    • break;
    • }
    • string baseCode = "\033[";
    • string resetCode = baseCode + "0m";
    • Dictionary<string, int> colourCodes = new()
    • {
    • { "Red", 31 },
    • { "Green", 32 },
    • { "Yellow", 33 },
    • { "Blue", 34 },
    • { "Magenta", 35 },
    • { "Cyan", 36 },
    • { "White", 37 }
    • };
    • return colourCodes.TryGetValue(color, out int code) ? $"{baseCode}{code}m{text}{resetCode}" : "";
    • }
    • }
Code
Diff
  • using System;
    using System.Linq;
    
    public class Kumite 
    {
      public static bool IsThree(int number) 
      {
        return number.ToString().Contains('3');
      }
    }
    • using System;
    • using System.Linq;
    • public class Kumite {
    • public static bool IsThree(int number) {
    • var digitToSearch = 3;
    • var amountOfDigits = (int)Math.Log10(number);
    • for (int i = 0; i <= amountOfDigits; i++)
    • {
    • var divisor = (int)Math.Pow(10, i);
    • var truncate = number / divisor;
    • var digit = truncate % 10;
    • if (digit == digitToSearch)
    • {
    • return true;
    • }
    • }
    • return false;
    • public class Kumite
    • {
    • public static bool IsThree(int number)
    • {
    • return number.ToString().Contains('3');
    • }
    • }
Code
Diff
  • // Given 2 Arrays, Return True if arrays contain common item, else false.
    // e.g a = ['a','b','g','c'] and b = ['z','e','c'] returns true
    
    using System.Linq;
    
    public class Kata
    {
      public static bool ContainsCommonItemBetter(char[] a, char[] b)
      { 
        return a.Intersect(b).Any();
      } 
    }
    
    • //Given 2 Arrays, Return True if arrays contain common item, else false.
    • //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true
    • // naive approach, loop through first array, with each item in a, loop through b and compare item to
    • //each item in b returning true if a match is found or false.
    • //input : 2 arrays
    • //output: bool
    • using System.Collections.Generic;
    • public class Kata{
    • public static bool ContainsCommonItem(char[] a, char[] b){ //naive O(a*b)
    • for(int i=0;i<a.Length;i++)
    • for(int j=0; j< b.Length; j++)
    • if(a[i]==b[j])
    • return true;
    • return false;
    • }
    • public static bool ContainsCommonItemBetter(char[] a,char[]b){
    • HashSet<char> items = new HashSet<char>();
    • foreach(char item in a)
    • items.Add(item);
    • for (int i =0; i< b.Length; i++)
    • if (items.Contains(b[i]))
    • return true;
    • return false;
    • }
    • // Given 2 Arrays, Return True if arrays contain common item, else false.
    • // e.g a = ['a','b','g','c'] and b = ['z','e','c'] returns true
    • using System.Linq;
    • public class Kata
    • {
    • public static bool ContainsCommonItemBetter(char[] a, char[] b)
    • {
    • return a.Intersect(b).Any();
    • }
    • }
Code
Diff
  • public static class Kata 
    {
        public static int SameCase(char a, char b)
        {
          return (char.IsLetter(a) && char.IsLetter(b))
            ? (char.IsUpper(a) && char.IsUpper(b)) || (char.IsLower(a) && char.IsLower(b))
            ? 1 : 0
            : -1;
        }
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b) =>
    • (a >= 65 && a <= 90 || a >= 97 && a <= 122)
    • ? (b >= 65 && b <= 90 || b >= 97 && b <= 122)
    • ? (a >= 97 && b >= 97 || a <= 90 && b <= 90)
    • ? 1
    • : 0
    • : -1
    • : -1;
    • public static int SameCase(char a, char b)
    • {
    • return (char.IsLetter(a) && char.IsLetter(b))
    • ? (char.IsUpper(a) && char.IsUpper(b)) || (char.IsLower(a) && char.IsLower(b))
    • ? 1 : 0
    • : -1;
    • }
    • }