Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created January 15, 2021 13:51
Show Gist options
  • Save sedera-tax/bf10e637558a83c4e92847c722ce1ca2 to your computer and use it in GitHub Desktop.
Save sedera-tax/bf10e637558a83c4e92847c722ce1ca2 to your computer and use it in GitHub Desktop.
5. Thesaurus A thesaurus contains words and synonyms for each word. Below is an example of a data structure that defines a thesaurus: array("buy" => array("purchase"), "big" => array("great", "large")) Implement the function getSynonyms, which accepts a word as a string and returns all synonyms for that word in JSON format, as in the example be…
<?php
class Thesaurus
{
private $thesaurus;
function __construct(array $thesaurus)
{
$this->thesaurus = $thesaurus;
}
public function getSynonyms(string $word) : string
{
return (array_key_exists($word, $this->thesaurus)) ? '{"word":'.json_encode($word).',"synonyms":'.json_encode($this->thesaurus[$word]).'}' : '{"word":'.json_encode($word).',"synonyms":[]}';
}
}
$thesaurus = new Thesaurus(
[
"buy" => array("purchase"),
"big" => array("great", "large")
]
);
echo $thesaurus->getSynonyms("big");
echo "\n";
echo $thesaurus->getSynonyms("agelast");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment