dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
95 lines
4.1 KiB
Markdown
95 lines
4.1 KiB
Markdown
---
|
|
id: 6a0dcd03ee4e68698080ef67
|
|
title: "Challenge 303: Roommates"
|
|
challengeType: 29
|
|
dashedName: challenge-303
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an array of people and their roommate group, return the room assignments for a hotel stay using the following rules:
|
|
|
|
- Each person has a `name` and a `group` property:
|
|
|
|
```json
|
|
[
|
|
{ "name": "Alice", "group": "A" },
|
|
{ "name": "Bob", "group": "B" },
|
|
{ "name": "Carol", "group": "A" }
|
|
]
|
|
```
|
|
|
|
- People can only share a room with someone from the same group and are paired in the order they are given.
|
|
- Return an array of strings with names separated by `" and "` for a shared room, and just the name for a solo room. Names must appear in the order they were paired. For the example above, return `["Alice and Carol", "Bob"]`.
|
|
|
|
# --hints--
|
|
|
|
`get_roommates([{ "name": "Alice", "group": "A" }, { "name": "Bob", "group": "B" }, { "name": "Carol", "group": "A" }])` should return `["Alice and Carol", "Bob"]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(sorted(get_roommates([{"name": "Alice", "group": "A"}, {"name": "Bob", "group": "B"}, {"name": "Carol", "group": "A"}])), sorted(["Alice and Carol", "Bob"]))`)
|
|
}})
|
|
```
|
|
|
|
`get_roommates([{ "name": "John", "group": "C" }, { "name": "Julia", "group": "C" }, { "name": "Jim", "group": "C" }])` should return `["John and Julia", "Jim"]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(sorted(get_roommates([{"name": "John", "group": "C"}, {"name": "Julia", "group": "C"}, {"name": "Jim", "group": "C"}])), sorted(["John and Julia", "Jim"]))`)
|
|
}})
|
|
```
|
|
|
|
`get_roommates([{ "name": "Adam", "group": "D" }, { "name": "Abraham", "group": "E" }, { "name": "Austin", "group": "E" }, { "name": "Augustus", "group": "D" }, { "name": "Angelica", "group": "D" }, { "name": "Aaron", "group": "E" }])` should return `["Adam and Augustus", "Angelica", "Abraham and Austin", "Aaron"]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(sorted(get_roommates([{"name": "Adam", "group": "D"}, {"name": "Abraham", "group": "E"}, {"name": "Austin", "group": "E"}, {"name": "Augustus", "group": "D"}, {"name": "Angelica", "group": "D"}, {"name": "Aaron", "group": "E"}])), sorted(["Adam and Augustus", "Angelica", "Abraham and Austin", "Aaron"]))`)
|
|
}})
|
|
```
|
|
|
|
`get_roommates([{ "name": "Frank", "group": "A" }, { "name": "Emitt", "group": "B" }, { "name": "Daria", "group": "F" }, { "name": "Charles", "group": "D" }, { "name": "Bailey", "group": "A" }, { "name": "Albert", "group": "F" }])` should return `["Frank and Bailey", "Emitt", "Daria and Albert", "Charles"]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(sorted(get_roommates([{"name": "Frank", "group": "A"}, {"name": "Emitt", "group": "B"}, {"name": "Daria", "group": "F"}, {"name": "Charles", "group": "D"}, {"name": "Bailey", "group": "A"}, {"name": "Albert", "group": "F"}])), sorted(["Frank and Bailey", "Emitt", "Daria and Albert", "Charles"]))`)
|
|
}})
|
|
```
|
|
|
|
`get_roommates([{ "name": "Kevin", "group": "A" }, { "name": "Yuri", "group": "A" }, { "name": "Hugo", "group": "B" }, { "name": "Violet", "group": "A" }, { "name": "Brett", "group": "A" }, { "name": "Wayne", "group": "B" }])` should return `["Kevin and Yuri", "Violet and Brett", "Hugo and Wayne"]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(sorted(get_roommates([{"name": "Kevin", "group": "A"}, {"name": "Yuri", "group": "A"}, {"name": "Hugo", "group": "B"}, {"name": "Violet", "group": "A"}, {"name": "Brett", "group": "A"}, {"name": "Wayne", "group": "B"}])), sorted(["Kevin and Yuri", "Violet and Brett", "Hugo and Wayne"]))`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def get_roommates(people):
|
|
|
|
return people
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def get_roommates(people):
|
|
groups = {}
|
|
for person in people:
|
|
groups.setdefault(person["group"], []).append(person["name"])
|
|
result = []
|
|
for members in groups.values():
|
|
for i in range(0, len(members), 2):
|
|
result.append(" and ".join(members[i:i + 2]))
|
|
return result
|
|
```
|