<?php
/**
* Task 2: Parse the string
*
* Parse the $input string and build object tree that represents this structure:
* https://drive.google.com/file/d/0BwhWcFzxN6_mZ2hkQlhZTnRrMDA/view?usp=sharing
* Use Node class to build the tree and dump it to output
*
* Realize Node::dump() function and use it to output the tree in format:
* A
* B
* C
* C
* C
* B
* C
* C
* A
* B
* B
* C
*/
$input = 'A(B(CCC)B(CC))A(BB(C))';
class Node {
public $letter;
private $items = [];
public function __construct($letter) {
$this->letter = $letter;
}
public function addItem($item) {
$this->items[] = $item;
}
public function dump($level = 0) {
// write your code here...
}
}
function createTree(string $input) {
$jsonInput = $input;
$jsonInput = str_replace(['(', ')'], ['[', ']'], $jsonInput);
$jsonInput = preg_replace('~(\w)\[~', '"${1}":', $jsonInput);
echo $jsonInput;
}
createTree($input);
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
"A":"B":CCC]"B":CC]]"A":B"B":C]]
Output for 5.6.38
Catchable fatal error: Argument 1 passed to createTree() must be an instance of string, string given, called in /in/LUdKn on line 50 and defined in /in/LUdKn on line 42
Process exited with code 255.