-2

Asked another way, if I showed you this masked code file, using only your human brain, is it possible to fix the indentation issues, even if you know it should be 2-space indentation?

  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    x
x

   xxxxxxxxxxxxxxxxxxxxxxxxxxx

I have my own ideas, but I don't want to bias the answer. The actual source code and language will be revealed after I get a good batch of answers. Feel free to post your fix as a code block below.

This test assumes the following:

  1. You have no idea the language in which this code is written.
  2. All you know is how many spaces or tabs lead up to the first character of each line. In this case, there are no tabs (just spaces).
  3. You know what the indent size should be. In this case, 2 spaces.

Note: If it's possible with your human brain, it should also be possible with code, right?

Bonus Points (optional): How would you break-down the logic to tackle this problem?

EDIT: Here's the source code, from which these exes were created:

function greet(firstName, lastName) {

    var firstName = prompt('What is your first name?');
    var lastName = prompt('Last name?');

    var fullName = firstName + ' ' + lastName;
    for (var i = 0; i < 10; i++) {
        console.log('Hello,', fullName + '!');
    }
}

greet(firstName, lastName);
jedmao
  • 10,224
  • 11
  • 59
  • 65
  • SnakeYAML from my understanding uses the indention like this (no tabs permitted, 2 spaces recommended). It was used in the Bukkit project for Minecraft, and I'm speaking from experience. https://github.com/asomov/snakeyaml/tree/master/src/main/java/org/yaml/snakeyaml <-- just one of many cloned projects. – CoderMusgrove Mar 20 '15 at 21:30
  • The 2-space indentation is irrelevant. It could just as well be 3- or 4-space indentation. – jedmao Mar 20 '15 at 21:31
  • 1
    This is not a puzzle site. If you want to frame your question in the form of a puzzle, explain it clearly and post it on [Programming Puzzles and Code Golf](http://codegolf.stackexchange.com/). They have rules for what it means to define a good puzzle and can help with that. But on StackOverflow you are expected to give those seeking to offer assistance all the best information you have to define your clear, real, problem. *(Note: at one time, programming puzzles were part of the things done here, so this is a change in policy.)* – HostileFork says dont trust SE Mar 20 '15 at 22:28
  • @HostileFork this is very much a real problem, for which I'm seeking unbiased answers. Can you tell me what part of the question is unclear or what information is missing so that it can't be answered? – jedmao Mar 20 '15 at 22:48
  • Without knowing the (block structure of the) language, you can't do this. The language may insist the particular syntax looks exactly like this. You want to narrow the class of langauges involved? – Ira Baxter Mar 20 '15 at 22:54
  • @IraBaxter I'm writing tools for the EditorConfig project, which doesn't have the luxury or project scope to know anything about the syntax tree. Let's assume the exes above could be any language out there – flying blind, so to speak. – jedmao Mar 20 '15 at 22:56
  • Pretty hard to be smart, when you have zero data. Even Sherlock couldn't solve puzzles without some evidence. – Ira Baxter Mar 20 '15 at 22:58
  • .... frankly, what you want is a tool that can re-indent given poor layout, not just bad indentation. – Ira Baxter Mar 20 '15 at 23:01
  • @IraBaxter you're right, because I'm not in control of the source code. We have to assume the absolute worst-case scenario. – jedmao Mar 20 '15 at 23:06

1 Answers1

1
 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                x
            x

    xxxxxxxxxxxxxxxxxxxxxxxxxxx
  • Thanks for the submission! What kind of language are you thinking would work with this indentation? At first, I'm thinking Python because you've indented the very last line, but don't you wonder what those solo x's are? They look an awful lot like `}`, don't they? Feel free to explain your reasoning for doing it the way you did and edit your post if needed. – jedmao Mar 20 '15 at 23:22
  • It would work with groovy and yes those lone x's looked a lot like } to me. In my mind there was a method or a nested if there. Hence to two lone x's. – Jody Albritton Mar 21 '15 at 20:33
  • A for effort. I posted the source code in the original post. – jedmao Mar 26 '15 at 17:20