Skip to content

Instantly share code, notes, and snippets.

@dmekhov
Last active March 6, 2017 14:53
Show Gist options
  • Save dmekhov/8ea37f31fd297f5b779d4a7d38cd338f to your computer and use it in GitHub Desktop.
Save dmekhov/8ea37f31fd297f5b779d4a7d38cd338f to your computer and use it in GitHub Desktop.
test
<?php
/**
* Судя по приведенному примеру отсчет количества символов "каждые n" идет с нуля
* В реализованной функции не реализована работа со сторокой в utf-8 кодировке.
* Если необходима работа с utf-8 кодировкой я могу написать свой вариант реализации функции str_split используя mb_* функции
*
*/
function encrypt($text, $n) {
if($n < 0) return false;
if($n == 0 || ($n + 1) >= strlen($text)) return $text;
for($i = $n; $i > 0; $i--) {
$source_string_array = str_split($text);
$new_string = '';
for($j = $n; $j < strlen($text); $j += ($n+1)) {
$new_string .= $source_string_array[$j];
unset($source_string_array[$j]);
}
$text = $new_string . implode('',$source_string_array);
}
return $text;
}
?>
<?php
function solution($str) {
if(!is_string($str)) return false;
$string_array = str_split($str, 2); //если требудется работа со строками в юникоде - нужно написать функцию перевода строки в массив самостоятельно, с использованием функций mb_*
if(strlen(end($string_array)) == 1) {
$string_array[key($string_array)] .= '_';
}
return implode(',',$string_array); //или вернуть массив - в зависимости от условий задачи
}
?>
<?php
function count_smileys($arr): int {
if(!is_array($str)) return false;
$count = 0;
foreach ($arr as $smile) {
if(strlen($smile) < 2 || strlen($smile) > 3) continue;
if(!preg_match("/[:;]/", $smile[0])) continue;
if(!preg_match("/[-~\)D]/", $smile[1])) continue;
if(strlen($smile) == 2 && preg_match("/[\)D]/", $smile[1])) $count++;
if(isset($smile[2]) && preg_match("/[\)D]/", $smile[2])) $count++;
continue;
}
return $count;
}
?>
<?php
function summation($n) {
if($n < 1) return false;
$result = 0;
for($i = 0; $i <= $n; $i++) {
$result = $result + $i;
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment