Created
February 11, 2025 09:22
-
-
Save daverave1212/6d761f4c1c351655d58430d7aa25501d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text.RegularExpressions; | |
| public static class U { | |
| // Math | |
| public static double PercentOf(this double value, double ofWhat) { | |
| return ofWhat * value / 100; | |
| } | |
| public static double WhatPercentOf(double isValue, double ofValue) { | |
| return isValue * 100 / ofValue; | |
| } | |
| public static int ProbabilityDistribution(int[] distribution) { | |
| if (distribution == null) { | |
| throw new Exception("null distribution"); | |
| } | |
| if (distribution.Length == 0) { | |
| return -1; | |
| } | |
| var balls = new List<int>(); | |
| for (int index = 0; index < distribution.Length; index++) { | |
| for (int time = 0; time < distribution[index]; time++) { | |
| balls.Add(index); | |
| } | |
| } | |
| return balls[RandomInt(0, balls.Count - 1)]; | |
| } | |
| public static int ToInt(double f) { | |
| return (int)f; | |
| } | |
| public static double AngleBetweenPoints(double x1, double y1, double x2, double y2) { | |
| double xDiff = x2 - x1; | |
| double yDiff = y2 - y1; | |
| return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI; | |
| } | |
| public static float DistanceBetweenPoints(double x1, double y1, double x2, double y2) { | |
| return (float) Math.Sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); | |
| } | |
| public static (float, float) GetMiddlePoint(double x1, double y1, double x2, double y2) { | |
| float x = (float) (x1 + x2) / 2; | |
| float y = (float) (y1 + y2) / 2; | |
| return (x, y); | |
| } | |
| public static bool IsBetween(this double i, double a, double b) { | |
| return a <= i && i <= b; | |
| } | |
| public static double CalculateSlope(double x1, double y1, double x2, double y2) { | |
| double m = (y2 - y1) - (x2 - x1); | |
| return m; | |
| } | |
| public static double CalculateIntercept(double x, double y, double m) { | |
| double c = y - m * x; | |
| return c; | |
| } | |
| // Random | |
| public static int RandomIntBetween(int minimum, int maximum) { | |
| return RandomInt(minimum, maximum); | |
| } | |
| public static int RandomInt(int minimum, int maximum) { | |
| Random random = new Random(); | |
| return random.Next(minimum, maximum + 1); | |
| } | |
| public static double RandomFloatBetween(double minimum, double maximum) { | |
| Random random = new Random(); | |
| return random.NextDouble() * (maximum - minimum) + minimum; | |
| } | |
| public static T RandomOf<T>(T[] array) { | |
| return array[RandomInt(0, array.Length - 1)]; | |
| } | |
| public static int RandomIndex<T>(this T[] array) { | |
| if (array.Length == 0) { | |
| return -1; | |
| } | |
| return RandomInt(0, array.Length - 1); | |
| } | |
| public static int RandomIndex<T>(this List<T> array) { | |
| if (array.Count == 0) { | |
| return -1; | |
| } | |
| return RandomInt(0, array.Count - 1); | |
| } | |
| // Arrays | |
| public static T Last<T>(this T[] array) { | |
| return array[array.Length - 1]; | |
| } | |
| public static T First<T>(this T[] array) { | |
| return array[0]; | |
| } | |
| public static int GetFirstNull<T>(this T[] array) { | |
| for (int i = 0; i < array.Length; i++) { | |
| if (array[i] == null) { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| } | |
| public static T[] Times<T>(T what, int nTimes) { | |
| var list = new List<T>(); | |
| for (int i = 1; i <= nTimes; i++) { | |
| list.Add(what); | |
| } | |
| return list.ToArray(); | |
| } | |
| public static List<T> TimesList<T>(T what, int nTimes) { | |
| var list = new List<T>(); | |
| for (int i = 1; i <= nTimes; i++) { | |
| list.Add(what); | |
| } | |
| return list; | |
| } | |
| public static void PushTimes<T>(List<T> list, T what, int nTimes) { | |
| for (int i = 1; i <= nTimes; i++) { | |
| list.Add(what); | |
| } | |
| } | |
| public static int[] Indices<T>(this T[] array) { | |
| var indices = Enumerable.Range(0, array.Length).ToArray(); | |
| return indices; | |
| } | |
| public static int[] Indices<T>(this List<T> list) { | |
| var indices = Enumerable.Range(0, list.Count).ToArray(); | |
| return indices; | |
| } | |
| public static int SumInt(this int[] array) { | |
| var sum = 0; | |
| foreach (var elem in array) { | |
| sum += elem; | |
| } | |
| return sum; | |
| } | |
| public static int SumInt(this List<int> list) { | |
| var sum = 0; | |
| foreach (var elem in list) { | |
| sum += elem; | |
| } | |
| return sum; | |
| } | |
| public static float SumFloat(this float[] array) { | |
| float sum = 0; | |
| foreach (float elem in array) { | |
| sum += elem; | |
| } | |
| return sum; | |
| } | |
| public static float SumFloat(this List<float> list) { | |
| float sum = 0; | |
| foreach (var elem in list) { | |
| sum += elem; | |
| } | |
| return sum; | |
| } | |
| public static bool IsOutOfBounds<T>(this T[] array, int index) { | |
| return index < 0 || index >= array.Length; | |
| } | |
| public static bool IsOutOfBounds<T>(this List<int> array, int index) { | |
| return index < 0 || index >= array.Count; | |
| } | |
| // String | |
| public static string Reverse(string s) { | |
| char[] charArray = s.ToCharArray(); | |
| Array.Reverse(charArray); | |
| return new string(charArray); | |
| } | |
| public static int HexStringToInt(string hexString) { | |
| char[] reversedHexStringChars = hexString.ToUpper().ToCharArray(); | |
| reversedHexStringChars = reversedHexStringChars.Reverse().ToArray(); | |
| var charIntMap = new Dictionary<char, int>() { | |
| { '0', 0 }, | |
| { '1', 1 }, | |
| { '2', 2 }, | |
| { '3', 3 }, | |
| { '4', 4 }, | |
| { '5', 5 }, | |
| { '6', 6 }, | |
| { '7', 7 }, | |
| { '8', 8 }, | |
| { '9', 9 }, | |
| { 'A', 10 }, | |
| { 'B', 11 }, | |
| { 'C', 12 }, | |
| { 'D', 13 }, | |
| { 'E', 14 }, | |
| { 'F', 15 } | |
| }; | |
| int intNum = 0; | |
| for (int i = 0; i < reversedHexStringChars.Length; i++) { | |
| intNum += (int) charIntMap[reversedHexStringChars[i]] * ((int) Math.Pow(16, i)); | |
| } | |
| return intNum; | |
| } | |
| public static bool IsStringNumber(this string input) | |
| { | |
| if (string.IsNullOrWhiteSpace(input)) | |
| return false; | |
| string pattern = @"^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$"; | |
| return Regex.IsMatch(input, pattern); | |
| } | |
| public static bool StringContains(string s, char letter) { | |
| return s.Contains(letter); | |
| } | |
| public static string[] SplitString(string s, string delimiters) { | |
| var returnedList = new List<string>(); | |
| var lastLetterWasDelimiter = false; | |
| var firstCharIndex = 0; | |
| if (s.Length > 0){ | |
| while (StringContains(delimiters, s[firstCharIndex]) && firstCharIndex < s.Length){ | |
| firstCharIndex++; | |
| } | |
| } | |
| var start = firstCharIndex; | |
| var end = firstCharIndex; | |
| for (int i = firstCharIndex; i < s.Length; i++) { | |
| var letter = s[i]; | |
| if (StringContains(delimiters, letter)) { | |
| if (lastLetterWasDelimiter) { | |
| start = i + 1; | |
| } else { | |
| returnedList.Add(s.Substring(start, end - start)); | |
| start = i + 1; | |
| end = start; | |
| } | |
| lastLetterWasDelimiter = true; | |
| } else { | |
| lastLetterWasDelimiter = false; | |
| end = i + 1; | |
| } | |
| } | |
| if (!lastLetterWasDelimiter) { | |
| returnedList.Add(s.Substring(start)); | |
| } | |
| return returnedList.ToArray(); | |
| } | |
| public const int READING_WORD = 0; | |
| public const int DELIMITING = 1; | |
| public static string[] SmartSplitString(string s, string[] excludeDelimiters, string[] includeDelimiters) { | |
| var state = READING_WORD; | |
| var currentWordStart = 0; | |
| var currentWords = new List<string>(); | |
| for (int i = 0; i < s.Length; i++) { | |
| char chr = s[i]; | |
| Func<bool> foundExcludeDelimiter = () => excludeDelimiters.Contains(chr.ToString()); | |
| Func<bool> foundIncludeDelimiter = () => includeDelimiters.Contains(chr.ToString()); | |
| switch (state) { | |
| case READING_WORD: | |
| if (foundExcludeDelimiter()) { | |
| currentWords.Add(s.Substring(currentWordStart, i - currentWordStart)); | |
| currentWordStart = -1; | |
| state = DELIMITING; | |
| } else if (foundIncludeDelimiter()) { | |
| currentWords.Add(s.Substring(currentWordStart, i - currentWordStart)); | |
| currentWords.Add(chr.ToString()); | |
| currentWordStart = -1; | |
| state = DELIMITING; | |
| } else { | |
| Pass(); | |
| } | |
| break; | |
| case DELIMITING: | |
| if (foundIncludeDelimiter()) { | |
| currentWords.Add(chr.ToString()); | |
| } else if (foundExcludeDelimiter()) { | |
| Pass(); | |
| } else { | |
| currentWordStart = i; | |
| state = READING_WORD; | |
| } | |
| break; | |
| default: break; | |
| } | |
| } | |
| if (state == READING_WORD && currentWordStart != -1) { | |
| currentWords.Add(s.Substring(currentWordStart)); | |
| } | |
| return currentWords.ToArray(); | |
| } | |
| public static T[] MergeArrays<T>(T[] arr1, T[] arr2) { | |
| var arr = arr1.Union(arr2).ToArray(); | |
| return arr; | |
| } | |
| public static T[] RemoveDuplicates<T>(T[] array) { | |
| var elementsMap = new Dictionary<T, bool>(); | |
| foreach (var elem in array) { | |
| elementsMap[elem] = true; | |
| } | |
| return elementsMap.Keys.ToArray(); | |
| } | |
| public static void Shuffle<T>(T[] array) { | |
| for (int i = 0; i < array.Length; i++) { | |
| var aux = array[i]; | |
| var indexToSwap = RandomIntBetween(0, array.Length - 1); | |
| array[i] = array[indexToSwap]; | |
| array[indexToSwap] = aux; | |
| } | |
| } | |
| // Other | |
| public static void Pass() {} | |
| public static T NullOr<T>(T maybeNull, T notNull) { | |
| if (maybeNull == null) return notNull; | |
| return maybeNull; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment