Skip to content

Commit 92a020b

Browse files
authored
Merge pull request mouredev#3658 from gabrielmoris/08-php
#8 - php
2 parents 128297f + 317dc85 commit 92a020b

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?php
2+
/*
3+
* EXERCISE:
4+
* Explore the concept of a class and create an example that implements an initializer,
5+
* attributes, and a function that prints them (taking into account the possibilities
6+
* of your language).
7+
* Once implemented, create it, set its parameters, modify them, and print them
8+
* using its function.
9+
*/
10+
class My_Class
11+
{
12+
// This would be the private properties:
13+
private $data;
14+
15+
// Constructor
16+
public function __construct()
17+
{
18+
$this->data = [];
19+
}
20+
21+
// Private functions are available only internally
22+
23+
private function _count()
24+
{
25+
return count($this->data);
26+
}
27+
28+
// Public Functions are available outside
29+
public function add(string $item): int
30+
{
31+
$this->data[] = $item;
32+
return $this->_count();
33+
}
34+
35+
public function delete($item): int
36+
{
37+
$pos = array_search($item, $this->data);
38+
unset($this->data[$pos]);
39+
return $this->_count();
40+
}
41+
42+
public function clear(): int
43+
{
44+
$this->data = [];
45+
return $this->_count();
46+
}
47+
48+
public function find($item)
49+
{
50+
$pos = array_search($item, $this->data);
51+
return $this->data[$pos];
52+
}
53+
54+
public function is_empty()
55+
{
56+
return empty($this->data);
57+
}
58+
59+
60+
// Getters
61+
public function get_data(): array
62+
{
63+
return $this->data;
64+
}
65+
66+
// Setters
67+
public function set_data(array $data): void
68+
{
69+
$this->data = $data;
70+
}
71+
}
72+
73+
$my_class = new My_Class();
74+
$my_class->add("item1");
75+
$my_class->add("item2");
76+
$my_class->add("item3");
77+
$my_class->add("item4");
78+
$my_class->add("item5");
79+
80+
foreach ($my_class->get_data() as $data) {
81+
echo "Data: " . $data . "\n";
82+
}
83+
84+
$my_class->set_data([1, 2, 3, 4, 5]);
85+
foreach ($my_class->get_data() as $data) {
86+
echo "Data: " . $data . "\n";
87+
}
88+
$is_empty = $my_class->is_empty() ? " YES\n" : " NO\n";
89+
echo "Is Empty? : " . $is_empty;
90+
$my_class->clear();
91+
$is_empty2 = $my_class->is_empty() ? " YES\n" : " NO\n";
92+
echo "And Now? : " . $is_empty2;
93+
94+
/* EXTRA DIFFICULTY (optional):
95+
* Implement two classes that represent the Stack and Queue structures (studied
96+
* in exercise number 7 of the study path)
97+
* - They must be able to initialize and have operations to add, remove,
98+
* return the number of elements, and print all their contents.
99+
*/
100+
101+
102+
class Stack
103+
{
104+
private $pages;
105+
private $length;
106+
107+
// Constructor
108+
public function __construct()
109+
{
110+
$this->items = [];
111+
$this->length = 0;
112+
}
113+
114+
public function push($item)
115+
{
116+
$this->length += 1;
117+
array_push($this->items, $item);
118+
}
119+
120+
public function pop()
121+
{
122+
$this->length -= 1;
123+
return array_pop($this->items);
124+
}
125+
126+
public function peek()
127+
{
128+
if (empty($this->items)) {
129+
return null;
130+
}
131+
132+
return $this->items[count($this->items) - 1];
133+
}
134+
135+
public function size()
136+
{
137+
return $this->length;
138+
}
139+
140+
public function search($item)
141+
{
142+
$index = array_search($item, $this->items);
143+
144+
if (!$index && $index !== 0) {
145+
return -1;
146+
}
147+
148+
return $index;
149+
}
150+
151+
public function clear()
152+
{
153+
$this->length = 0;
154+
$this->items = [];
155+
}
156+
}
157+
158+
echo "====== STACK ======\n";
159+
$st = new Stack();
160+
$st->push(1);
161+
echo $st->peek() . "\n"; // 1
162+
$st->push(2);
163+
echo $st->peek() . "\n"; // 2
164+
echo $st->search(1) . "\n"; //0
165+
echo $st->pop() . "\n"; // 2
166+
echo $st->peek() . "\n"; // 1
167+
echo $st->search(2) . "\n"; // -1
168+
169+
170+
class Queue
171+
{
172+
private $items;
173+
private $length;
174+
175+
// Constructor
176+
public function __construct()
177+
{
178+
$this->items = [];
179+
$this->length = 0;
180+
}
181+
182+
public function enqueue($item)
183+
{
184+
$this->length += 1;
185+
array_push($this->items, $item);
186+
}
187+
188+
public function dequeue()
189+
{
190+
$this->length -= 1;
191+
return array_shift($this->items);
192+
}
193+
194+
public function peek()
195+
{
196+
if (empty($this->items)) {
197+
return null;
198+
}
199+
200+
return $this->items[0];
201+
}
202+
203+
public function size()
204+
{
205+
return $this->length;
206+
}
207+
208+
public function search($item)
209+
{
210+
$index = array_search($item, $this->items);
211+
212+
if (!$index && $index !== 0) {
213+
return -1;
214+
}
215+
216+
return $index;
217+
}
218+
219+
public function clear()
220+
{
221+
$this->length = 0;
222+
$this->items = [];
223+
}
224+
}
225+
226+
echo "====== QUEUE ======\n";
227+
$qu = new Queue();
228+
$qu->enqueue(1);
229+
echo $qu->peek() . "\n"; // 1
230+
$qu->enqueue(2);
231+
echo $qu->peek() . "\n"; // 1
232+
echo $qu->search(1) . "\n"; // 0
233+
echo $qu->dequeue() . "\n"; // 1
234+
echo $qu->peek() . "\n"; // 2
235+
echo $qu->search(2) . "\n"; // 0

0 commit comments

Comments
 (0)