@absolute100/

airbnb: display page

Python 2.7

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def pagedisplay(input_csv_array, k):
    ids = [line.split(',')[0] for line in input_csv_array]
    hmap = {}
    pages = []
    start = 0
 
    for i, id in enumerate(ids):
        if id not in hmap or hmap[id]<start:
            hmap[id]=start
        if hmap[id]==len(pages):
            pages.append([])
        pages[hmap[id]].append(input_csv_array[i])
        hmap[id]+=1
        if len(pages[start])==k:
            start+=1
 
    # if you need to print exact k lines in a page (i.e., tolerate some dup)
    # then have a third loop to print the page
    for page in pages:
        print '---- page ----'
        for line in page:
            print line

input_csv_array = [
  "1,28,300.1,SanFrancisco",
  "4,5,209.1,SanFrancisco",
  "20,7,208.1,SanFrancisco",
  "23,8,207.1,SanFrancisco",
  "16,10,206.1,Oakland",
  "1,16,205.1,SanFrancisco",
  "6,29,204.1,SanFrancisco",
  "7,20,203.1,SanFrancisco",
  "8,21,202.1,SanFrancisco",
  "2,18,201.1,SanFrancisco",
  "2,30,200.1,SanFrancisco",
  "15,27,109.1,Oakland",
  "10,13,108.1,Oakland",
  "11,26,107.1,Oakland",
  "12,9,106.1,Oakland",
  "13,1,105.1,Oakland",
  "22,17,104.1,Oakland",
  "1,2,103.1,Oakland",
  "28,24,102.1,Oakland",
  "18,14,11.1,SanJose",
  "6,25,10.1,Oakland",
  "19,15,9.1,SanJose",
  "3,19,8.1,SanJose",
  "3,11,7.1,Oakland",
  "27,12,6.1,Oakland",
  "1,3,5.1,Oakland",
  "25,4,4.1,SanJose",
  "5,6,3.1,SanJose",
  "29,22,2.1,SanJose",
  "30,23,1.1,SanJose"
]
 
input_csv_array2 = [
  "1,28,300.1,SanFrancisco",
  "29,22,2.1,SanJose",
  "28,22,2.1,SanJose",
  "29,22,2.1,SanJose",
  "1,5,209.1,SanFrancisco",
  "1,7,208.1,SanFrancisco"
]

input_csv_array3 = [
  "1,28,300.1,SanFrancisco",
  "1,5,209.1,SanFrancisco",
  "1,7,208.1,SanFrancisco",
  "28,22,2.1,SanJose",
  "29,22,2.1,SanJose",
]

print "\ninput_csv_array"
pagedisplay(input_csv_array, 12)
print "\ninput_csv_array2"
pagedisplay(input_csv_array2, 2) # same id skip a page
print "\ninput_csv_array3"
pagedisplay(input_csv_array3, 2) # page grows faster than ids