Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Liste von Strings vertikalisieren. (Zeichenketten)

Schreibe ein Programm, das eine gegebene Liste von Strings vertikal auf der Konsole ausgibt.

Zwischen den Buchstaben sollte ein Leerzeichen sein.

 

Beispiel:

liste=['programmier', 'aufgaben', '.ch']

Ausgabe:

p a .
r u c
o f h
g g  
r a  
a b  
m e  
m n  
i    
e    
r

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

7 Lösung(en)

l=['programmier', 'aufgaben', '.ch']
print("\n".join([" ".join([b[c] for b in [a+" "*(len(max(l,key=len))-len(a)) for a in l]]) for c in range(len(max(l,key=len)))]))
                

Lösung von: rob ert (tub)

function verticalize(arr) {
  let max = -1,
      i, j;
  for (i of arr) max = Math.max(max, i.length);
  for (i = 0; i < max; i++) {
    let out = '';
    for (j of arr) if (j[i]) out += j[i] + ' ';
    console.log(out.trim());
  }
}

verticalize( ['programmier', 'aufgaben', '.ch'] );
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// C++20
// Refactoring aufgrund eines Kompatibilitätsproblems mit MSVC

#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>

static const auto zip(const std::vector<std::string>& words_, bool w_space_ = true) {
    std::vector<std::string> out{};

    const auto size{ std::ranges::max(words_
        | std::views::transform([](const auto& s) { return s.length(); })) };

    for (const auto& i : std::views::iota(0u, size)) {
        std::string tmp{};
        for (const auto& word : words_ 
            | std::views::filter([&i](const auto& w){ return i < w.length(); })) {
            tmp.push_back(word[i]);
            if (w_space_) tmp.push_back(' ');
        }
        out.push_back(tmp);
    }

    return out;
}

int main() {
    std::vector<std::string>words{ "programmier", "aufgaben", ".ch" };

    for (const auto& elem : zip(words))
        std::cout << elem << "\n";
}
                

Lösung von: Jens Kelm (@JKooP)

// NET 8.x | C# 12.x | VS-2022

List<string> words = ["programmier", "aufgaben", ".ch"];

foreach (var i in Enumerable.Range(0, words.Max(x => x.Length))) {
    foreach (var word in words)
        if (i < word.Length)
            Console.Write(word[i] + " ");
    Console.WriteLine();
}
                

Lösung von: Jens Kelm (@JKooP)

' VBA

Function Size(words As Variant) As Integer
    Dim max As Integer
    Dim word As Variant
    For Each word In words
        max = WorksheetFunction.max(max, Len(word))
    Next
    Size = max
End Function

Function Zip(words As Variant, Optional wSpace As Boolean = True) As Variant
    Dim out() As Variant, word As Variant
    Dim k As Integer, i As Integer
    Dim tmp As String
    For i = 1 To Size(words)
        For Each word In words
            If i < Len(word) Then
                tmp = tmp & Mid(word, i, 1) & IIf(wSpace, " ", "")
            End If
        Next word
        ReDim Preserve out(k)
        out(k) = tmp
        tmp = ""
        k = k + 1
    Next i
    Zip = out
End Function

Sub Main()
    words = Array("programmier", "aufgaben", ".ch")
    For Each elem In Zip(words)
        Debug.Print elem
    Next
End Sub

                

Lösung von: Jens Kelm (@JKooP)

// variante für html/css
// ausgabe als DIV

function verticalize(arr) {
  return '<div>'
      + '<p style="text-orientation:upright; writing-mode:vertical-lr">'
      + arr.join('<br>')
      + '</p></div>';
}

document.write(
  verticalize ( ['programmier', 'aufgaben', '.ch'] )
);

                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

#include <stdio.h>
#include <string.h>

#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))

size_t get_longest_word(const char* words[], size_t size) {
    size_t max = 0;
    for (size_t i = 0; i < size; ++i)
        max = MAX(max, strlen(words[i]));
    return max;
}

int main() {
    const char* words[] = { "programmier", "aufgaben", ".ch" };

    size_t size = sizeof(words) / sizeof(words[0]);
    size_t longest = get_longest_word(words, size);

    for (size_t i = 0; i < longest; ++i) {
        for (size_t k = 0; k < size; ++k)
            if (i < strlen(words[k]))
                printf("%c ", words[k][i]);
        printf("\n");
    }
}

                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

p a .
r u c
o f h
g g  
r a  
a b  
m e  
m n  
i    
e    
r

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 1
Schwierigkeit: Mittel
Webcode: wrnx-dth6
Autor: ()

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen