SimeonTs

SUPyF2 Text-Pr.-Ex. - 08. Letters Change Numbers

Oct 27th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. """
  2. Text Processing - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1740#7
  4.  
  5. SUPyF2 Text-Pr.-Ex. - 08. Letters Change Numbers (not included in final score)
  6.  
  7. Problem:
  8. Nakov likes Math. But he also likes the English alphabet a lot.
  9. He invented a game with numbers and letters from the English alphabet.
  10. The game was simple. You get a string consisting of a number between two letters.
  11. Depending on whether the letter was in front of the number or after it you would perform different mathematical
  12. operations on the number to achieve the result.
  13. First you start with the letter before the number.
  14. • If it's uppercase you divide the number by the letter's position in the alphabet.
  15. • If it's lowercase you multiply the number with the letter's position in the alphabet.
  16. Then you move to the letter after the number.
  17. • If it's uppercase you subtract its position from the resulted number.
  18. • If it's lowercase you add its position to the resulted number.
  19. But the game became too easy for Nakov really quick.
  20. He decided to complicate it a bit by doing the same but with multiple strings keeping track of only the total
  21. sum of all results. Once he started to solve this with more strings and bigger numbers it became quite hard to do it
  22. only in his mind. So he kindly asks you to write a program that calculates the sum of all numbers after the
  23. operations on each number have been done.
  24. For example, you are given the sequence "A12b s17G":
  25. We have two strings – "A12b" and "s17G". We do the operations on each and sum them.
  26. We start with the letter before the number on the first string. A is Uppercase and its position in the alphabet is 1.
  27. So we divide the number 12 with the position 1 (12/1 = 12). Then we move to the letter after the number.
  28. b is lowercase and its position is 2. So we add 2 to the resulted number (12+2=14).
  29. Similarly for the second string s is lowercase and its position is 19 so we multiply it with the number (17*19 = 323).
  30. Then we have Uppercase G with position 7, so we subtract it from the resulted number (323 – 7 = 316).
  31. Finally, we sum the 2 results and we get 14 + 316=330.
  32. Input
  33. The input comes from the console as a single line, holding the sequence of strings.
  34. Strings are separated by one or more white spaces.
  35. The input data will always be valid and in the format described. There is no need to check it explicitly.
  36. Output
  37. Print at the console a single number: the total sum of all
  38. processed numbers rounded up to two digits after the decimal separator.
  39. Constraints
  40. The count of the strings will be in the range [1 … 10].
  41. • The numbers between the letters will be integers in range [1 … 2 147 483 647].
  42. • Time limit: 0.3 sec. Memory limit: 16 MB.
  43. Examples:
  44. Input:      Output:
  45. A12b s17G   330.00
  46. Comments:
  47. 12/1=12, 12+2=14, 17*19=323, 323–7=316, 14+316=330
  48.  
  49. Input:                   Output:
  50. P34562Z q2576f   H456z   46015.13
  51.  
  52. Input:                   Output:
  53. a1A                      0.00
  54. """
  55. all_codes = [code for code in input().split()]
  56. all_nums = []
  57. for code in all_codes:
  58.     first_letter = code[0]
  59.     last_letter = code[-1]
  60.     num = int(''.join([item for item in code if item.isdigit()]))
  61.     if first_letter.isupper():
  62.         num /= (ord(first_letter.lower()) - 96)
  63.     elif first_letter.islower():
  64.         num *= (ord(first_letter.lower()) - 96)
  65.     if last_letter.isupper():
  66.         num -= (ord(last_letter.lower()) - 96)
  67.     elif last_letter.islower():
  68.         num += (ord(last_letter.lower()) - 96)
  69.     all_nums += [num]
  70. print(f"{(sum(all_nums)):.2f}")
Add Comment
Please, Sign In to add comment