SOP.py

FrontPage|FindPage|TitleIndex|RecentChanges| UserPreferences P RSS
""" Sum of Pairs Read each line from standard input and print the SOP until the end of file or a blank line e.g. when groupdata.txt is: 1 2 3 4 5 10 20 30 c:\somewherewiththefiles>python SOP.py < groupdata.txt > SOPresult.txt will write the result in SOPresult.txt as: 20 40""" def getPairs(n): """get a list and return all pair combination of the list as a list""" result=[] for i1 in range(len(n)): for i2 in range(i1+1,len(n)): result.append((n[i1],n[i2])) return result def getAbsoluteSum(p): """get a list of pairs and return the abs sum of them""" sum=0 for first,second in p: sum+=abs(first-second) return sum if __name__=="__main__": import sys input=sys.stdin while 1: eachLine=input.readline() if not eachLine or eachLine=='\n': break listLine= map(int,eachLine.split()) print getAbsoluteSum(getPairs(listLine)) #---- #[ì»´í~S¨í~D°ë¶~Dë¥~X]

"; if (isset($options[timer])) print $menu.$banner."
".$options[timer]->Write()."
"; else print $menu.$banner."
".$timer; ?> # # ?>