Skip to content

2018-1: Oh Say Can You See?

Imagine standing in front of a line of skyscrapers of varying heights. Assume that you can always see a skyscraper that is taller than a closer skyscraper. For example, a person on the left of the diagram below can see 3 skyscrapers – the first, fourth, and sixth from left to right. In contrast, a person on the right can see 2 skyscrapers – the seventh and sixth.

                                        ┌───┐        
                                        │   │
                                        │   │
                                        │   │
                                        │   │
                          ┌───┐         │   │  ┌───┐
                          │   │         │   │  │   │       
                          │   │         │   │  │   │ 
                          │   │         │   │  │   │  
                          │   │         │   │  │   │
     ┌───┐  ┌───┐         │   │         │   │  │   │ 
     │   │  │   │         │   │         │   │  │   │ 
     │   │  │   │         │   │  ┌───┐  │   │  │   │
     │   │  │   │  ┌───┐  │   │  │   │  │   │  │   │
     │   │  │   │  │   │  │   │  │   │  │   │  │   │ 
     │ 5 │  │ 5 │  │ 2 │  │ 10 │  │ 3 │  │ 15 │  │ 10 │ 
    ─┘   └──┘   └──┘   └──┘   └──┘   └──┘   └──┘   └─

Write an APL function that, given a scalar or vector of skyscraper heights from closest to furthest, will return an integer representing the number of skyscrapers that can be seen.

Examples:

     (your_function) 5 5 2 10 3 15 10 ⍝ from the left person's view
3
     (your_function) ⌽ 5 5 2 10 3 15 10 ⍝ from the right person's view
2
     (your_function) ⍬   ⍝ no skyscrapers here!
0
     (your_function) 10  ⍝ single skyscraper
1
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub