2

I thought the problem of rotating a number 180 degrees clockwise ( or ccw) and getting the same number. For all digits, except 3, 4 and 7, the rotated one is a valid digit. (rotate 3 and you get ε). I'm new in C# but I managed to solve it.

 public static bool MyMethod(int originalInt)
        {
            bool is180 = false;

            if (Array.IndexOf(originalInt.ToString().ToArray(), '3') > -1 || Array.IndexOf(originalInt.ToString().ToArray(), '4') > -1 || Array.IndexOf(originalInt.ToString().ToArray(), '7') > -1)
            {
                return false;
            }
            else
            {
                List<int> tempList = new List<int>();
                int tempInt = originalInt;
                do
                {
                    int lastDigit = tempInt % 10;
                    if (lastDigit == 9)
                    {
                        lastDigit = 6;
                    }
                    else if (lastDigit == 6)
                    {
                        lastDigit = 9;
                    }
                    tempInt = tempInt / 10;
                    tempList.Add(lastDigit);

                }
                while (tempInt > 0);

                tempList.Reverse();

                int tempInt2 = originalInt;
                int lastDigit2 = 0;
                foreach (int item in tempList)
                {
                    lastDigit2 = tempInt2 % 10;
                    if (item == lastDigit2)
                    {
                        is180 = true;
                        tempInt2 = tempInt2 / 10;
                    }
                    else 
                    {
                        return false;
                    }
                }
            }
            return is180;
        }

Can you find a way of solving this simpler? Thank you.

Hector Sanchez
  • 2,297
  • 4
  • 26
  • 39
iusmar
  • 339
  • 1
  • 6
  • 15
  • 1
    It's not clear what this is being used for, but you may want to take a look at the System.Drawing.Graphics object if this is WinForms. – Adam Plocher Oct 16 '13 at 22:44
  • Isn't it a bit of a stretch to say that '2' rotated looks like '5'? That requires a mirror operation, and even then it doesn't really look like a '5' unless you have crazy handwriting. – paddy Oct 16 '13 at 22:45
  • 1
    This looks like a Rube Goldberg coding challenge! – Kevin Oct 16 '13 at 22:45
  • Maybe if you gave an example of input and expected output that would make it easier for us to see what the solution should be. – Kevin Oct 16 '13 at 22:46
  • 1
    I suppose what you say could be true, if you were using a font that resembled a seven-segment display. – Jonathon Reinhart Oct 16 '13 at 22:49
  • 1 rotated returns 1; 2->2; 5->5; 6->9; 8->8; 9->6; 0->0. It's sort of a mirror operation. You don't get 2 rotated 5, and viceversa; you just rotate de numbers 180degrees. Take a seven-segment display and rotate for every digit and see what you get. – iusmar Oct 16 '13 at 22:49
  • Just to be picky, rotating '1' on a seven-segment display does not yield the seven-segment display's representation of '1'. =P – paddy Oct 16 '13 at 22:58
  • The way to simplify this code is to break it apart into multiple methods and then simplify each of them. – Eric Lippert Oct 16 '13 at 23:15

3 Answers3

3

Pseudocode:

map['0'] = '0';
map['1'] = '1';
map['2'] = '2';
map['5'] = '5';
map['6'] = '9';
map['8'] = '8';
map['9'] = '6';

for each position i in input_string :
    if map index exists for input_string[i] :
        rotated_string[i] = map[input_string[i]]
    else
        exit for

rotated_string = reverse(rotated_string)

if input_string = rotated_string :
    has_rotational_symmetry = true
else
    has_rotational_symmetry = false
paddy
  • 60,864
  • 6
  • 61
  • 103
1

I'm not 100% sure what you are asking.. but the following returns true and false properly for your method...

Edit: Now with Lippertization!

public static bool MyMethod(int originalInt)
{
        var s = originalInt.ToString();
        return !(s.Contains('3') || s.Contains('4') || s.Contains('7'));
}
Kevin
  • 4,586
  • 23
  • 35
  • You could shorten that to `return !(s.Contains('3') || s.Contains('4') || s.Contains('7'));` – Eric Lippert Oct 16 '13 at 23:12
  • @EricLippert I definitely should have but in my defense this was a throw away response since I really don't 'get' the utility of this question. Especially since the output of the re-ordering in the listed code doesn't seem to be used. I was very tempted to go regex on it just for fun. – Kevin Oct 16 '13 at 23:26
0

Couldn't resist an F# version:

let is180 s =
    let rd = function|'0'->'0'|'1'->'1'|'2'->'2'|'5'->'5'|'6'->'9'|'8'->'8'|'9'->'6'|_->'X'
    let flip x = new string(x |> Seq.map rd |> Seq.toArray |> Array.rev)
    s.Equals(flip s)
lesscode
  • 6,221
  • 30
  • 58