Advertisement
YavorGrancharov

Array_Manipulator(91%)

Oct 15th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Array_Manipulator
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.  
  13.             List<int> list = input
  14.                 .Split(' ')
  15.                 .Select(int.Parse)
  16.                 .ToList();
  17.  
  18.             while (input != "print")
  19.             {
  20.                 input = Console.ReadLine();
  21.                 string[] tokens = input.Split(' ').ToArray();
  22.                 if (tokens[0] == "add")
  23.                 {
  24.                     int index = int.Parse(tokens[1]);
  25.                     int element = int.Parse(tokens[2]);
  26.                     list.Insert(index, element);
  27.                 }
  28.                 else if (tokens[0] == "addMany")
  29.                 {
  30.                     int index = int.Parse(tokens[1]);
  31.                     List<int> nums = tokens.Skip(2).Select(int.Parse).ToList();
  32.                     list.InsertRange(index, nums);
  33.                 }
  34.                 else if (tokens[0] == "contains")
  35.                 {
  36.                     int existing = int.Parse(tokens[1]);
  37.                     if (list.Contains(existing))
  38.                     {
  39.                         for (int i = 0; i < list.Count; i++)
  40.                         {
  41.                             if (existing == list[i])
  42.                             {
  43.                                 Console.WriteLine(i);
  44.                             }                            
  45.                         }
  46.                     }
  47.                     else
  48.                     {
  49.                         Console.WriteLine(-1);
  50.                     }
  51.                 }
  52.                 else if (tokens[0] == "remove")
  53.                 {
  54.                     int index = int.Parse(tokens[1]);
  55.                     list.RemoveAt(index);
  56.                 }
  57.                 else if (tokens[0] == "sumPairs")
  58.                 {
  59.                     int sum = 0;
  60.                     int count = 0;
  61.                     List<int> temp = new List<int>();
  62.                     for (int i = 0; i < list.Count; i += 2)
  63.                     {
  64.                         count++;
  65.                         if (count <= list.Count / 2)
  66.                         {
  67.                             sum = list[i] + list[i + 1];
  68.                             temp.Add(sum);
  69.                         }
  70.                         else
  71.                         {
  72.                             temp.Add(list[i]);
  73.                         }
  74.                     }
  75.                     list.Clear();
  76.                     for (int j = 0; j < temp.Count; j++)
  77.                     {
  78.                         list.Add(temp[j]);
  79.                     }
  80.                 }
  81.                 else if (tokens[0] == "shift")
  82.                 {
  83.                     int shift = int.Parse(tokens[1]);
  84.                     for (int i = 0; i < shift; i++)
  85.                     {
  86.                         list.Add(list[0]);
  87.                         list.Remove(list[0]);
  88.                     }
  89.                 }
  90.             }
  91.             Console.WriteLine($"[{string.Join(", ", list)}]");
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement