注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
Stripe Tech Phone interview.
- # For this interview, imagine that we are working with a simple database. Each row
- # associates column names (strings) with integer values (for example: 5, 0, -3,
- # and so on). Here's a table with three rows:
-
- # a b c d
- # ---------------
- # 1 0 0 0
- # 0 2 3 0
- # 0 0 0 4
-
- # We can choose to represent a database table in JSON as an array of objects. For
- # example, the previous table could be written as:
-
- # [ {"a": 1, "b": 0, "c": 0, "d": 0},
- # {"a": 0, "b": 2, "c": 3, "d": 0},
- # {"a": 0, "b": 0, "c": 0, "d": 4} ]
-
- # (There is no need to use JSON in your solutions -- the notation is just used to
- # introduce and explain the problems.)
-
- # Write a function minByColumn that takes a database table (as above), along with a
- # column, and returns the row that contains the minimum value for the given column.
- # If a row doesn't have any value for the column, it should behave as though the
- # value for that column was zero.
-
- # In addition to writing this function, you should use tests to demonstrate that it's
- # correct, either via an existing testing system or one you create.
- ## Examples
-
- # table1 = [
- # {"a": 1},
- # {"a": 2},
- # {"a": 3}
- # ]
- # minByColumn(table1, "a") returns {"a": 1}
- # In part 1 you may have noticed that it's possible for two rows to be "tied",
- # meaning that either would be an acceptable return value from minByColumn.
- # Consider:
-
- # table4 = [
- # {"a": 1, "b": 2},
- # {"a": 1, "b": 3},
- # {"a": 1, "b": 4}
- # ]
- # minByColumn(table4, "a") returns ???
-
- # Since all three rows have the same value for a, all three rows are acceptable
- # candidates to be returned by minByColumn(table, "a").
-
- # In these cases it would be nice if users could specify additional columns (e.g. "b")
- # to use as tie-breakers. A tie-breaker would only apply in cases where multiple rows
- # share the same minimum value. In `table4` above, the row {"a": 1, "b": 2} is
- # tied for the smallest "a" value (1) and of all the tied candidates, it has the
- # smallest "b" value (2). If two records had equal values for "a" and also for "b" then
- # another tie-breaker (e.g. "c") could be used. When records are tied with respect to
- # all columns, either record may be considered the minimum.
-
- # Write a function minByOrder that takes a database table and a list of columns, and
- # returns the row with the minimum column values using the tie-breaking logic above.
- # If only one column is provided, then the behavior of minByOrder is identical to
- # passing that column to minByColumn:
-
- # minByOrder(table, [column]) is equal to minByColumn(table, column)
-
- # As in Part 1, you should use tests to demonstrate that it's correct, either via an
- # existing testing system or one you create.
-
- # ## Examples
- # table5 = [
- # {"x": 1, "y": 3},
- # {"x": 1, "y": 0}
- # ]
复制代码
面試時傻了,沒仔細想清楚這題目的真正問題。直接硬上寫了很多方程式來解決問題。
面分享我的解法
- import sys
- def getValue(item, key):
- # since we find the smallest one,
- # default to sys max int if there is no key is provided in the item (column).
- return item.get(key, sys.maxint)
- def minByColumn(table, column):
- return sorted(table, key=lambda i: getValue(i, column))[0]
- def minByOrder(table, columns):
- return sorted(table, key=lambda i: tuple([getValue(i, c) for c in columns]))[0]
复制代码
歡迎大家不吝嗇指教
|