loading...

Daily Challenge #171 - Ordering Beers in Poland

thepracticaldev profile image dev.to staff ・2 min read

Witamy!

You are in Poland and want to order a drink. You need to ask "One beer please.", so you say "Jedno piwo poprosze."

 Translator.orderingBeers(1) = "Jedno piwo poprosze."

But let's say you're really thirsty and you want several beers. You would need to learn how to count in Polish, and understand grammar and verb tenses.

Rules

In English, the plural of beer is simply beers, with an "s".
In Polish, however, the plural of piwo (nominative singular) can be piw (genitive plural) or piwa (nominative plural). It depends!

  • Usually the plural is genitive: piw
  • An exception occurs when using numbers 2, 3, or 4 (and any other numbers that end with those), the noun changes to the nominative piwa
  • The exception to the exception is for numbers 12, 13, and 14, which take the genitive piw.

Numbers in Polish

From 0 - 9:
"zero", "jeden"*, "dwa", "trzy", "cztery", "piec", "szesc" , "siedem", "osiem", "dziewiec"

From 10 - 19, nearly the same but with -ascie at the end:
"dziesiec", "jedenascie", "dwanascie", "trzynascie", "czternascie", "pietnascie", "szesnascie", "siedemnascie", "osiemnascie", "dziewietnascie"

Tens** from 10 to 90 are nearly the same, but with -ziesci or ziesiat at the end:
"dziesiec", "dwadziescia", "trzydziesci", "czterdziesci", "piecdziesiat", "szescdziesiat", "siedemdziesiat", "osiemdziesiat", "dziewiecdziesiat"

*"One" could be male ("Jeden"), female ("Jedna") or neuter ("Jedno"), which is the case for "beer" (piwo). But all other numbers are invariant, even if ending with "jeden".
**Compound numbers are constructed similarly to English: tens + units. For example, 22 is "dwadziescia dwa".

Tests

How would you order one beer?

Three beers?

Ten beers?

Good luck!


This challenge comes from GaspardP on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

pic
Editor guide
 
import math

pdigits = {
  0: ("zero", "piw"),
  1: ("jeden", "piw"),
  2: ("dwa", "piwa"),
  3: ("trzy", "piwa"),
  4: ("cztery", "piwa"),
  5: ("piec", "piw"),
  6: ("szesc", "piw"),
  7: ("siedem", "piw"),
  8: ("osiem", "piw"),
  9: ("dziewiec" "piw"),
  }

pteens = {
  10: ("dziesiec", "piw"),
  11: ("jedenascie", "piw"),
  12: ("dwanascie", "piw"),
  13: ("trzynascie", "piw"),
  14: ("czternascie", "piw"),
  15: ("pietnascie", "piw"),
  16: ("szesnascie", "piw"),
  17: ("siedemnascie", "piw"),
  18: ("osiemnascie", "piw"),
  19: ("dziewietnascie" "piw"),
  }

ptens = {
  2: "dwadziescia",
  3: "trzydziesci",
  4: "czterdziesci",
  5: "piecdziesiat",
  6: "szescdziesiat",
  7: "siedemdziesiat",
  8: "osiemdziesiat",
  9: "dziewiecdziesiat",
  }

def extract_number_and_beer_plural(num):
  if num == 1:
    return ("jedno", "piwo")

  if num < 10:
    return pdigits[num]

  if num < 20:
    return pteens[num]

  if num < 100:
    tens = math.floor(num/10)
    digit = num - (tens*10)

    if digit == 0:
      number = ptens[tens]
    else:
      number = "%s %s" % (ptens[tens], pdigits[digit][0])
    return (number, pdigits[digit][1])

def order_beer(num):
  pnum, pbeer = extract_number_and_beer_plural(num)
  return "%s %s poprosze." % (pnum.capitalize(), pbeer)

print("1")
print(order_beer(1))
print("Jedno piwo poprosze.")

print("\n2")
print(order_beer(2))
print("Dwa piwa poprosze.")

print("\n3")
print(order_beer(3))
print("Trzy piwa poprosze.")

print("\n7")
print(order_beer(7))
print("Siedem piw poprosze.")

print("\n10")
print(order_beer(10))
print("Dziesiec piw poprosze.")

print("\n11")
print(order_beer(11))
print("Jedenascie piw poprosze.")

print("\n12")
print(order_beer(12))
print("Dwanascie piw poprosze.")

print("\n20")
print(order_beer(20))
print("Dwadziescia piw poprosze.")

print("\n23")
print(order_beer(23))
print("Dwadziescia trzy piwa poprosze.")

print("\n34")
print(order_beer(34))
print("Trzydziesci cztery piwa poprosze.")

print("\n47")
print(order_beer(47))
print("Czterdziesci siedem piw poprosze.")

print("\n96")
print(order_beer(96))
print("Dziewiecdziesiat szesc piw poprosze.")

print("\n50")
print(order_beer(50))
print("Piecdziesiat piw poprosze.")
 

A: "Browary dla wszystkich" ;-)

 

A challenge every author of any i18n lib out there should take ;)