Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //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.
- //Example: 514 first digit – 5 insert 514 at the 5th index of the list.
- //After you insert all the elements, print the list, separated by single spaces.
- namespace integer_insertion
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> inputList = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- string command = string.Empty;
- bool end = true;
- while (end)
- {
- command = Console.ReadLine();
- if (command == "end")
- {
- end = false;
- break;
- }
- else
- {
- int firstDigit = FirstDigit(command);
- if (firstDigit <= inputList.Count)
- {
- inputList.Insert(firstDigit, int.Parse(command));
- }
- else
- {
- inputList.Add(int.Parse(command));
- }
- }
- }
- Console.WriteLine(string.Join(" ", inputList));
- }
- static int FirstDigit(string command)
- {
- char[] digits = command.ToCharArray();
- int firstDigit = (int) digits[0] - 48;
- return firstDigit;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement