In sich aufsteigende Wörter (Zeichenketten)
Laden Sie die Datei deutsch.zip (ab Web-Code) herunter und entpacken Sie diese. Darin finden Sie die Datei deutsch.txt (die Rechtschreibe-Datei des Browsers Firefox).
Schreiben Sie nun ein Programm, dass alle Wörter dieser Datei ausgibt, die in sich alphabetisch aufsteigend sind. Also alle Wörter, deren Buchstaben in alphabetischer Reihenfolge auftreten.
Dateien:
0 Kommentare
4 Lösung(en)
import codecs
word_list = []
def check_reihenfolge(s):
#Unicode
tmp=u'0'
for c in s:
if c < tmp:
return False
tmp=c
return True
# Woerter File einlesen
def read_woerter(name):
f = codecs.open(name, encoding='utf-8')
for line in f:
#Wort ohne Zeilenumbruch
wort = line.split()
wort = wort[0].lower()
if (check_reihenfolge(wort)==True):
word_list.append(wort)
for w in word_list:
print w
read_woerter('deutsch.txt')
String.prototype.isAlphabetAscending = function() {
if (this.length == 1) return true;
let w = this.toLowerCase();
for (let i = 0; i < w.length-1; i++) {
if (w[i].localeCompare(w[i+1]) != -1) return false;
}
return true;
}
/*----------------------------------------------------*\
| Der einfachheit halber wurde deutsch.txt bearbeitet, |
| im html geladen und liegt als array $list vor. |
\*----------------------------------------------------*/
let arr = [];
for (let i = 0; i < list.length; i++)
if (list[i].isAlphabetAscending()) arr.push(list[i]);
console.log(arr.join(', '));
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
Print(@"C:\...\deutsch.txt");
string[] ReadFile(string path) => File.Exists(path) ? File.ReadAllLines(path) : Array.Empty<string>();
bool IsAscending(string s) {
for (int i = 1; i < s.Length; i++)
if (s[i] <= s[i - 1]) return false;
return true;
}
IEnumerable<string> GetAlphabetList(string path) => ReadFile(path).Where(x => IsAscending(x.ToLower()));
void Print(string path) => GetAlphabetList(path).ToList().ForEach(x => Console.WriteLine(x));
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
const bool is_ascending(const std::string& str_) {
auto str{ str_ };
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
for (auto i{ 1LL }; i < str.length(); ++i)
if (str[i] <= str[i - 1]) return false;
return true;
}
int main() {
const std::string path{ "C:\\...\\deutsch.txt" };
std::string line;
std::ifstream input(path);
if (!input) {
std::cerr << "Pfad nicht gefunden: " << path << "\n";
return EXIT_FAILURE;
}
while (std::getline(input, line)) {
if (is_ascending(line))
std::cout << line << "\n";
}
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | |
Schwierigkeit: | k.A. |
Webcode: | aira-ngmu |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |