Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- public class ReadWriter
- {
- private static Dictionary<Tuple<char, char>, int> Alphabet
- {
- get
- {
- var temp = new Dictionary<Tuple<char, char>, int>();
- for (int i = (int)'A'; i <= (int)'Z'; i++)
- {
- temp.Add(new Tuple<char, char>((char)i, (char)(i + 32)), 0);
- }
- return temp;
- }
- }
- public static Tuple<char, char> GetMostAndLeastCommonLetters(string path)
- {
- if (File.Exists(path))
- {
- var collector = File.ReadAllText(path);
- var alphabet = Alphabet;
- foreach (char nig in collector)
- {
- var crutch = alphabet.Keys.ToList().Find(x => (x.Item1 == nig) || (x.Item2 == nig));
- if (crutch != null)
- alphabet[crutch]++;
- }
- 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);
- }
- else throw new IOException("kys");
- }
- public static void ReplaceMostRareLetter(Tuple<char, char> leastAndMostCommon, string inputPath, string outputPath)
- {
- if (File.Exists(inputPath))
- {
- var collector = File.ReadAllText(inputPath);
- var modifier = new StringBuilder(collector);
- modifier.Replace(leastAndMostCommon.Item2, leastAndMostCommon.Item1);
- using (StreamWriter fs = new StreamWriter(new FileStream(outputPath, FileMode.Create, FileAccess.Write)))
- {
- fs.Write(modifier);
- }
- }
- else throw new IOException("kys");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement