Advertisement
Guest User

Untitled

a guest
Aug 14th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import sys;
  2.  
  3. abs_path = sys.stdin.readline().rstrip('\n')
  4. params = sys.stdin.readline().split(',')
  5.  
  6. if len(params) != 3:
  7.     print "Invalid Input"
  8.     sys.exit();
  9.  
  10. dept = params[0]
  11. page_size = int(params[1])
  12. page_num = int(params[2]) - 1
  13.  
  14. def FileParser(path):
  15.     f = open(path)
  16.     row = f.readline()
  17.     data_dict = {}
  18.     while row != '':
  19.         row = f.readline()
  20.         if row != '':
  21.             row = row.rstrip('\n')
  22.             columns = row.split(',')
  23.             temp_dict = {}
  24.             dept_id = columns[0]
  25.             temp_dict['dept'] = dept_id
  26.             temp_dict['id'] = columns[1]
  27.             name_parts = columns[2].split(" ")
  28.            
  29.             if len(name_parts) > 1:
  30.                 temp_dict['fname'] = name_parts[1]
  31.             else:
  32.                 temp_dict['fname'] = columns[2]
  33.  
  34.             temp_dict['full_name'] = columns[2]
  35.            
  36.             if not dept_id in data_dict:
  37.                 data_dict[dept_id] = []
  38.             data_dict[dept_id].append(temp_dict)
  39.     f.close()
  40.     for department_id in data_dict:
  41.         data_dict[department_id].sort(key=lambda k: len(k['full_name']))
  42.     return data_dict;
  43.  
  44. emp_dict = FileParser(abs_path)
  45.  
  46. if dept in emp_dict:
  47.     offset = page_num * page_size
  48.     if offset > 0:
  49.         offset -= 1
  50.     dept_size = len(emp_dict[dept])
  51.     if offset < dept_size:
  52.         arr = []
  53.         for x in range(offset,min(dept_size,offset+page_size)):
  54.             arr.append(emp_dict[dept][x])
  55.         arr.sort()
  56.         for emp in arr:
  57.             print ','.join([str(dept),emp['id'],emp['full_name']])
  58.         sys.exit()
  59.  
  60. print "Data Unavailable"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement