''' 題目描述 開發一個座標計算工具, A表示向左移動,D表示向右移動,W表示向上移動,S表示向下移動。從(0,0)點開始移動, 從輸入字符串裏面讀取一些座標,並將最終輸入結果輸出到輸出文件裏面。 輸入: 合法座標爲A(或者D或者W或者S) + 數字(兩位以內) 座標之間以;分隔。 非法座標點需要進行丟棄。如AA10;A1A;$%$;AD; 等。 ''' import sys for line in sys.stdin: a = line.split()[0] direction=a.split(';') output=[0,0] direct={'A':[-1,0],'D':[1,0],'W':[0,1],'S':[0,-1]} number=['0','1','2','3','4','5','6','7','8','9'] #print(direction) for ch in direction: if ch=='': continue if ch[0] in direct: if len(ch)==3 and ch[1] in number and ch[2] in number: dir=direct[ch[0]]#表示朝着某個方向移動了單位長度 move_dir=list(map(lambda x,y:x*y,dir,[int(ch[1:])]*2)) output=list(map(lambda x,y:x+y,output,move_dir)) elif len(ch)==2 and ch[0] in direct and ch[1] in number: dir = direct[ch[0]] # 表示朝着某個方向移動了單位長度 move_dir = list(map(lambda x, y: x * y, dir, [int(ch[1:])] * 2)) output = list(map(lambda x, y: x + y, output, move_dir)) print(str(output[0])+','+str(output[1]))
python 座標移動
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.