Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

danail2003 avatar danail2003 27 Точки

List Manipulation Advanced

Здравейте, имам 40/100.

 

1.List Manipulation Advanced

Next, we are going to implement more complicated list commands, extending the previous task. Again, read a list and keep reading commands until you receive "end":

Contains {number} – check if the list contains the number and if so - print "Yes", otherwise print "No such number".
PrintEven – print all the even numbers, separated by a space.
PrintOdd – print all the odd numbers, separated by a space.
GetSum – print the sum of all the numbers.
Filter {condition} {number} – print all the numbers that fulfill the given condition. The condition will be either '<', '>', ">=", "<=".

After the end command, print the list only if you have made some changes to the original list. Changes are made only from the commands from the previous task.

Example

Input

Output

2 13 43 876 342 23 543

Contains 100

Contains 543

PrintEven

PrintOdd

GetSum

Filter >= 43

Filter < 100

end

No such number

Yes

2 876 342

13 43 23 543

1842

43 876 342 543

2 13 43 23

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace List_Manipulation_Advanced
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
            string command = Console.ReadLine();
            int counter = 0;

            while (command != "end")
            {
                string[] token = command.Split();

                if (token[0] == "Contains")
                {
                    int number = int.Parse(token[1]);

                    for (int i = 0; i < numbers.Count; i++)
                    {
                        if (numbers[i] == number)
                        {
                            counter++;
                        }                       
                    }

                    if (counter > 0)
                    {
                        Console.WriteLine("Yes");
                    }
                    else
                    {
                        Console.WriteLine("No such number");
                    }
                }
                else if (token[0] == "PrintEven")
                {
                    for (int i = 0; i < numbers.Count; i++)
                    {
                        int evenOrOdd = numbers[i];

                        if (evenOrOdd % 2 == 0)
                        {
                            Console.Write(evenOrOdd + " ");
                        }                      
                    }

                    Console.WriteLine();
                }
                else if (token[0] == "PrintOdd")
                {
                    for (int i = 0; i < numbers.Count; i++)
                    {
                        int oddNumber = numbers[i];

                        if (oddNumber % 2 != 0)
                        {
                            Console.Write(oddNumber + " ");
                        }
                    }

                    Console.WriteLine();
                }
                else if (token[0] == "GetSum")
                {
                    Console.WriteLine(numbers.Sum());
                }
                else if (token[0] == "Filter")
                {
                    if (token[1] == "<")
                    {
                        int number = int.Parse(token[2]);

                        for (int i = 0; i < numbers.Count; i++)
                        {
                            if (numbers[i] < number)
                            {
                                Console.Write(string.Join(" ", numbers[i]) + " ");
                            }
                        }

                        Console.WriteLine();
                    }
                    else if (token[1] == ">")
                    {
                        int number = int.Parse(token[2]);

                        for (int i = 0; i < numbers.Count; i++)
                        {
                            if (numbers[i] > number)
                            {
                                Console.Write(string.Join(" ", numbers[i]) + " ");
                            }
                        }

                        Console.WriteLine();
                    }
                    else if (token[1] == ">=")
                    {
                        int number = int.Parse(token[2]);

                        for (int i = 0; i < numbers.Count; i++)
                        {
                            if (numbers[i] >= number)
                            {
                                Console.Write(string.Join(" ", numbers[i]) + " ");
                            }
                        }

                        Console.WriteLine();
                    }
                    else
                    {
                        int number = int.Parse(token[2]);

                        for (int i = 0; i < numbers.Count; i++)
                        {
                            if (numbers[i] <= number)
                            {
                                Console.Write(string.Join(" ", numbers[i] + " "));
                            }
                        }

                        Console.WriteLine();
                    }
                }

                command = Console.ReadLine();
            }
        }
    }
}

 

0
Programming Fundamentals
SvetoslavGeorgiev86 avatar SvetoslavGeorgiev86 17 Точки

Първо имаш малко точки защото тази задача е продължение на предната с Add, Insert, ... и второ с малко промени ти ще си ги видиш най добре ти дава 100/100 https://pastebin.com/tw6gh3Yf. А ако искаш и друг вариант и малко по кратък сигурен съм че и още по кратки има но това е моето решение: https://pastebin.com/2Qvituf8

Успех!!

0
danail2003 avatar danail2003 27 Точки

Благодаря ти !

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.