闲着做一道内推试题

同学发了一个题,看着挺有意思的,然后闲着做了一下。先上题目,不知道是哪个公司今年的内推试题。

看完这题先吐槽一下,特么为毛用字符串存储结果?动物不够多么--

分析一下,这个题看着好像就是指定id去取其下面动物的位移纪录,这个看着还算好,直接拿字典一存,貌似就完事了。

但是,看完题才知道,最恶心的事最后一个要求:需要校验动物的位移

好吧。上代码。基本测试了下,思路还是可行了,结果验证也是准确的。                                                                          

import collections
import re
import sys                                                                       
                                                                                 
string_test = '''e4e87cb2-8e9a-4749-abb6-26c59344dfee                            
2016/09/02 22:30:46                                                              
cat1 10 9                                                                        
351055db-33e6-4f9b-bfe1-16f1ac446ac1                                             
2016/09/02 22:30:52                                                              
cat1 10 9 2 -1                                                                   
cat2 2 3                                                                         
dcfa0c7a-5855-4ed2-bc8c-4accae8bd155                                             
2016/09/02 22:31:02                                                              
cat1 12 8 3 4'''                                                                 
                                                                                 
                                                                                 
# The main function                                                              
def getSnapShot(historyData, id):                                                
    _check_string(id)                                                            
    _check_string(historyData)                                                   
    data = _to_order_dict(string_test)                                           
    _check_shift_value(data)                                                     
                                                                                 
    data[id].sort()                                                              
    return data[id]                                                              
                                                                                 
# Make input string to dict                                                      
def _to_order_dict(historyData):                                                 
    animal_str = historyData.split('\n')                                         
    animal_key = collections.OrderedDict()                                       
    # regex compile                                                              
    regex_id = re.compile(r'(\w+)\-(\w+)\-(\w+)\-(\w+)')                         
    regex_time = re.compile(r'(\d+)\/(\d+)\/(\d+)')                              
                                                                                 
    for elemet in animal_str:                                                    
        if regex_id.match(elemet):                                               
            temp = elemet                                                        
            animal_key[temp] = []                                                
        elif regex_time.match(elemet):                                           
            pass                                                                 
        else:                                                                    
            animal_key[temp].append(elemet)                                      
                                                                                 
    return animal_key 
# Check animal shift.                                                            
def _check_shift_value(data):                                                    
    last_temp = {}                                                               
    for key, value in data.items():                                              
        if last_temp == {}:                                                      
            last_temp[key] = value                                               
            continue                                                             
        last_temp_value = _merge_list(last_temp.values())                        
        last_type = _make_shift_dict(last_temp_value)                            
        now_type = _make_shift_dict(value)                                       
        for last_key, last_value in last_type.items():                           
            for now_key, now_value in now_type.items():                          
                if now_key == last_key:                                          
                    if len(last_value) == 2:                                     
                        continue                                                 
                    else:                                                        
                        if _check_xy(last_value, now_value) is None:             
                            print "Conflict found at %s" % key                   
                            sys.exit()                                           
        last_temp[key] = value                                                   
                                                                                 
                                                                                 
# Merge lists                                                                    
def _merge_list(data):                                                           
    temp = []                                                                    
    for i in data:                                                               
        temp.extend(i)                                                           
    return temp                                                                  
                                                                                 
                                                                                 
# Check animal shift x,y value                                                   
def _check_xy(last_value, now_value):                                            
    if int(now_value[0]) == int(last_value[0]) + int(last_value[2]) and \        
            int(now_value[1]) == int(last_value[1]) + int(last_value[3]):        
        return True 
                                                                                 
                                                                                 
# Make animal shift value to dict                                                
def _make_shift_dict(data):                                                      
    shift_key = {}                                                               
    for elemet in data:                                                          
        temp = elemet.split(' ')                                                 
        key = temp[0]                                                            
        temp.pop(0)                                                              
        value = temp                                                             
        shift_key[key] = value                                                   
    return shift_key                                                             
                                                                                 
# Check input data type                                                          
def _check_string(data):                                                         
    if isinstance(data, str):                                                    
        pass                                                                     
    else:                                                                        
        print 'Invalid format.'                                                  
        sys.exit()                                                               
                                                                                 
                                                                                 
animal = getSnapShot(string_test, '351055db-33e6-4f9b-bfe1-16f1ac446ac1')        
print animal                                                                  

 解析一下思路,分析一下需求 

1,按照id,返回指定序列的动物位移-》

def getSnapShot(historyData, id)
1,传入数据和指定id
2,然后调用校验的两个函数对数据可靠性做判定
3,将传入数据转成有序字典,为什么用有序字典?因为校验动物位移也需要转字典,同时对位移的顺序有要求,所以将转字典的函数挪出来重用
4,传入id得出结果,将结果进行排序

 

2,验证输入格式

def _check_string(data)
1,就检查是否为字符串,文档也没明确检查什么--

3,验证数据问题(动物位移是否合理)

def _check_shift_value(data)
1,分析可以得出,动物当前位移校验,需要上一次位移数据提供基准,所以对数据便利,保存上一份,拿上一份数据对当前数据进行校验
2,对日期没要求,所以日期的数据没有拿出来用 

代码只验证了功能要求,没进行优化修改,mark放一下--

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值