Skip to content

Instantly share code, notes, and snippets.

@miku
Last active February 13, 2017 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/6c5d3858736c631c9f0d82fffe621856 to your computer and use it in GitHub Desktop.
Save miku/6c5d3858736c631c9f0d82fffe621856 to your computer and use it in GitHub Desktop.
Puzzle
#!/usr/bin/python
# coding: utf-8
from __future__ import print_function
import sys
def filled(seq):
"""
Given a sequence of non-negative numbers, determine the units of "water",
that could be contained, if we interpret the numbers in `seq` as heights of
vertical bars.
Example:
Given: [1, 0, 3]
X X
X X
X_X could be filled with 1 unit of water: X█X
Given: [2, 0, 1, 2, 0, 1]
X X X██X
X_XX_X could be filled with 4 units of water: X█XX█X
And so on.
"""
pass # TODO: implement this function, return the sum of water units given a list of integers
if __name__ == '__main__':
print(filled([1, 2, 3]))
print(filled([2, 1, 3]))
print(filled([2, 1, 1, 2]))
print(filled([2, 1, 0, 2]))
print(filled([2, 1, 0, 2, 0, 1]))
print(filled([2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 7, 4, 1]))
# python main.py
# 0
# 1
# 2
# 3
# 4
# 33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment