JulianJulianov

13.TextprocessingExercise-Letters Change Numbers

Apr 21st, 2020
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. 08.*Letters Change Numbers
  2. Nakov likes Math. But he also likes the English alphabet a lot. He invented a game with numbers and letters from the English alphabet. The game was simple. You get a string consisting of a number between two letters. Depending on whether the letter was in front of the number or after it you would perform different mathematical operations on the number to achieve the result.
  3. First you start with the letter before the number.
  4. If it's uppercase you divide the number by the letter's position in the alphabet.
  5. If it's lowercase you multiply the number with the letter's position in the alphabet.
  6. Then you move to the letter after the number.
  7. If it's uppercase you subtract its position from the resulted number.
  8. • If it's lowercase you add its position to the resulted number.
  9. But the game became too easy for Nakov really quick. He decided to complicate it a bit by doing the same but with multiple strings keeping track of only the total sum of all results. Once he started to solve this with more strings and bigger numbers it became quite hard to do it only in his mind. So he kindly asks you to write a program that calculates the sum of all numbers after the operations on each number have been done.
  10. For example, you are given the sequence "A12b s17G":
  11. We have two strings – "A12b" and "s17G". We do the operations on each and sum them. We start with the letter before the number on the first string. A is Uppercase and its position in the alphabet is 1. So we divide the number 12 with the position 1 (12/1 = 12). Then we move to the letter after the number. b is lowercase and its position is 2. So we add 2 to the resulted number (12+2=14). Similarly for the second string s is lowercase and its position is 19 so we multiply it with the number (17*19 = 323). Then we have Uppercase G with position 7, so we subtract it from the resulted number (3237 = 316). Finally, we sum the 2 results and we get 14 + 316=330.
  12. Input
  13. The input comes from the console as a single line, holding the sequence of strings. Strings are separated by one or more white spaces.
  14. The input data will always be valid and in the format described. There is no need to check it explicitly.
  15. Output
  16. Print at the console a single number: the total sum of all processed numbers rounded up to two digits after the decimal separator.
  17. Constraints
  18. The count of the strings will be in the range [110].
  19. • The numbers between the letters will be integers in range [12 147 483 647].
  20. • Time limit: 0.3 sec. Memory limit: 16 MB.
  21. Examples
  22. Input                           Output                           Comments
  23. A12b s17G                       330.00                           12/1=12, 12+2=14, 17*19=323, 3237=316, 14+316=330
  24. P34562Z q2576f   H456z          46015.13   
  25. a1A                             0.00   
  26.  
  27. using System;
  28.                    
  29. public class Program
  30. {
  31.      public static void Main(string[] args)
  32.      {
  33.             var lettersChNums = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  34.             var result = 0.0;
  35.             var finalResult = 0.0;
  36.             foreach (var subString in lettersChNums)
  37.             {
  38.                 var number = subString.Substring(1, subString.Length - 2);
  39.                 var onlyLetters = subString.Replace(number, string.Empty);
  40.                 var counterLetters = 0;
  41.                 foreach (var letter in onlyLetters)
  42.                 {
  43.                     counterLetters++;
  44.                     if (counterLetters == 1)
  45.                     {
  46.                         if (letter >= 65 && letter <= 90)
  47.                         {
  48.                             var upLetterPosition = Math.Abs(90 - letter - 26);
  49.                             var divide = double.Parse(number) / upLetterPosition;
  50.                             result = divide;
  51.                         }
  52.                         else if (letter >= 97 && letter <= 122)
  53.                         {
  54.                             var lowLetterPosition = Math.Abs(122 - letter - 26);
  55.                             var multiply = double.Parse(number) * lowLetterPosition;
  56.                             result = multiply;
  57.                         }
  58.                     }
  59.                     else
  60.                     {
  61.                         if (letter >= 65 && letter <= 90)
  62.                         {
  63.                             var upLetterPosition = Math.Abs(90 - letter - 26);
  64.                             var subtract = result - upLetterPosition;
  65.                             result = subtract;
  66.                         }
  67.                         else if (letter >= 97 && letter <= 122)
  68.                         {
  69.                             var lowLetterPosition = Math.Abs(122 - letter - 26);
  70.                             var add = result + lowLetterPosition;
  71.                             result = add;
  72.                         }
  73.                     }
  74.                 }
  75.                 finalResult += result;
  76.             }
  77.             Console.WriteLine($"{finalResult:F2}");
  78.      }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment