DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #223 - Responsible Drinking

Welcome to the bar!

We recommend you drink 1 glass of water per drink so you're not hungover tomorrow morning. Your fellow coders have bought you several drinks tonight in the form of strings. Return a string suggesting how many glasses of water you should drink to not be hungover.

If you would like to keep things simple, you can consider any "numbered thing" in the string to be a drink. Even "1 bear" => "1 glass of water" or "1 chainsaw and 2 pools" => "3 glasses of water".

Example Pairs
Input 0:
"1 beer"
Output 0:
"1 glass of water"

Input 1:
"1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer"
Output 1:
"10 glasses of water"

Tests
hydrate("1 beer")
hydrate("2 glasses of wine and 1 shot")
hydrate("1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer")

Good luck!


This challenge comes from taylormb 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!

Top comments (18)

Collapse
 
vidit1999 profile image
Vidit Sarkar

Python solution

import re

def hydrate(drinks: str) -> str:
    drinkCount = sum(map(int, re.findall("\d+", drinks))) # total number of drinks
    # this will also work, drinkCount = sum([int(num) for num in drinks.split(" ") if num.isdigit()])

    return str(drinkCount) + " glass" + ("es" if drinkCount > 1 else "") + " of water"


print(hydrate("1 beer")) # output -> 1 glass of water
print(hydrate("2 glasses of wine and 1 shot")) # output -> 3 glasses of water
print(hydrate("1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer")) # output -> 10 glasses of water
Collapse
 
vidit1999 profile image
Vidit Sarkar

I want to suggest a challenge. What should be the subject of my email? Can I send a markdown file?

Collapse
 
devencourt profile image
Brian Bethencourt

Hey Vidit, I'm happy to hear you wanna suggest a challenge. You can send any suggestions to yo+challenge@dev.to. Feel free to drop the content of your challenge in the email, markdown file works too. Subject doesn't matter too much, we'll know what it is.

Take care!

Thread Thread
 
vidit1999 profile image
Vidit Sarkar

Thanks!

Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

Using regular expressions in JavaScript:

const hydrate = s => {
  // get an array with all the numbers in the string, and add the values using reduce
  const number = s.match(/\d/g)
                  .reduce((acc, val) => acc + parseInt(val), 0);
  return `${number} glass${number != 1 ? 'es' : ''} of water`;
}

We get the following results:

hydrate("1 beer")
// 1 glass of water 
hydrate("2 glasses of wine and 1 shot")
// 3 glasses of water 
hydrate("1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer")
// 10 glasses of water
Collapse
 
fluffynuts profile image
Davyd McColl

I didn't see yours, but mine is practically identical 😮

Collapse
 
alvaromontoro profile image
Alvaro Montoro

Yours has some checks that make it more robust... And that I probably should add to mine 😳

Collapse
 
arnavmotwani profile image
Arnav Motwani

My solution in python

import re
order = input("list the drinks")


def Hydrate(order):
    glasses = 0
    drinks = re.split(", |and ",order)
    for number in drinks:
        tab = number.split(" ")
        glasses += int(tab[0])
    print(glasses)

Hydrate(order)
Collapse
 
jack_garrus profile image
Nadia Guarracino • Edited

Js vanilla solution (fix below)
Js vanilla solution

Collapse
 
fluffynuts profile image
Davyd McColl

What if I drink "11 beers"?

(I have a feeling I'll be severely under-hydrated)

Collapse
 
jack_garrus profile image
Nadia Guarracino • Edited

Oh, that's interesting. I'll upload a fix asap. Thanks for reporting!
EDIT: fixed! The problem was the split -not necessary. Also have forgot the existence of reduce! I need a vacation

fix

Collapse
 
nnari profile image
Tatu Pesonen

Your theme looks amazing, what is it?

Collapse
 
jack_garrus profile image
Nadia Guarracino

Thank you! It's a customization I've made from Material Ocean, nothing special :)

Thread Thread
 
nnari profile image
Tatu Pesonen

Any chance you would be willing to upload it for me somewhere? :)

Thread Thread
 
jack_garrus profile image
Nadia Guarracino

Well, I could send it to you by email but note that I had customize only the Js, Sass, Css. HTML and Ts parts. Let me know!

Collapse
 
rafaacioly profile image
Rafael Acioly

Python solution 🐍

import re

def drink(order: str) -> str:
    numbers = re.findall('\d+', order)
    amount = sum(int(amount) for amount in numbers)
    return f"{amount} glasses of water"
Collapse
 
kesprit profile image
kesprit • Edited

My swift solution :

func hydrate(_ input: String) -> String {
    let number = input.split(separator: .init(" "))
        .map { $0.filter { c in c.isNumber } }
        .compactMap { Int($0) }
        .reduce(into: 0) { $0 += $1 }
    return "\(number) glass\(number > 1 ? "es": "") of water"
}

hydrate("1 beer") // 1 glass of water
hydrate("2 glasses of wine and 1 shot") // 3 glasses of water
hydrate("1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer") // 10 glasses of water
Collapse
 
fluffynuts profile image
Davyd McColl
function hydrate(sentence) {
  const
    howMany = (sentence.match(/(\d+)/g) || [])
      .reduce((acc, cur) => acc + parseInt(cur), 0),
    s = howMany === 1 ? "" : "es";
    return `${howMany} glass${s} of water`;
}