Guest User

Untitled

a guest
Nov 15th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1. <?php
  2. $input = preg_split('/\s+/', readline());
  3. $totalSum = 0;
  4. $sum = 0;
  5. foreach ($input as $values) {
  6.     $re = '/((?<front>[a-zA-z])(?<num>\d+))(?<back>[a-zA-z])/m';
  7.     preg_match_all($re, $values, $matches, PREG_SET_ORDER, 0);
  8.     for ($i = 0; $i < count($matches); $i++) {
  9.         $front = $matches[$i]['front'];
  10.         $num = $matches[$i]['num'];
  11.         $back = $matches[$i]['back'];
  12.         $frontPos = ord(strtoupper($front)) - ord('A') + 1;
  13.         $backPos = ord(strtoupper($back)) - ord('A') + 1;
  14.         if (ctype_upper($front)) {
  15.             $sum = $num / $frontPos;
  16.         } else {
  17.             $sum = $num * $frontPos;
  18.         }
  19.         if (ctype_upper($back)) {
  20.             $sum -= $backPos;
  21.         } else {
  22.             $sum += $backPos;
  23.         }
  24.     }
  25.     $totalSum += $sum;
  26. }
  27. printf("%.2f", round($totalSum, 2));
Add Comment
Please, Sign In to add comment