forked from Filpizza/php6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_test.php
More file actions
105 lines (85 loc) · 3.1 KB
/
first_test.php
File metadata and controls
105 lines (85 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
require_once("utils.php");
/**
* Function receives an array with integer numbers,
* should return its sum. It is not alowed to use built-in php functions.
*
* Функция получает на вход массив чисел, должна вернуть сумму этих чисел.
* Не использовать встроенные функции суммирования php.
*
* @param array $arr
* @return integer
*/
function my_sum($arr) {
$sum = 0;
for ($i = 0; $i < count($arr); $i++){
$sum = $sum + $arr[$i];
}
return $sum;
}
/**https://www.google.com/webhp?hl=ru&ictx=2&sa=X&ved=0ahUKEwjpju3usKPZAhXJXSwKHYt5DHYQPQgD
* Function receives a long string with many words.
* It should return the same string, but words,
* larger then 6 symbols, should be changed, symbols
* after the sixth one should be replaced by symbol *
*
* Функция получает на вход длинную строку с множеством слов.
* Она должна вернуть ту же строку, но в словах, которые длиннее 6 символов,
* функция должна вместо всех символов после шестого поставить одну звездочку.
* Пример: Из слова 'verwijdering' должно получиться 'verwij*'
*
* @param string $shortMe
* @return string
*/
function shortener($shortMe) {
$txt = explode(" ", $shortMe);
for ($i = 0; $i <= count($txt); $i++){
if (strlen($txt[$i]) > 6) {
$txt[$i] = substr($txt[$i], 0, 6) . "*";
}
}
return implode(" ", $txt);
}
/**
* Function receives an array of strings.
* Please return number of strings, which
* length is at least 2 symbols and first character
* is equal to the last character
*
* Функция получает на вход массив строк. Вернуть надо количество строк,
* которые не короче двух символов и у которых первый и последний
* символ совпадают.
*
* @param array $arr
* @return number
*/
function compare_ends($arr) {
$number = 0;
foreach ($arr as $str) {
if (strlen($str) >= 2) {
$a = str_split($str,1);
$first = $a[0];
$last = array_pop($a);
if ($first == $last) {
$number++;
}
}
}
return $number;
}
/**
* Function receives a string, should return this string reversed.
*
* Функция получает на вход строку, должна вернуть ее перевернутой.
*
* @param string $str
* @return string
*/
function reverse_string($str) {
$rev = strrev($str);
return $rev;
}
test_shortener();
test_compare_ends();
test_my_sum();
test_reverse_string();