14
1
A convenient and useful way to represent topological surfaces is with a fundamental polygon. Each side on a polygon matches to another side and can be either parallel or anti-parallel. For instance the here is the fundamental polygon of a torus:
To figure out why this is a torus we could imagine our polygon being a sheet of paper. To make the proper surface we want to bend our paper so that the corresponding edges line up with their arrows going the same way. For our torus example we can start by rolling the paper into a cylinder so that the two blue edges (labeled b) are connected. Now we take our tube and bend it so that the two red edges (labeled a) connect to each other. We should have a donut shape, also called the torus.
This can get a bit trickier. If you try to do the same with the following polygon where one of the edges is going in the opposite direction:
you might find yourself in some trouble. This is because this polygon represents the Klein bottle which cannot be embedded in three dimensions. Here is a diagram from wikipedia showing how you can fold this polygon into a Klein bottle:
As you may have guessed the task here is to take a fundamental polygon and determine which surface it is. For four sided polygons (the only surfaces you will be required to handle) there are 4 different surfaces.
They are
Torus
Klein Bottle
Sphere
Projective plane
Now this is not image-processing so I don't expect you to take an image as input instead we will use a convenient notation to represent the fundamental polygon. You may have noticed in the two examples above that I named corresponding edges with the same letter (either a or b), and that I gave the twisted edge an additional mark to show its twisted. If we start at the upper edge and write down the label for each edge as we go clockwise we can get a notation that represents each fundamental polygon.
For example the Torus provided would become abab and the Klein Bottle would become ab-ab. For our challenge we will make it even simpler, instead of marking twisted edges with a negative we will instead make those letters capitalized.
Task
Given a string determine if it represents a fundamental polygon and output a value that corresponding to the proper surface of it is. You do not need to name the surfaces exactly, you just need 4 output distinct values each representing one of the 4 surfaces with a fifth value representing improper input. All of the basic cases are covered in the Simple Tests section, every car will be isomorphic to one of the or invalid.
Rules
Sides will not always be labeled with a and b, but they will always be labeled with letters.
Valid input will consist of 4 letters, two of one type and two of another. You must always output the correct surface for valid input.
You should reject (not output any of the 4 values representing surfaces) invalid input. You may do anything when rejecting an input, as long as it is distinguishable from the 4 surfaces
This is code-golf so the goal is to minimize the number of bytes in your source code.
Tests
Simple Tests
abab Torus
abAb Klein Bottle
abaB Klein Bottle
abAB Projective Plane
aabb Klein Bottle
aAbb Projective Plane
aabB Projective Plane
aAbB Sphere
abba Klein Bottle
abBa Projective Plane
abbA Projective Plane
abBA Sphere
Trickier Tests
ABAB Torus
acAc Klein Bottle
Emme Projective Plane
zxXZ Sphere
aaab Bad input
abca Bad input
abbaa Bad input
ab1a Bad input
Why is
abab
a torus andaabb
a Klein bottle? – Neil – 2017-05-20T20:57:23.867@Neil
– Post Rock Garf Hunter – 2017-05-20T21:00:13.993abab
is the example in the first paragraph, you can look there for an explanation. Here is an image showing whyaabb
is the same asabAb
which is a Klein bottle.Does this have to do with your new language Klein by the way? – Erik the Outgolfer – 2017-05-20T21:29:05.140
"...but they will always be labeled with letters" what does this add to the challenge, why not allow any base four representation as input? – Jonathan Allan – 2017-05-20T21:31:39.853
@JonathanAllan I'm not sure what you mean by base four representation. I chose letters because other symbols usually don't have upper and lower case versions. – Post Rock Garf Hunter – 2017-05-20T21:38:15.860
You have a representation for which valid input is a subset of the four digit base four numbers (i.e. your digits are
a
,b
,A
, andB
). – Jonathan Allan – 2017-05-20T21:40:40.247@JonathanAllan I don't think I would want to do that. The challenge is really an input processing challenge, there is not a lot of actual math going on behind the scenes. I think turning it into a base 4 challenge would make it too simple. The challenge is pretty simple as is. – Post Rock Garf Hunter – 2017-05-20T21:59:15.833
Well I have not worked out all the rules, but maybe I am simple :p – Jonathan Allan – 2017-05-20T22:07:50.017
1What bad inputs do we have to handle and identify as bad? All possible strings? Printable ASCII? Any limits on length? If we write a function, might it be passed a non-string object? Really, this whole input processing business strikes me as a chameleon challenge. – xnor – 2017-05-21T07:29:33.290
@xnor You will only receive strings other than that they may contain anything. Don't get me wrong this challenge is an input processing challenge. Any reasonable solution will do very little actual math. – Post Rock Garf Hunter – 2017-05-21T07:35:54.547
1@WheatWizard In that case, can you please make that clear in the title and body? It reads like math all the way until Rules, and even there it's easy to miss the gamechanging requirement to validate rather than just classify. – xnor – 2017-05-21T07:39:38.867
2Separately, I think an explanation is missing of what makes a string be classified into a given category, since you seem not to expect people to do out the math behind the classification. I think I could puzzle out the rules from test cases, but that's far from ideal. – xnor – 2017-05-21T07:43:05.157
@xnor I've hopefully made it clear it's a input processing challenge if it's not feel free to inform me or edit it yourself. However I can't implement your second suggestion at this moment. I assure you every possible shape is covered in the simple cases section with the only variation being choice of letter or inferring the case on a pair, but I'll try to make it clear later. – Post Rock Garf Hunter – 2017-05-21T07:48:43.203
so a sphere is a polygon? – Khaled.K – 2017-05-22T06:27:31.263