SHARE
TWEET

count dashes

DeaD_EyE Jun 27th, 2019 (edited) 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from typing import Union
  2.  
  3.  
  4. def naive_dash_sum_dict(digits):
  5.     """
  6.    Calculates the sum of dashes in numbers.
  7.    """
  8.     dashes = {
  9.         '0': 6, '1': 2, '2': 5, '3': 5,
  10.         '4': 4, '5': 5, '6': 6, '7': 3,
  11.         '8': 7, '9': 6,
  12.     }
  13.     result = 0
  14.     for digit in digits:
  15.         result += dashes[digit]
  16.     return result
  17.  
  18.  
  19. def dash_sum1(digits):
  20.     """
  21.    Calculates the sum of dashes in numbers.
  22.    The digits must be a string.
  23.    """
  24.     dashes = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  25.     result = 0
  26.     for digit in digits:
  27.         result += dashes[int(digit)]
  28.     return result
  29.  
  30.  
  31. def dash_sum2(digits):
  32.     """
  33.    Calculates the sum of dashes in numbers.
  34.    The digits can be an integer or a string.
  35.    """
  36.     dashes = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  37.     result = 0
  38.     digits = str(digits)
  39.     for digit in digits:
  40.         result += dashes[int(digit)]
  41.     return result
  42.  
  43.    
  44. def dash_sum3(digits: Union[str, int]):
  45.     """
  46.    Calculates the sum of dashes in numbers.
  47.    The digits can be an integer or a string.
  48.    """
  49.     dashes = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  50.     result = 0
  51.     digits = str(digits)
  52.     for digit in map(int, digits):
  53.         result += dashes[digit]
  54.     return result
  55.  
  56.  
  57. def dash_sum4(digits: Union[str, int]):
  58.     """
  59.    Calculates the sum of dashes in numbers.
  60.    The digits can be an integer or a string.
  61.    """
  62.     dashes = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  63.     result = 0
  64.     for digit in map(int, str(digits)):
  65.         result += dashes[digit]
  66.     return result
  67.  
  68.  
  69. def dash_sum5(digits: Union[str, int]):
  70.     """
  71.    Calculates the sum of dashes in numbers.
  72.    The digits can be an integer or a string.
  73.    """
  74.     dashes = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  75.     return sum(
  76.         dashes[digit]
  77.         for digit in
  78.         map(int, str(digits))
  79.     )
  80.  
  81. functions = [globals()[func] for func in sorted(dir()) if 'dash' in func]
  82.  
  83. test_str = '1234567890'
  84. print('Test string:', test_str)
  85. for func in functions:
  86.     print(f'{func.__name__}: {func(test_str)}')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top