#codingexercise
Generate a 2D distance matrix for the nearest distance from each cell as city location to a parking spot that appears as one or more occupied cells.
Generate a 2D distance matrix for the nearest distance from each cell as city location to a parking spot that appears as one or more occupied cells.
static int[][] getParkingDistanceGrid(int cityLength, int cityWidth, int[] parkingXCoordinates, int[] parkingYCoordinates) {
var d = new int[cityWidth][];
for (int i = 0; i < cityWidth; i++)
d[i] = new int[cityLength];
for (int i = 0; i < cityWidth; i++)
{
for (int j = 0; j < cityLength; j++)
{
var parkingdistances = new List<int>();
for (int count = 0; count < parkingXCoordinates.Length; count++)
{
int distance = GetDistance(i+1, j+1, parkingXCoordinates[count], parkingYCoordinates[count]);
parkingdistances.Add(distance);
}
int min = INT_MAX;
for (int k = 0; k < parkingXCoordinates.Length; k++)
if (parkingdistances[k] < min)
min = parkingdistances[k];
d[i][j] = min;
}
}
return d;
}
static int GetDistance(int r1, int c1, int r2, int c2)
{
int deltax = r1 - r2;
deltax = deltax > 0 ? deltax : 0-deltax;
int deltay = c1 - c2;
deltay = deltay > 0 ? deltay : 0-deltay;
return deltax + deltay;
}
Generate a recommendation list of courses taken by the social circle of a user. The recommendation must include courses taken by the members of her social circle but not by her. It must be ranked by the number of attendees to the course. A social circle consists only of the persons direct friends and their direct friends.
public List<string> getRankedCourses(string user) {var coursePopularity = new Dictionary<string, int>();
var circle = new List<string>();
var direct = getDirectFriendsForUser(user);
circle.AddRange(direct);
foreach (var friend in direct){
if (friend != user){
var directForFriend = getDirectFriendsForUser(friend);
directForFriend.Remove(user);
circle.AddRange(directForFriend);
}
}
foreach (var person in circle)
{
var attended = getAttendedCoursesForUser(person);
foreach(var course in attended)
{
if (!coursePopularity.ContainsKey(course))
{
coursePopularity.Add(course,1);
}
else
{
coursePopularity[course] += 1;
}
}
}
foreach(var attending in getAttendedCoursesForUser(user))
{
coursePopularity.Remove(attending);
}
List<KeyValuePair<string, int>> ranked = coursePopularity.ToList();
ranked.Sort(
delegate(KeyValuePair<string, int> pair1,
KeyValuePair<string, int> pair2)
{
return pair1.Value.CompareTo(pair2.Value);
}
);
var recommendations = new List<string>();
foreach (var kvp in ranked)
recommendations.Add(kvp.Key);
recommendations.Reverse();
return recommendations;
}
No comments:
Post a Comment