Skip to content

Instantly share code, notes, and snippets.

@disolovyov
Created July 12, 2012 06:01
Show Gist options
  • Select an option

  • Save disolovyov/3096163 to your computer and use it in GitHub Desktop.

Select an option

Save disolovyov/3096163 to your computer and use it in GitHub Desktop.
Coding Dojo 2012-07-11
+
+
+ +
+
+
+ +
+ +
+ +
+
+
+ +
+ +
+ +
+ +
+ +
+
star :: Int -> String
star n = unlines [[if x + y == n + 1 then '+' else ' ' | x <- idx] | y <- idx]
where idx = [1..n] ++ reverse [1..n - 1]
main :: IO ()
main = do
putStrLn $ star 1
putStrLn $ star 2
putStrLn $ star 3
putStrLn $ star 4
import java.util.ArrayList;
import java.util.List;
public class Star {
private static List<Integer> makeIndices(int size) {
List<Integer> indices = new ArrayList<Integer>(size * 2 - 1);
for (int i = 1; i <= size; i++) {
indices.add(i);
}
for (int i = size - 1; i >= 1; i--) {
indices.add(i);
}
return indices;
}
private static String star(int size) {
StringBuilder builder = new StringBuilder((size + 1) * size);
List<Integer> indices = makeIndices(size);
for (int x : indices) {
for (int y : indices) {
builder.append((x + y == size + 1) ? '+' : ' ');
}
builder.append('\n');
}
return builder.toString();
}
public static void main(String[] args) {
System.out.println(star(1));
System.out.println(star(2));
System.out.println(star(3));
System.out.println(star(4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment