Ну вот, все тесты готовы. Завтра надо заслать. Хотя думаю что это уже не имеет значения.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
/**
* Generates various statistics about the tweets data set returned by the given TweetsApiService instance.
*/
namespace CodeScreen.Assessments.TweetsApi
{
class TweetDataStatsGenerator
{
private readonly TweetsApiService TweetsApiService;
private List<Tweet> Tweets { get; set; }
public TweetDataStatsGenerator(TweetsApiService tweetsApiService)
{
TweetsApiService = tweetsApiService;
}
public void LoadTweets(string userName)
{
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("User name cannot be empty.");
Tweets ??= TweetsApiService.GetTweetsAsync(userName).Result;
}
/**
* Retrieves the highest number of tweets that were created on any given day by the given user.
*
* A day's time period here is defined from 00:00:00 to 23:59:59
* If there are no tweets for the given user, this method should return 0.
*
* @param userName the name of the user
* @return the highest number of tweets that were created on a any given day by the given user
*/
public int GetMostTweetsForAnyDay(string userName)
{
LoadTweets(userName);
if (!Tweets.Any())
return 0;
var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
var maxTweetsPerDay = 0;
var perDayCounter = 0;
foreach (var tweet in Tweets.OrderBy(x => x.DateCreated))
{
if (date.Day != tweet.DateCreated.Day)
{
if (maxTweetsPerDay < perDayCounter)
{
maxTweetsPerDay = perDayCounter;
perDayCounter = -1;
}
}
else
perDayCounter++;
date = tweet.DateCreated;
}
return maxTweetsPerDay;
}
/**
* Finds the ID of longest tweet for the given user.
*
* You can assume there will only be one tweet that is the longest.
* If there are no tweets for the given user, this method should return null.
*
* @param userName the name of the user
* @return the ID of longest tweet for the given user
*/
public string GetLongestTweet(string userName)
{
LoadTweets(userName);
if (!Tweets.Any())
return "";
var longestTweet = Tweets.OrderByDescending(x => x.Text.Length).First();
return longestTweet.Id;
}
/**
* Retrieves the most number of days between tweets by the given user, wrapped as an OptionalInt.
*
* This should always be rounded down to the complete number of days, i.e. if the time is 12 days & 3 hours, this
* method should return 12.
* If there are no tweets for the given user, this method should return 0.
*
* @param userName the name of the user
* @return the most number of days between tweets by the given user
*/
public int FindMostDaysBetweenTweets(string userName)
{
LoadTweets(userName);
if (!Tweets.Any())
return 0;
var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
var daysBetween = 0;
foreach (var tweet in Tweets.OrderBy(x => x.DateCreated))
{
var timeRange = tweet.DateCreated - date;
if (daysBetween < timeRange.Days)
daysBetween = timeRange.Days;
date = tweet.DateCreated;
}
return daysBetween;
}
/**
* Retrieves the most popular hash tag tweeted by the given user.
*
* Note that the string returned by this method should include the hashtag itself.
* For example, if the most popular hash tag is "#Java", this method should return "#Java".
* If there are no tweets for the given user, this method should return null.
*
* @param userName the name of the user
* @return the most popular hash tag tweeted by the given user.
*/
public string GetMostPopularHashTag(string userName)
{
LoadTweets(userName);
if (!Tweets.Any())
return "";
var regex = new Regex(@"#\w+");
var hashTags = new Dictionary<string, int>();
foreach (var matches in Tweets.Select(tweet => regex.Matches(tweet.Text)))
{
foreach (var match in matches)
{
var key = match.ToString();
if (string.IsNullOrEmpty(key))
continue;
if (!hashTags.ContainsKey(key))
hashTags.Add(key, 1);
else
hashTags[key] += 1;
}
}
var mostPopularTag = hashTags.OrderByDescending(x => x.Value).FirstOrDefault();
return mostPopularTag.Key ?? "";
}
}
}