Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Web-Code (Simulationen)

Für ein Buchprojekt wird ein Web-Code benötigt. Mit diesem Web-Code können Artikel direkt online abgefragt werden. Der Code soll aus acht Zeichen bestehen. Vorkommen dürfen Ziffern und Kleinbuchstaben.
Um Verwechslungen zu vermeiden, kommen die Ziffer Eins (1) und der Kleinbuchstabe "ell" (l) nicht vor. Ebenso kommt die Null (0) nicht vor. Dass das große Oh (O) nicht vorkommen kann, ist klar, denn die Vorgabe erlaubt nur Kleinbuchstaben. Schreiben Sie ein Programm, das fünf zufällige Web-Codes erzeugt.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

8 Lösung(en)

/**
 * Erzeuge einige zufällige Web-Codes
 * Vorgabe: Kleinbuchstaben und Ziffern, ohne 1 (eins) und ohne l (kleines Ell), ebenso ohne Oh(O).
 * @author Philipp Gressly (phi@gressly.ch)
 */
/*
 * History: first Implementation: May 25, 2010
 * Bugs   :
 */
public class WebCode {
  public static void main(String[] args) {
    new WebCode().top();
  }
 
  
  char [] GUELTIGE_ZEICHEN = {'2', '3', '4', '5', '6', '7', '8', '9',
                              'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                              'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's',
                              't', 'u', 'v', 'w', 'x', 'y', 'z'};
  int CODE_LAENGE = 8;
  
  void top() {
      int anz = 5; // Einige Codes erzeugen
      while(anz > 0) {
          String code = erzeugeWebCode();
          System.out.println("Code = " + code);
          anz = anz - 1;
      }
  }
  
  String erzeugeWebCode() {
      String code = "";
      int zeichenPosition = 1;
      while(zeichenPosition <= CODE_LAENGE) {
          int zufallsPosition = (int) (Math.random() * GUELTIGE_ZEICHEN.length);
          char zufallsZeichen = GUELTIGE_ZEICHEN[zufallsPosition];
          code = code + zufallsZeichen;
          zeichenPosition = zeichenPosition + 1;
      }
      return code;
  }
  
}  // end of class WebCode
                
/**
 * Erzeuge einige zufällige Web-Codes
 * Vorgabe: Kleinbuchstaben und Ziffern, ohne 1 (eins) und ohne l (kleines Ell).
 * @author Stefan Dürrenberger (sd@pm7.ch)
 */
for ($i = 0; $i < 5; $i++)
{
    echo generateWebCode() . "\n";
}

function generateWebCode($length = 8)
{
    $characters = "23456789abcdefghijkmnopqrstuvwxyz";
    for ($i = 0; $i < $length; $i++)
    {
        $webCode .= $characters[mt_rand(0, strlen($characters) - 1)];
    }
    return $webCode;
}
                
import random
def webcode():
    zeichen = ['2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c',
               'd', 'e', 'f', 'g', 'h', 'i','j', 'k', 'm', 'n', 'o',
               'p', 'q', 'r', 's','t', 'u', 'v', 'w', 'x', 'y', 'z']
    webcode = ''
    for i in range(8):
        random.shuffle(zeichen)
        webcode = webcode + zeichen[0]
    return webcode

print webcode()
                
#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
	char strErlaubt[] = { '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's','t', 'u', 'v', 'w', 'x', 'y', 'z' };
	char strText[8];
	int iZufall = 0;
	int iLänge = 0;

	srand(time(NULL));

	for(int i = 0; i < 8; i += 1) {
		iZufall = rand() % 33 + 1;
		strText[i] = strErlaubt[iZufall];
	}

	printf("Web-Code: ");

	for(int i = 0; i < 8; i += 1) {
		printf("%c", strText[i]);

		if(i == 3) {
			printf(" ");
		}
	}

	getchar();
	return 0;
}
                

Lösung von: Elias Zech (Optics Balzers)

function randomWebCode() {
  let digits = '23456789abcdefghjkmnopqrstuvwxyz'.split(''),
  code = '';
  while (code.length < 8)
    code += digits[Math.floor(Math.random() * digits.length)];
  return code;
}

for (let i = 1; i <= 5; i++) console.log(randomWebCode());   // lissalanda@gmx.at

                

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

// NET 6.x | C# 10.x | VS-2022

// Variante 1 (komfortable Kurzschreibweise [pattern], aber deutlich ineffizienter bei der Ausführung)
var allowedChars1 = "2-9a-hj-z";
var arrChars1 = new System.Text.RegularExpressions.Regex(@"[" + allowedChars1 + "]").Matches(new string(Enumerable.Range(0, 256).Select(x => (char)x).ToArray())).ToArray();
PrintWebCode(arrChars1, 8, 5);

// Variante 2 
var allowedChars2 = "23456789abcdefghjkmnopqrstuvwxyz";
var arrChars2 = allowedChars2.ToCharArray();
PrintWebCode(arrChars2);

// Variante 3
var arrChars3 = new List<char>();
for (int i = '2'; i <= 'z'; i++)
    if (char.IsDigit((char)i) || char.IsLower((char)i) && (char)i != 'l')
        arrChars3.Add((char)i);
PrintWebCode2(arrChars3.ToArray(), 8);

// Ausgabe Variante 1 (LINQ + Lambda)
void PrintWebCode<T>(T[] arr, int l = 8, int n = 1) => Enumerable.Range(0, n).ToList().ForEach(x => Console.WriteLine(string.Join("", arr.OrderBy(x => Guid.NewGuid()).Take(l))));

// Ausgabe Variante 2 (klassisch)
void PrintWebCode2<T>(T[] arr, int l = 8, int n = 1) {
    for (int i = 0; i < n; i++) {
        for (int k = 0; k < l; k++)
            Console.Write(arr[new Random().Next(0, arr.Length)]);
        Console.WriteLine();
    }
}
                

Lösung von: Jens Kelm (@JKooP)

// C++ 14 | VS-2022

#include <iostream>

std::string get_webCode(const std::string allowedChars = "23456789abcdefghjkmnopqrstuvwxyz") {
	std::string webCode{};
	for (auto i{ 0 }; i < 8; i++)
		webCode.push_back(allowedChars[rand() % allowedChars.size()]);
	return webCode;
}

int main() {
	srand((int)time(nullptr));
	for (auto i{ 0 }; i < 5; i++)
		std::cout << get_webCode() << std::endl;
}
                

Lösung von: Jens Kelm (@JKooP)

' als VBA-Funktion
Function GetWebCode(Optional allowedChars As String = "23456789abcdefghjkmnopqrstuvwxyz") As String
    Randomize
    Dim webCode As String, i As Integer
    For i = 1 To 9
        webCode = webCode + IIf(i = 5, "-", Mid(allowedChars, Int(Rnd * Len(allowedChars)) + 1, 1))
    Next
    GetWebCode = webCode
End Function
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Vergleichen Sie Ihre Codes mit den Web-Codes in vorliegendem Buch.

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.5
Schwierigkeit: k.A.
Webcode: d2rh-rur6
Autor: Martin Guggisberg (Universität Basel / PH FHNW)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen