Advertisement
tane_superior

Untitled

Jun 9th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. public class ReadWriter
  8. {
  9.     private static Dictionary<Tuple<char, char>, int> Alphabet
  10.     {
  11.         get
  12.         {
  13.             var temp = new Dictionary<Tuple<char, char>, int>();
  14.             for (int i = (int)'A'; i <= (int)'Z'; i++)
  15.             {
  16.                 temp.Add(new Tuple<char, char>((char)i, (char)(i + 32)), 0);
  17.             }
  18.             return temp;
  19.         }
  20.     }
  21.  
  22.     public static Tuple<char, char> GetMostAndLeastCommonLetters(string path)
  23.     {
  24.         if (File.Exists(path))
  25.         {
  26.             var collector = File.ReadAllText(path);
  27.             var alphabet = Alphabet;
  28.  
  29.             foreach (char nig in collector)
  30.             {
  31.                 var crutch = alphabet.Keys.ToList().Find(x => (x.Item1 == nig) || (x.Item2 == nig));
  32.                 if (crutch != null)
  33.                     alphabet[crutch]++;
  34.             }
  35.             return new Tuple<char, char>(alphabet.Keys.ToList().Find(x => alphabet.Values.Max() == alphabet[x]).Item1, alphabet.Keys.Where(x => alphabet[x] != 0).ToList().Find(x => (alphabet.Values.Min() == alphabet[x])).Item1);
  36.         }
  37.         else throw new IOException("kys");
  38.     }
  39.  
  40.     public static void ReplaceMostRareLetter(Tuple<char, char> leastAndMostCommon, string inputPath, string outputPath)
  41.     {
  42.         if (File.Exists(inputPath))
  43.         {
  44.             var collector = File.ReadAllText(inputPath);
  45.             var modifier = new StringBuilder(collector);
  46.             modifier.Replace(leastAndMostCommon.Item2, leastAndMostCommon.Item1);
  47.             using (StreamWriter fs = new StreamWriter(new FileStream(outputPath, FileMode.Create, FileAccess.Write)))
  48.             {
  49.                 fs.Write(modifier);
  50.             }
  51.         }
  52.         else throw new IOException("kys");
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement