Aufgabe 5

1. Frage

Lösung

~ $ cat artikel.txt | tr '[:upper:]' '[:lower:]' | tr -s ' ' '\n' | sort | uniq -c | sort -nr | head -5
     45 die
     36 und
     36 der
     22 den
     15 wie

2. Frage

Lösung

#!usr/bin/perl
#Aufgabe 5.2
#Liest eine Zeile von der Konsole ein und zählt die Vokale

use strict;

{

   my ($line,$char,@chars);
   my $a = 0;
   my $e = 0;
   my $i = 0;
   my $o = 0;
   my $u = 0;

   print "Ich bin ein Programm, das Vokale zählt -\n";
   print "Bitte geben Sie eine Zeile ein >>> ";
   $line = <>;
   chomp($line);

   @chars = split(//,$line);

   foreach $char(@chars){
      if (lc($char) eq "a"){
         $a++;
      }
      if (lc($char) eq "e"){
         $e++;
      }
      if (lc($char) eq "i"){
         $i++;
      }
      if (lc($char) eq "o"){
         $o++;
      }
      if (lc($char) eq "u"){
         $u++;
      }
   }

   print "a kommt $a","mal vor.\n";
   print "e kommt $e","mal vor.\n";
   print "i kommt $i","mal vor.\n";
   print "o kommt $o","mal vor.\n";
   print "u kommt $u","mal vor.\n";
   
}

3. Frage

Lösung

#!usr/bin/perl
#Aufgabe 5.3
#Gibt zwischen 0 und 20 jede gerade Zahl aus

use strict;

{
   my $i;

   for ($i = 0; $i <= 20; $i+=2){
      print "$i\n";
   }
}

4. Frage

Lösung

#!usr/bin/perl
#Aufgabe 5.4
#Liest eine Zahl ein und testet, ob es eine gerade Zahl,
#ob sie gleich -6, -8 oder -10 ist und
#ob die Zahl eine negative Zahl ist, aber ja nicht kleiner als -50 ist.

use strict;

{
   
   my $zahl;
   
   print "Bitte geben Sie eine Zahl en und ich teste dann,\nob es eine gerade Zahl,\n";
   print "ob sie gleich -6, -8 oder -10 ist und \n";
   print "ob die Zahl eine negative Zahl ist, aber ja nicht kleiner als -50.\n";
   print "\nZahl >>> ";
   $zahl = <>;
   chomp($zahl);
   
   if($zahl%2 == 0){
      print "Die eingegebene Zahl ist gerade,\n";
   }else{
      print "Die eingegebene Zahl ist ungerade,\n";
   }
   if($zahl == -6 || $zahl == -8 || $zahl == -10){
      print "ist $zahl,\n";
   }else{
      print "ist weder -6, noch -8, noch -10,\n";
   }
   if($zahl < 0){
      print "ist negativ, ";
      if($zahl < -50){
         print "und zwar sogar kleiner als -50.\n";
      }else{
         print "aber nicht kleiner als -50.\n";
      }
   }else{
      print "sondern eine positive Zahl.\n";
   }
   
}

5. Frage

Lösung mit for-Schleife,

#!usr/bin/perl
#Aufgabe 5.5
#Gibt alle Zahlen von 0 bis 30 aus und gibt jeweils nach 5 Zahlen Bescheid

use strict;

{
   my $i;
   my $count = 0;
    
   for($i = 0; $i <= 30; $i++){
      print "$i\n";
      $count++;
      if($count == 5){
         print "Das waren wieder 5 Zahlen.\n";
         $count = 0;
      }
   }
}

oder mit foreach-Schleife

#!usr/bin/perl
#Aufgabe 5.5
#Gibt alle Zahlen von 0 bis 30 aus und gibt jeweils nach 5 Zahlen Bescheid

use strict;

{

   my @zahlen = (0..30);
   my $count = 0;
   my $zahl;
   
   foreach $zahl(@zahlen){
      print "$zahl\n";
      $count++;
      if($count == 5){
         print "Das waren wieder 5 Zahlen.\n";
         $count = 0;
      }
   }
}

6. Frage

Lösung

#!usr/bin/perl
#Aufgabe 5.6
#Passwort-Programm
#5 Versuche

use strict;

{
   my $passwort = "Freitag";
   my $eingabe;
   my $versuche = 5;
   
   print "Passwort >>> ";
   $eingabe = <>;
   chomp($eingabe);
   
   while (($versuche > 1) && ($eingabe ne $passwort)){
      $versuche--;
      print "Passwort falsch. Noch $versuche Versuche.\nPasswort >>> ";
      $eingabe = <>;
      chomp($eingabe);
   }
   if ($eingabe eq $passwort){
      print "Gratulation.\n";
   }else{
   print "Keine Versuche mehr.\n";
   }
}

7. Frage

Lösung

#!usr/bin/perl
#Aufgabe 5.7
#Liest ein Wort ein und testet, ob der erste und der letzte Buchstabe identisch sind

use strict;

{
   my ($word, $length, @letters);
   
   print "Ich teste, ob der erste und der letzte Buchstabe eines Wortes identisch sind.\n";
   print "(Ohne Berücksichtigung von Groß- und Kleinschreibung)\n\n";
   print "Bitte geben Sie ein Wort ein >>> ";
   $word = <>;
   chomp($word);
   $word = lc($word);
   
   $length = length($word);
   @letters = split(//,$word);
   
   if($letters[0] eq $letters[$length-1]){
      print "Erster und letzter Buchstabe sind identisch!\n";
   }else{
      print "Erster und letzter Buchstabe sind nicht identisch!\n";  
   }
}

8. Frage

Lösung

#!usr/bin/perl
#Aufgabe 5.8
#Liest ein Wort ein und testet, ob es ein Palindrom ist

use strict;

{
   my ($word, $lower_word, $reverse_word);
   
   print "Ich bin ein Palindrom-Programm :)\n";
   print "Bitte geben Sie ein Wort ein >>> ";
   $word = <>;
   chomp($word);
   
   $lower_word = lc($word);
   $reverse_word = reverse($lower_word);
   
   if($reverse_word eq $lower_word){
      print "$word ist ein Palindrom :)\n";
   }else{
      print "$word ist kein Palindrom :(\n";
   }
}

oder ohne reverse- Funktion.

#!usr/bin/perl
#Palindrom-Programm ohne reverse-Funktion

use strict;
use warnings;

{
   my ($word,$lc_word,@chars,$index_last,$pali,$i);

   print "Ich teste, ob Ihr eingegebenes Wort ein Palindrom ist.\nEingabe >>> ";

   $word = <>;
   chomp($word);
   $lc_word = lc($word);

   $index_last = length($word)-1;
   
   @chars = split(//, $lc_word);
   $pali = "true";

   for ($i = 0; $i < length($word)/2; $i++){
      if($chars[$i] ne $chars[$index_last-$i]){  
         $pali = "false";
      }
   }
   
   if ($pali eq "true"){
      print "$word ist ein Palindrom!\n";
   }else{
      print "$word ist kein Palindrom!\n";
   }
}
    
   

9. Frage

Lösung

   a) Dezimalzahl 16

	16 : 2 = 8 Rest 0  
 	 8 : 2 = 4 Rest 0
 	 4 : 2 = 2 Rest 0
 	 2 : 2 = 1 Rest 0
	 1 : 2 = 0 Rest 1 
	Binär: 10000

	16 : 16 = 1 Rest 0  
	 1 : 16 = 0 Rest 1 
	Hexadezimal: 10

	16 :  8 = 2 Rest 0
	 2 :  8 = 0 Rest 2
	Oktal: 20


   b) Oktalzahl 777

	777 = 7*8² + 7*8¹ + 7*8⁰ = 511
	Dezimal: 511

	511 : 2 = 255  Rest 1 
	255 : 2 = 127  Rest 1 
	127 : 2 =  63  Rest 1
	 63 : 2 =  31  Rest 1
	 31 : 2 =  15  Rest 1
	 15 : 2 =   7  Rest 1
	  7 : 2 =   3  Rest 1
	  3 : 2 =   1  Rest 1
	  1 : 2 =   0  Rest 1
	Binär: 111111111
	
	511 : 16 = 31 Rest 15
	 31 : 16 =  1 Rest 15
	  1 : 16 =  0 Rest  1
	Hexadezimal : 1FF
   
 
   c)   Hexadezimalzahl FF
	
	FF = 15*16¹ + 15*16⁰ = 255
	Dezimal: 255

	255 : 2 = 127 Rest 1
	127 : 2 =  63 Rest 1
	 63 : 2 =  31 Rest 1
	 31 : 2 =  15 Rest 1
	 15 : 2 =   7 Rest 1
	  7 : 2 =   3 Rest 1
	  3 : 2 =   1 Rest 1
	  1 : 2 =   0 Rest 1
	Binär: 11111111
	
	255 : 8 = 31 Rest 7
	 31 : 8 =  3 Rest 7
	  3 : 8 =  0 Rest 3
	Oktal: 377

	
   d)  Dezimalzahl 512

       512 : 2 = 256 Rest 0
       256 : 2 = 128 Rest 0
       128 : 2 = 64  Rest 0
        64 : 2 = 32  Rest 0
        32 : 2 = 16  Rest 0
        16 : 2 = 8   Rest 0
         8 : 2 = 4   Rest 0
         4 : 2 = 2   Rest 0
         2 : 2 = 1   Rest 0
         1 : 2 = 0   Rest 1
	Binär: 1000000000

	512 : 8 = 64 Rest 0
	 64 : 8 =  8 Rest 0
	  8 : 8 =  1 Rest 0
	  1 : 8 =  0 Rest 1
	Oktal : 1000

	512 : 16 = 32 Rest 0
	 32 : 16 =  2 Rest 0
	  2 : 16 =  0 Rest 2
	Hexadezimal: 200