Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

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

Bitte melde dich an um einen Kommentar abzugeben

5 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)

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit:
Schwierigkeit: Mittel
Webcode: crk2-zoqi
Autor: ()

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen