Hey Mates,
I have much to do at the moment, so I can’t publish that often, but I try to do as much as I can .
However, I am currently studying for the “Bundeswettbewerb für Informatik” in germany which takes place in September (In english: Nationwide competition for informatics) and I thought these exercises could be interesting for you too! Whenever I’m finished with the next one I post the exercise description here and my solution, which will be probably in C#.
Description
This time you have to build a simple program which takes a territoy as input and prints out if it overlaps with other territories. The territories are given like this: 3385. The first two numbers are the x and y coordinate of the corner on the bottom left of the rectangle. The last two numbers represent the x and y coordinate of the top right point.
See the coordinate system below:
Here you can see 3 territories. The black and red one can get allowed, but the blue one overlaps, so it has to get disallowed!
Ok, I hope you understood the system.
My Solution
Here you can find a simple program in C# which takes the territories one by one and always prints out if they can get allowed or not.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Territory_Tester
{
class Program
{
static void Main(string[] args)
{
List<String> areas = new List<String>();
while (true)
{
//Get territory and validates
String territory = input("\nYour territory (e.g. 3385): ");
if (!validate(territory))
continue;
//Build a list of all points in the rectangle
List<String> points = getAllPoints(territory);
//Tests if the rectangle can be allowed
if(testTerritory(areas, points))
{
areas.Add(territory);
Console.WriteLine("[*] This territory can get allowed!");
}
else
{
Console.WriteLine("[!] This territory can't get allowed!");
}
}
}
/// <summary>
/// Tests if the territory overlaps
/// </summary>
/// <param name="areas"></param>
/// <param name="points"></param>
/// <returns></returns>
static bool testTerritory(List<String> areas, List<String> points)
{
foreach(String terr in areas)
{
foreach(String p in points)
{
int x = int.Parse(p[0].ToString());
int y = int.Parse(p[1].ToString());
if(x < int.Parse(terr[2].ToString()) && x > int.Parse(terr[0].ToString()))
{
if (y < int.Parse(terr[3].ToString()) && y > int.Parse(terr[1].ToString()))
return false;
}
}
}
return true;
}
/// <summary>
/// This function builds a list with all points in the rectangle
/// </summary>
/// <param name="territory"></param>
/// <returns></returns>
static List<String> getAllPoints(String territory)
{
int x = int.Parse(territory[0].ToString());
int y = int.Parse(territory[1].ToString());
int xMax = int.Parse(territory[2].ToString());
int yMax = int.Parse(territory[3].ToString());
List<String> points = new List<String>();
while (x <= xMax)
{
while(y <= yMax)
{
String p = x.ToString() + y.ToString();
points.Add(p);
y += 1;
}
x += 1;
y = int.Parse(territory[1].ToString());
}
return points;
}
/// <summary>
/// Tests whether it's a valid territory
/// </summary>
/// <param name="territory"></param>
/// <returns></returns>
static bool validate(String territory)
{
//Tests the length
if (territory.Length != 4)
{
Console.WriteLine("\n[!] Not a valid territory!");
return false;
}
//Tests whether the input is a number
try
{
int.Parse(territory);
}
catch
{
Console.WriteLine("\n[!] Not a valid territory!");
return false;
}
return true;
}
/// <summary>
/// Just a small input with given text function. It's like input() in Python.
/// </summary>
/// <param name="text"></param>
/// <returns>User input</returns>
static String input(String text)
{
Console.Write(text);
return Console.ReadLine();
}
}
}
Conclusion
This can be seen as a good programming challenge. I think it’s good for learning to build up a strategy first and then work on accomplishing it. As always I hope you have fun with trying to solve this easy challenge.
|-TheDoctor-|