Advertisement
grubcho

Integer insertion (char[] digits = command.ToCharArray()) -

Jun 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You will receive a list of integers on the same line (separated by one space). On the next lines, you will start receiving a list of //strings, until you receive the string “end”. Your task is to insert each string (converted to integer) at a specific index in the //list. The index is determined by the first digit of the number.
  7. //Example: 514  first digit – 5  insert 514 at the 5th index of the list.
  8. //After you insert all the elements, print the list, separated by single spaces.
  9.  
  10. namespace integer_insertion
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             List<int> inputList = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  17.             string command = string.Empty;
  18.             bool end = true;
  19.             while (end)
  20.             {
  21.                 command = Console.ReadLine();
  22.                 if (command == "end")
  23.                 {
  24.                     end = false;
  25.                     break;
  26.                 }
  27.                 else
  28.                 {
  29.                     int firstDigit = FirstDigit(command);
  30.                     if (firstDigit <= inputList.Count)
  31.                     {
  32.                         inputList.Insert(firstDigit, int.Parse(command));
  33.                     }
  34.                     else
  35.                     {
  36.                         inputList.Add(int.Parse(command));
  37.                     }
  38.                    
  39.                 }
  40.             }
  41.             Console.WriteLine(string.Join(" ", inputList));
  42.         }
  43.         static int FirstDigit(string command)
  44.         {
  45.             char[] digits = command.ToCharArray();
  46.             int firstDigit = (int) digits[0] - 48;
  47.             return firstDigit;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement