Wortbildung (Anzahl möglicher Ballons) (Schleifen)
In einem String (s) kommen unsortiert Zeichen (Kleinbuchstaben) vor, mit denen man das Wort „balloon“ bilden soll. Erstelle eine Methode/Funktion, die als Ergebnis die Anzahl der möglichen Wortbildungen ausgibt, wobei jedes Zeichen (Buchstabe) nur einmal verwendet werden darf.
Beispiel 1:
s = "nlaebolko"
Lösung: 1
Beispiel 2:
s = "loonbalxballpoon"
Lösung: 2
Beispiel 3:
s = „balbalonn“
Lösung: 0
0 Kommentare
7 Lösung(en)
def count_possible_constructions(inp, word):
from collections import Counter
required_one, got_chars = Counter(word), Counter(inp)
return min((got_chars[key]//required_one[key] for key in required_one))
for test in ["nlaebolko", "loonbalxballpoon", "balbalonn"]:
print(f"{test}: {count_possible_constructions(test, word='balloon')}")
Lösung von: Name nicht veröffentlicht
// NET 6.x | C# 10.x | VS-2022
var l = new List<string> { "nlaebolko", "Loonbalxballpoon", "balbalonn" };
l.ForEach(x => Console.WriteLine($"{x}:= {MaxNum(x)}"));
static int MaxNum(string s) {
var a = new int[26];
foreach (var c in s.ToLower()) a[c - 'a'] += 1;
return new int[] { a[0], a[1], a[11] / 2, a[13], a[14] / 2 }.Min();
}
Lösung von: Jens Kelm (@JKooP)
// C++ 17
#include <iostream>
#include <vector>
#include <algorithm>
auto max_num(std::string s) {
int a[26]{ 0 };
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
for (const auto& c : s) a[c - 'a'] += 1;
const std::vector<int> v{ a[0], a[1], a[11] / 2, a[13], a[14] / 2 };
return *min_element(v.begin(), v.end());
}
int main() {
std::vector<std::string> v{ "nlaebolko", "Loonbalxballpoon", "balbalonn" };
for (const auto& e : v) std::cout << e << ":= " << max_num(e) << "\n";
}
Lösung von: Jens Kelm (@JKooP)
' VBA-Funktion
Function MaxNum(ByVal str As String) As Integer
Dim low As String
Dim bal As Variant
Dim chr(25) As Integer, pos As Integer, i As Integer
low = LCase(str)
For i = 1 To Len(low)
pos = Asc(Mid(low, i, 1)) - 97
chr(pos) = chr(pos) + 1
Next
bal = Array(chr(0), chr(1), chr(11) / 2, chr(13), chr(14) / 2)
MaxNum = WorksheetFunction.Min(bal)
End Function
Sub Main()
Dim c As Variant, chrs As Variant
chrs = Array("znlaebolko", "Loonbalxballpoon", "balbalonn")
For Each c In chrs
Debug.Print c, MaxNum(c)
Next
End Sub
Lösung von: Jens Kelm (@JKooP)
function possibleWordBuilds(str, txt) {
txt = txt.split('');
let found = true,
count = 0;
while (found) {
for (let i = 0; i < str.length; i++) {
let j = txt.indexOf(str[i]);
(j == -1) ? found = false : txt.splice(j, 1);
}
if (found) count++;
}
return count;
}
for (let x of ['nlaebolko', 'loonbalxballpoon', 'balbalonn'])
console.log( possibleWordBuilds('balloon', x) );
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define UINT unsigned int
UINT max_words(const char* string, UINT size) {
int arr[26] = {};
for (UINT i = 0; i < size; ++i)
arr[(tolower(string[i]) - 'a')]++;
UINT out[5] = { arr[0], arr[1], arr[11] / 2, arr[13], arr[14] / 2 };
UINT min = arr[0];
for (UINT i = 1; i < 5; ++i)
min = MIN(min, out[i]);
return min;
}
int main() {
const char* words[] = { "nlaebolko", "Loonbalxballpoon", "balbalonn" };
UINT size = sizeof(words) / sizeof(words[0]);
for (UINT i = 0; i < size; ++i) {
UINT max = max_words(words[i], strlen(words[i]));
printf("%s", words[i]);
printf(" - %i\n", max);
}
}
Lösung von: Jens Kelm (@JKooP)
// C++23 - msvc only!
#include <iostream>
#include <ranges>
#include <array>
#include <print>
static constexpr auto max_num(std::string_view s) {
std::array<size_t, 26> a{ 0 };
for (const size_t& c : s | std::views::transform(::tolower))
a[c - 'a']++;
const std::array<size_t, 5> v{ a[0], a[1], a[11] / 2, a[13], a[14] / 2 };
return *std::ranges::min_element(v);
}
int main() {
for (const auto& e : { "nlaebolko", "Loonbalxballpoon", "balbalonn" })
std::print("{}: {}\n", e, max_num(e));
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | |
Schwierigkeit: | Mittel |
Webcode: | crk2-zoqi |
Autor: | () |