/
Sock-pairs.py
38 lines (32 loc) · 881 Bytes
/
Sock-pairs.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
''' ** PYTHON CODE CHALLENGE - SORTING SOCKS **
# after doing your laundry, you now have a bag full of socks
# to pair up and sort. right and left socks are mirror images
# of each other so 'ad' is paired with 'da', 'pl' with '1p
etc.
# write code to pair and sort your socks, putting
# leftover singles in a separate 'bag' for next time.
'''
socks = [ 'lv','eb','ho', 'ug',
'da', 'be', 'se', 'kc',
'p1', 'ck', 'gu', 'vl', 'nb', 'ad' ]
''' # your output should be
pairs
= [['ad', 'da'], ['be', 'eb'], ['ck', 'kc'],
['gu', 'ug'], ['lv', 'vl']]
singles = ['nb', 'pl', 'ho', 'se'] '''
socks.sort()
pairs = []
singles = []
while socks:
left = socks.pop()
found = False
for i, right in enumerate(socks):
if left[::-1] == right:
pairs.append([left, right])
socks.pop(i)
found = True
break
if not found:
singles.append(left)
print(pairs)
print(singles)