Advertisement
Guest User

Untitled

a guest
Aug 31st, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. sing System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5.  
  6. public static class Kata
  7. {
  8.  
  9.         public static string HighAndLow(string numbers)
  10.         {
  11.             var numberArray = StringToIntArray(numbers);
  12.             var max = numberArray.Max();
  13.             var min = numberArray.Min();
  14.             return max + " " + min;
  15.         }
  16.  
  17.         private static List<int> StringToIntArray(string numbersString)
  18.         {
  19.             var stringList = numbersString.ToList();
  20.             var numbersList = new List<int>();
  21.             for (int i = 0; i < stringList.Count(); i++)
  22.             {
  23.                 if (numbersString[i].Equals('-'))
  24.                 {
  25.                     string negativeNumber = numbersString[i].ToString() + MulitpleDigitNumberExtraction(numbersString, ref i);
  26.                     numbersList.Add(int.Parse(negativeNumber, NumberStyles.Any));
  27.                     i++;
  28.                 }
  29.                 else if(Char.IsDigit(numbersString[i]))
  30.                 {
  31.                     string numbers = numbersString[i].ToString();
  32.                     numbers += MulitpleDigitNumberExtraction(numbersString, ref i);
  33.                     numbersList.Add(int.Parse(numbers));
  34.                 }
  35.             }
  36.             return numbersList;
  37.         }
  38.  
  39.         private static string MulitpleDigitNumberExtraction(string numbersString, ref int i)
  40.         {
  41.             bool nextCharIsDigit = true;
  42.             string numbers = "";
  43.             while (nextCharIsDigit)
  44.             {
  45.                 if (i + 1 == numbersString.Count())
  46.                 {
  47.                     break;
  48.                 }
  49.                 if (Char.IsDigit(numbersString[i + 1]))
  50.                 {
  51.                     numbers += numbersString[i + 1];
  52.                     i++;
  53.                 }
  54.                 else
  55.                 {
  56.                     nextCharIsDigit = false;
  57.                 }
  58.             }
  59.             return numbers;
  60.         }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement