| 
									
										
										
										
											1996-11-27 19:52:01 +00:00
										 |  |  | #! /usr/bin/env python | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # 2)  Sorting Test | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | #     Sort an input file that consists of lines like this | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | #         var1=23 other=14 ditto=23 fred=2 | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | #     such that each output line is sorted WRT to the number.  Order | 
					
						
							|  |  |  | #     of output lines does not change.  Resolve collisions using the | 
					
						
							|  |  |  | #     variable name.   e.g. | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  | # | 
					
						
							|  |  |  | #         fred=2 other=14 ditto=23 var1=23 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | #     Lines may be up to several kilobytes in length and contain | 
					
						
							|  |  |  | #     zillions of variables. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # This implementation: | 
					
						
							|  |  |  | # - Reads stdin, writes stdout | 
					
						
							|  |  |  | # - Uses any amount of whitespace to separate fields | 
					
						
							|  |  |  | # - Allows signed numbers | 
					
						
							|  |  |  | # - Treats illegally formatted fields as field=0 | 
					
						
							|  |  |  | # - Outputs the sorted fields with exactly one space between them | 
					
						
							|  |  |  | # - Handles blank input lines correctly | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-24 17:17:56 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | import string | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2003-04-24 17:17:56 +00:00
										 |  |  |     prog = re.compile('^(.*)=([-+]?[0-9]+)') | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |     def makekey(item, prog=prog): | 
					
						
							| 
									
										
										
										
											2003-04-24 17:17:56 +00:00
										 |  |  |         match = prog.match(item) | 
					
						
							|  |  |  |         if match: | 
					
						
							|  |  |  |             var, num = match.group(1, 2) | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |             return string.atoi(num), var | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # Bad input -- pretend it's a var with value 0 | 
					
						
							|  |  |  |             return 0, item | 
					
						
							|  |  |  |     while 1: | 
					
						
							|  |  |  |         line = sys.stdin.readline() | 
					
						
							|  |  |  |         if not line: | 
					
						
							|  |  |  |             break | 
					
						
							| 
									
										
										
										
											2003-04-24 17:17:56 +00:00
										 |  |  |         items = line.split() | 
					
						
							| 
									
										
										
										
											2007-07-17 20:59:35 +00:00
										 |  |  |         items = list(map(makekey, items)) | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |         items.sort() | 
					
						
							|  |  |  |         for num, var in items: | 
					
						
							| 
									
										
										
										
											2007-07-17 20:59:35 +00:00
										 |  |  |             print("%s=%s" % (var, num), end=' ') | 
					
						
							|  |  |  |         print() | 
					
						
							| 
									
										
										
										
											1995-04-10 11:40:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | main() |