PHP :: Aufgabe #346

2 Lösungen Lösungen öffentlich

Noch mehr Mustervergleiche (word pattern)

Fortgeschrittener - PHP von JKooP - 17.04.2021 um 19:10 Uhr
Gegeben ist ein Text (String), der ausschließlich aus kleingeschriebenen Wörtern besteht,
welche durch Leerzeichen getrennt sind.
Weiterhin gibt es eine Sequenz (String) mit der der Text verglichen werden soll (Mustergleichheit).

Beispiele:
t = "hund katze katze hund"
s = "abba"
Lösung: wahr => a = hund; b = katze; b = katze; a = hund

t = "hund katze maus hund"
s = "abba"
Lösung: falsch => a = hund; b = katze; b = maus; a = hund (b ist unterschiedlich)

t = "hund hund hund hund"
s = "aaa"
Lösung: falsch => Anzahl stimmt nicht überein

Schreibe eine Methode/Funktion, mit der man die Mustergleichheit überprüfen kann.
Als fakultative Herausforderung: ohne die implementierten Funktionen wie etwa Dictionary, map, set, zip, etc.

Viel Spaß

Lösungen:

vote_ok
von tnle (120 Punkte) - 31.05.2021 um 14:55 Uhr
Quellcode ausblenden PHP-Code
<?php
  
  function checkMuster($values, $keys, $res= ''){
    
    if (empty($keys)) 
        return $res == '' ?  "empty" : $res;
    else if ( count($keys) != count($values) )
        return "count is diffrence";

    $compareKey = array_splice($keys, 0, 1)[0];
    $compareValue = array_splice($values, 0, 1)[0];
    
    $count = count($keys);

    for($i = $count - 1; $i >= 0 ; $i--){
      if ($keys[$i] == $compareKey) {
          if ($compareValue != $values[$i]){
              return "$compareKey => $compareValue difference $keys[$i] => $values[$i]";
            }           
          array_splice($keys, $i, 1);
          array_splice($values, $i, 1);
      }
    }

    $res = "true";
    return checkMuster($values, $keys, $res);
};

$strValue = 'dog cathh cat dog'; 
$strKey = 'abba';
echo checkMuster(explode(' ', $strValue), str_split($strKey));
vote_ok
von Exception (7090 Punkte) - 26.06.2021 um 22:22 Uhr
Quellcode ausblenden PHP-Code
<?php

$patternCheck = function(string $text, string $pattern): bool {
  // validate input
  if (!preg_match('/[a-z ]+/', $text)) return false;
  if (!preg_match('/[a-z]+/', $pattern)) return false;
  
  $words = explode(' ', $text);
  $pattern = str_split($pattern);
  
  // validate pattern length with amount of words
  if (sizeof($words) !== sizeof($pattern)) return false;
  
  $map = [];
  
  // sort all words by into an associative array.
  // the key is the single pattern chracter.
  foreach ($words as $index => $word) {
    $map[$pattern[$index]][] = $word;
  }
 
  foreach ($map as $patternKey => $items) {
    // A value greater than 1 means not all items are equal, thus pattern error
    if (sizeof(array_flip($items)) > 1) return false;
  }
  
  return true;
};

var_dump($patternCheck('hund katze katze hund', 'abba'));
var_dump($patternCheck('hund katze maus hund', 'abba'));
var_dump($patternCheck('hund katze maus maus katze hund', 'abccba'));
var_dump($patternCheck('hund hund hund hund', 'aaa'));
var_dump($patternCheck('hund katze hund katze', 'abab'));

Konsolenausgabe:

bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
1814303

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.