Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace Erettsegi1
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Record> records = new List<Record>();
- // 1. Read file contents
- foreach (string line in File.ReadAllLines("jarmu.txt"))
- records.Add(new Record(line.Split(' ')));
- // 2. Worktime
- Console.WriteLine("2. feladat:\n{0}", records[records.Count - 1].Hour - records[0].Hour);
- // 3. First occ. by hours
- Console.WriteLine("\n3. feladat:");
- int currentHour = records[0].Hour;
- foreach (KeyValuePair<int, Record> kp in records.Where(i => { if (i.Hour == currentHour) { currentHour++; return true; } return false; }).ToDictionary(i => i.Hour))
- Console.WriteLine("{0} óra: {1}", kp.Key.ToString(), kp.Value.PlateNumber);
- // 4. Categories
- Console.WriteLine("\n4. feladat:");
- Console.WriteLine("Autóbusz: {0}", records.Where(i => i.PlateNumber.ToCharArray()[0] == 'B').Count());
- Console.WriteLine("Kamion: {0}", records.Where(i => i.PlateNumber.ToCharArray()[0] == 'K').Count());
- Console.WriteLine("Motor: {0}", records.Where(i => i.PlateNumber.ToCharArray()[0] == 'M').Count());
- // 5. Longest quiet period
- Console.WriteLine("\n5. feladat:");
- Console.WriteLine(getLongestQuietPeriod(records));
- // 6. Search
- Console.WriteLine("\n6. feladat:");
- string search = Console.ReadLine();
- foreach (Record r in records.Where(getMatchingCars(search)))
- Console.WriteLine(r.PlateNumber);
- // 7. Get observed cars
- Console.WriteLine("\n7. feladat:");
- File.WriteAllLines("vizsgalt.txt", getRecordListData(getObservedCars(records)));
- Console.ReadLine();
- }
- static string getLongestQuietPeriod(List<Record> records)
- {
- long maxdiff = 0;
- DateTime minTime = DateTime.MinValue, maxTime = DateTime.MinValue;
- for (int i = 1; i < records.Count; i++)
- {
- var diff = records[i].GetTime().Ticks - records[i - 1].GetTime().Ticks;
- if (diff > maxdiff)
- {
- minTime = records[i - 1].GetTime();
- maxTime = records[i].GetTime();
- maxdiff = diff;
- }
- }
- return String.Format("{0} - {1}", minTime.ToString("HH:mm:ss"), maxTime.ToString("HH:mm:ss"));
- }
- static Func<Record, Boolean> getMatchingCars(string query)
- {
- return new Func<Record, bool>(i => {
- return new Regex(Regex.Escape(query).Replace(@"\*", ".*")).IsMatch(i.PlateNumber);
- });
- }
- static List<Record> getObservedCars(List<Record> records)
- {
- List<Record> results = new List<Record>();
- results.Add(records[0]);
- long lastObservedTime = records[0].GetTime().Ticks;
- long fiveMins = DateTime.MinValue.AddMinutes(5).Ticks;
- for(int i = 1; i < records.Count; i++)
- {
- if (records[i].GetTime().Ticks - lastObservedTime >= fiveMins)
- {
- results.Add(records[i]);
- lastObservedTime = records[i].GetTime().Ticks;
- }
- }
- return results;
- }
- static List<String> getRecordListData(List<Record> records)
- {
- List<String> results = new List<string>();
- foreach (Record r in records)
- results.Add(String.Format("{0} {1}", r.GetTime().ToString("HH mm ss"), r.PlateNumber));
- return results;
- }
- }
- public class Record
- {
- public int Hour;
- public int Min;
- public int Sec;
- public string PlateNumber;
- public Record(string[] data)
- {
- Hour = int.Parse(data[0]);
- Min = int.Parse(data[1]);
- Sec = int.Parse(data[2]);
- PlateNumber = data[3];
- }
- public DateTime GetTime()
- {
- return new DateTime(1970, 1, 1, Hour, Min, Sec);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement