JulianJulianov

12.TextProcessingExercise-String Explosion

Apr 20th, 2020
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. 12.  String Explosion
  2. Explosions are marked with '>'. Immediately after the mark, there will be an integer, which signifies the strength of the explosion.
  3. You should remove x characters (where x is the strength of the explosion), starting after the punch character ('>').
  4. If you find another explosion mark ('>') while you’re deleting characters, you should add the strength to your previous explosion.
  5. When all characters are processed, print the string without the deleted characters.
  6. You should not delete the explosion character – '>', but you should delete the integers, which represent the strength.
  7. Input
  8. You will receive single line with the string.
  9. Output
  10. Print what is left from the string after explosions.
  11. Constraints
  12. • You will always receive a strength for the punches
  13. • The path will consist only of letters from the Latin alphabet, integers and the char '>'
  14. • The strength of the punches will be in the interval [09]
  15. Examples
  16. Input                           Output          Comments
  17. abv>1>1>2>2asdasd               abv>>>>dasd     1st explosion is at index 3 and it is with strength of 1. We delete only the digit
  18.                                                 after the explosion character. The string will look like this: abv>>1>2>2asdasd
  19.                                                 2nd explosion is with strength one and the string transforms to this: abv>>>2>2asdasd
  20.                                                 3rd explosion is now with strength of 2. We delete the digit and we find another
  21.                                                 explosion. At this point the string looks like this: abv>>>>2asdasd.
  22.                                                 4th explosion is with strength 2. We have 1 strength left from the previous explosion,
  23.                                                 we add the strength of the current explosion to what is left and that adds up to a
  24.                                                 total strength of 3. We delete the next three characters and we receive the string
  25.                                                 abv>>>>dasd. We do not have any more explosions and we print the result: abv>>>>dasd.
  26.  
  27. pesho>2sis>1a>2akarate>4hexmaster   pesho>is>a>karate>master
  28.    
  29. using System;
  30.  
  31. namespace _StringExplosion
  32. {
  33.     public class Program
  34.     {
  35.         public static void Main(string[] args)
  36.         {
  37.             var stringExplosion = Console.ReadLine();            //Genius way
  38.  
  39.             var strength = 0;
  40.             for (int i = 0; i < stringExplosion.Length; i++)
  41.             {
  42.                 if (stringExplosion[i] != '>' && strength > 0)
  43.                 {
  44.                     stringExplosion = stringExplosion.Remove(i, 1);
  45.                     strength--;
  46.                     i--;
  47.                    
  48.                 }
  49.                 else if (stringExplosion[i] == '>')
  50.                 {
  51.  
  52.                     strength += int.Parse(stringExplosion[i + 1].ToString());
  53.                 }
  54.             }
  55.             Console.WriteLine(stringExplosion);
  56.             //var stringExplosion = Console.ReadLine().Split('>');                 //Hard way
  57.             //var listOutput = new List<string>();
  58.             //var strength = 0;
  59.             //var reserveStrength = 0;
  60.             //var counter = 0;
  61.             //foreach (var item in stringExplosion)
  62.             //{
  63.             //    var digit = 0;
  64.             //    counter++;
  65.             //    if (Char.IsDigit(item[0]))
  66.             //    {
  67.             //        digit = int.Parse(item[0].ToString());
  68.             //        if (digit <= 1)
  69.             //        {
  70.             //            if (item.Length > 1)
  71.             //            {
  72.             //                var newItem = item.Remove(0, strength);
  73.             //                listOutput.Insert(counter - 1, newItem + '>');
  74.             //            }
  75.             //            else
  76.             //            {
  77.             //                listOutput.Add(">");
  78.             //            }
  79.             //            continue;
  80.             //        }
  81.             //        else
  82.             //        {
  83.             //            strength = digit - 1;
  84.             //            if (item.Length > 1 && strength > 0)
  85.             //            {
  86.             //                var newItem = item.Remove(0, strength + reserveStrength + 1);
  87.             //                if (stringExplosion.Length != counter)
  88.             //                {
  89.             //                    listOutput.Insert(counter - 1, newItem + '>');
  90.             //                }
  91.             //                else
  92.             //                {
  93.             //                    listOutput.Insert(counter - 1, newItem );
  94.             //                }
  95.  
  96.             //            }
  97.             //            else
  98.             //            {
  99.             //                reserveStrength += digit - 1;
  100.             //                listOutput.Add(">");
  101.             //            }
  102.             //        }
  103.             //    }
  104.             //    else
  105.             //    {
  106.             //        listOutput.Add(item + '>');
  107.             //    }
  108.             //}
  109.             //Console.WriteLine(string.Join("", listOutput));
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment