Професионална програма
Loading...
Twitchy avatar Twitchy 12 Точки

More Exercise: Treasure Finder

Здравейте колеги,

гърми последният тест и не мога да разбера къде е грешката.

Кодът: https://pastebin.com/N7ejv0At

Условието на задачата:

Write a program that decrypts a message by a given key and gathers information about hidden treasure type and its coordinates. On the first line you will receive a key (sequence of numbers). On the next few lines until you receive "find" you will get lines of strings. You have to loop through every string and decrease the ascii code of each character with a corresponding number of the key sequence. The way you choose a key number from the sequence is just looping through it. If the length of the key sequence is less than the string sequence, you start looping from the beginning of the key. For more clarification see the example below. After decrypting the message you will get a type of treasure and its coordinates. The type will be between the symbol '&' and the coordinates will be between the symbols '<' and '>'. For each line print the type and the coordinates in format "Found {type} at {coordinates}".

Input

Output

Comment

1 2 1 3

ikegfp'jpne)bv=41P83X@

ujfufKt)Tkmyft'duEprsfjqbvfv=53V55XA

find

Found gold at 10N70W

Found Silver at 32S43W

We start looping through the first string and the key. When we reach the end of the key we start looping from the beginning of the key, but we continue looping through the string. (until the string is over)

 

The first message is: "hidden&gold&at<10N70W>" so we print we found gold at the given coordinates

 

We do the same for the second string

"thereIs&Silver&atCoordinates<32S43W>"(starting from the beginning of the key and the beginning of the string)

Тагове:
0
Fundamentals Module 19/11/2019 11:14:24
peter.trifonoff avatar peter.trifonoff 120 Точки
Best Answer

Здравей, малко си го завъртял кода :)

Ето едно по-кратко решение.

https://pastebin.com/quqkPFdY

 

0
Twitchy avatar Twitchy 12 Точки

Много по-оптимално решение, благодаря!

0
MartinHPetkov avatar MartinHPetkov 1 Точки

Може ли да направиш кода видим, за да видим по краткия и елегантен вариант на решението?

Моето е https://pastebin.com/aQrp2KgU

0
28/12/2019 18:22:02
JulianJulianov avatar JulianJulianov 4 Точки

Здравей! Задължително ли е с Regex да се реши тази задача и защо? Ето го моя: https://pastebin.com/iR7UnbE5 , но ми дава само 20/100? 

Извинявай, намерих грешката си . Вместо  if (index == key.Length) бях написал  if (index == 4) Голяма излагация от моя страна.

0
25/04/2020 16:32:43
Elena123456 avatar Elena123456 200 Точки

Здравейте,

упражнението за мен е доста интересно и искам да споделя един подход на т.нар. лупване, който Наков показва в Basic. Успех на курсистите, които тази сряда на 18.11 ще имат лаб по стрингове и текстообработка.

 

using System;
using System.Text;
using System.Linq;

namespace text
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
            string text = string.Empty;
            string type = string.Empty;
            string coordinates = string.Empty;
            while((text=Console.ReadLine())!="find")
            {
                int maxLength = Math.Max(numbers.Length, text.Length);// found maxLength
                StringBuilder sb = new StringBuilder(maxLength);
                for (int i = 0; i < maxLength; i++)
                {
                    sb.Append((char)(text[i] - numbers[i % numbers.Length]));// NB! how to looping
                }

                int startIndexType = sb.ToString().IndexOf('&') + 1;
                int endIndexType = sb.ToString().IndexOf('&', startIndexType + 1);
                int typeLength = endIndexType - startIndexType;
                type = sb.ToString().Substring(startIndexType, typeLength);

                int startIndexCoordinates = sb.ToString().IndexOf('<') + 1;
                int endIndexCoordinates = sb.ToString().IndexOf('>');
                int coordinatesLength = endIndexCoordinates - startIndexCoordinates;
                coordinates = sb.ToString().Substring(startIndexCoordinates, coordinatesLength);
                Console.WriteLine($"Found {type} at {coordinates}");
            }
        }
    }
}

0