Advertisement
Woobinda

Declination of word (amount)

Jul 26th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Числительное склонение слова, в зависимости от количества
  4. """
  5.  
  6. def f(count, name):
  7.     if count in range(11,15):
  8.         return str(count) + ' ' + name + 'ов'
  9.     else:
  10.         D = [{'':[1]},{'а':[2,3,4]},{'ов':[0,5,6,7,8,9]}]
  11.         def func(count):
  12.             count = str(count)
  13.             for _dict in D:
  14.                 for value in _dict.items():
  15.                     if int(count[-1]) in value[1]:
  16.                         return value[0]
  17.         _end = func(count)
  18.         return str(count) + ' ' + name + _end
  19.  
  20.  
  21. if __name__ == '__main__':
  22.  
  23.     assert f(0, 'товар') == '0 товаров'
  24.     assert f(1, 'товар') == '1 товар'
  25.     assert f(6, 'товар') == '6 товаров'
  26.     assert f(11, 'товар') == '11 товаров'
  27.     assert f(13, 'товар') == '13 товаров'
  28.     assert f(41, 'товар') == '41 товар'
  29.     assert f(55, 'товар') == '55 товаров'
  30.     assert f(4, 'товар') == '4 товара'
  31.     assert f(44, 'товар') == '44 товара'
  32.  
  33.     print('All Tests ok')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement