Redian新闻
>
打算烧点MA或V
avatar
打算烧点MA或V# Stock
b*n
1
写了200多行,2个小时,这电面肯定要跪
大牛有什么好的算法吗?
题目就是flatten json to a list of map, 有一段json,比如说如下:
{
"uuid": "abc",
"properties": {
"sessionName": "Test session name",
"waypoints": [
{"uuid": "def", "properties": {"latitude": 3}}
]
}
}
把它转化成List>, map里面uuid是key, properties是value。
所以结果应该像下面
[
{"uuid": "abc", "properties": {"sessionName": "Test session name", "
waypoints": ["def"]}},
{"uuid": "def", "properties": {"latitude": 3}},
...
]
avatar
w*d
2
V看起来更易燃,LOL
avatar
b*5
3
uber好像比较喜欢问json的问题。 你这种肯定要用GSON或者jackson json来parse
你看看我的blog
http://matuanstepmom.blogspot.com/2015/05/java-8.html, 里面有个flatmap的题目, 好像就是uber出来的
你这个还需要用GSON或者jackson json去parse一下, 把这个json转成hashmap《
string, object》

【在 b*********n 的大作中提到】
: 写了200多行,2个小时,这电面肯定要跪
: 大牛有什么好的算法吗?
: 题目就是flatten json to a list of map, 有一段json,比如说如下:
: {
: "uuid": "abc",
: "properties": {
: "sessionName": "Test session name",
: "waypoints": [
: {"uuid": "def", "properties": {"latitude": 3}}
: ]

avatar
j*a
4
最近有啥新闻?我想进点

【在 w**********d 的大作中提到】
: V看起来更易燃,LOL
avatar
z*o
5
递归
avatar
w*d
6
我是觉得图难看,两个都曾是牛股,最近breakdown。(声明啊,我是低手)
没有大新闻,yahoo有:
On Wednesday, the U.S. July consumer credit report showed that Americans
reduced their borrowing for the 23rd consecutive month as the slowing
economy and bleak employment picture kept them from using their plastic.
Borrowing on credit cards fell by 6.3% in July after dropping 7.5% in June.
Consumer credit outstanding fell at a seasonally adjusted annual rate of 1.8
%, down by $3.6 billion to $2.42 trillion, a Federal Reserve report said.
That

【在 j*a 的大作中提到】
: 最近有啥新闻?我想进点
avatar
n*s
7
现在的公司都肿么了,店面都能搞两个小时,如果是两个人,边边角角都问到了,分两
次电也还能理解,尼玛,就写个程序,这能电出个鸟来?
avatar
w*d
8
V 破 67.6

【在 w**********d 的大作中提到】
: V看起来更易燃,LOL
avatar
d*e
9
哪里用的了两个小时。10分钟的题。随手写一个
d = json.loads(json_string)
stack = [d]
while stack:
d = stack.pop()
if isinstance(d, dict):
for key in d:
stack.append(d[key]
if key == 'uuid':
yield key:d[key]
if isstance(d, list):
for entry in d:
stack.append(entry)

大意如此,需要用例子跑一下。上面程序在实际中还是用的不少的,比如说pretty
print什么的。
关键uber 是python 公司,你的用对工具。

【在 n*******s 的大作中提到】
: 现在的公司都肿么了,店面都能搞两个小时,如果是两个人,边边角角都问到了,分两
: 次电也还能理解,尼玛,就写个程序,这能电出个鸟来?

avatar
w*d
10
looking good downwards.
avatar
i*e
11
这种操作用js方便很多,并且lz确定输出是list of map而不是map么?有uuid作key前
者就没必要了吧
avatar
x*9
12
在list上被坑了一次。这题用Python可以秒。
import json
class Solution(object):
def __init__(self):
self.map_list = []
def flatten_json(self, json_str):
d = json.loads(json_str)
self.do_flatten_json(d)
return '\n'.join(map(str, self.map_list))
def do_flatten_json(self, d):
if isinstance(d, list):
map(self.do_flatten_json, d)
return

if not isinstance(d, dict):
return
if 'uuid' in d and 'properties' in d:
self.map_list.append({
'uuid': d['uuid'],
'properties': d['properties']})
for (key, value) in d.items():
self.do_flatten_json(value)
j = '''
{
"uuid": "abc",
"properties": {
"sessionName": "Test session name",
"waypoints": [
{"uuid": "def", "properties": {"latitude": 3}}
]
}
}
'''
S = Solution()
print S.flatten_json(j)
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。