Skip to content

Instantly share code, notes, and snippets.

@aluminiumgeek
Created September 8, 2014 15:37
Show Gist options
  • Select an option

  • Save aluminiumgeek/0385d9f04090c446f56a to your computer and use it in GitHub Desktop.

Select an option

Save aluminiumgeek/0385d9f04090c446f56a to your computer and use it in GitHub Desktop.
1, 2, 5 formatter
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Форматирование словоформы "1, 2, 5" (посетитель/посетителя/посетителей)
import random
def choose_word_form(number, form1, form2, default):
mod10 = abs(number) % 10
mod100 = abs(number) % 100
if mod10 == 1 and mod100 != 11:
return form1
elif mod10 == 2 and mod100 != 12 or mod10 == 3 and \
mod100 != 13 or mod10 == 4 and mod100 != 14:
return form2
return default
# Демонстрация
for _ in range(15):
num = random.randint(1, 10000)
print u'{0} {1}'.format(num, choose_word_form(num, u"штука", u"штуки", u"штук"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment