Advertisement
informaticage

OIS Numpad Python

Jun 17th, 2020
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. def absoluteValue ( x ):
  2.     if ( x >= 0 ):
  3.         return x
  4.     else:
  5.         return -x
  6.  
  7. def findCoordinates ( num ):
  8.     if ( num == 1 ):
  9.         return [ 0, 0 ]
  10.  
  11.     if ( num == 2 ):
  12.         return [ 0, 1 ]
  13.  
  14.     if ( num == 3 ):
  15.         return [ 0, 2 ]
  16.    
  17.     if ( num == 4 ):
  18.         return [ 1, 0 ]
  19.  
  20.     if ( num == 5 ):
  21.         return [ 1, 1 ]
  22.  
  23.     if ( num == 6 ):
  24.         return [ 1, 2 ]
  25.  
  26.     if ( num == 7 ):
  27.         return [ 2, 0 ]
  28.  
  29.     if ( num == 8 ):
  30.         return [ 2, 1 ]
  31.  
  32.     if ( num == 9 ):
  33.         return [ 2, 2 ]
  34.  
  35.     if ( num == 0 ):
  36.         return [ 3, 1 ]
  37.  
  38.     return [ -1, -1 ]
  39.  
  40. def distance ( first, second ):
  41.     positionFirst = findCoordinates ( first )
  42.     positionSecond = findCoordinates ( second )
  43.    
  44.     xDifference = absoluteValue ( positionFirst [ 0 ] - positionSecond [ 0 ] )
  45.     yDifference = absoluteValue ( positionFirst [ 1 ] - positionSecond [ 1 ] )
  46.  
  47.     return xDifference + yDifference
  48.  
  49. phoneNumber = input ( "> Insert phone number: " )
  50.  
  51. phoneNumberList = []
  52.  
  53. for x in range ( 0, len ( phoneNumber ) ):
  54.     phoneNumberList.append( int ( phoneNumber [ x ] ) )
  55.  
  56. print ( phoneNumberList )
  57.  
  58. totalDistance = distance ( 0, phoneNumberList [ 0 ] )
  59. for x in range ( 0, len ( phoneNumber ) - 1 ):
  60.     totalDistance = totalDistance + distance ( phoneNumberList [ x ], phoneNumberList [ x + 1 ] )
  61.  
  62. print ( totalDistance + len ( phoneNumber ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement