fork(2) download
  1. def b(a):j=len(a[0]);g=range;z=g(len(a));h=[i for i in z if'#'in a[i]];w=[i for i,c in[(i,[r[i]for r in a])for i in g(j)]if'#'in c];return[[any((r<h[0],h[-1]<r,c<w[0],w[-1]<c))and'.'or'#'for c in g(j)]for r in z]
  2.  
  3. tests = [[['#']],
  4. [['.','.','.'],['#','.','.'],['.','.','.']],
  5. [['.','.','.'],['#','.','.'],['.','.','#']],
  6. [['.','#','.'],['#','.','.'],['.','.','#']],
  7. [['.','.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.','.'],['.','.','.','.','#','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.','.']],
  8. [['.','.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.','.'],['.','.','.','.','#','.','.','.','.','.'],['.','.','.','#','.','.','.','.','.','.']],
  9. [['.','.','.','.','.','.','.','.','.','.'],['.','.','#','.','.','.','.','.','.','.'],['.','.','.','.','#','.','.','.','.','.'],['.','.','.','#','.','.','.','.','.','.']],
  10. [['.','.','.','.','.','.','.','.','.','.'],['.','.','#','.','.','.','.','.','.','.'],['.','.','.','.','#','.','.','#','.','.'],['.','.','.','#','.','.','.','.','.','.']],
  11. [['.','.','.','.','.','.','.','.','.','#'],['.','.','#','.','.','.','.','.','.','.'],['.','.','.','.','#','.','.','#','.','.'],['.','.','.','#','.','.','.','.','.','.']]]
  12.  
  13. for n, test in enumerate(tests, 1):
  14. print 'Test %d'%n
  15. print '\n'.join(''.join(r) for r in test)
  16. print
  17. print '\n'.join(''.join(r) for r in b(test))
  18. print
  19.  
Success #stdin #stdout 0s 9024KB
stdin
Standard input is empty
stdout
Test 1
#

#

Test 2
...
#..
...

...
#..
...

Test 3
...
#..
..#

...
###
###

Test 4
.#.
#..
..#

###
###
###

Test 5
..........
..........
....#.....
..........

..........
..........
....#.....
..........

Test 6
..........
..........
....#.....
...#......

..........
..........
...##.....
...##.....

Test 7
..........
..#.......
....#.....
...#......

..........
..###.....
..###.....
..###.....

Test 8
..........
..#.......
....#..#..
...#......

..........
..######..
..######..
..######..

Test 9
.........#
..#.......
....#..#..
...#......

..########
..########
..########
..########