M
M
Mister_krid2022-03-22 11:57:21
C++ / C#
Mister_krid, 2022-03-22 11:57:21

How can the code (review) be improved?

The task was to number the letters of the alphabet in the input string starting with "a = 1" and find the word with the highest value (not using Linq, current collections.generic and system). It works clearly, but I have suspicions that these triple nested loops are finally not cool, I would like an alternative (if any) and, in general, point out bad things.

using System;
using System.Collections.Generic;

namespace CW_Sort_words
{
    public class Kata
    {
        private const string Alphabet = "abcdefghijklmnopqrstuvwxyz";
        public static string Higt(string s)
        {
            string alphabet = Alphabet;
            char[] arrAlphabet = alphabet.ToCharArray();
            string[] word = s.Split(' ');
            int[] value = new int [word.Length];

            for (int i = 0; i < word.Length; i++)
            {                    
                char[] letter = word[i].ToCharArray();

                for (int j = 0; j < letter.Length; j++)
                {
                    for (int b = 0; b < arrAlphabet.Length; b++)
                    {
                        if(letter[j] == arrAlphabet[b])
                        {
                            if (b == 0)     //проверка т.к. отсчет букв идет с 1, а не 0. 
                            {
                                value[i] += 1; 
                                break;
                            }
                            else
                            {
                                value[i] += b + 1;
                                break;
                            }                          
                        }                    
                    }
                }
            }

            return word[MaxIndex(value)];

        }

        //ищем индекс наибольшего значения в массиве
        private static int MaxIndex(int[] array)
        {
            int max = int.MinValue;
            int index = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] > max)
                    max = array[i];
            }

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == max)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }
    }
    public static class Program
    {
        
        static void Main(string[] args)
        {
            string str = "man i need a taxi up to ubud"; 
            Console.WriteLine(Kata.Higt(str)); //выведет taxi
            
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
V Sh., 2022-03-22
@Mister_krid

If you want to output only one word with the maximum cost, then you can do this:
1. Replace your structure with a check for "Alphabet" with a simple cast of the character to an integer value (something like this: int value = (int)charValue. If important so that 'a' = 1, then you can do this: int value = charValue - 'a' + 1);
2. Enter the variables maxIndex (the index of the most expensive word in the words array), maxValue (the value of the most expensive word in words) and currentValue (the cost of the current word); On the initial iteration maxIndex = 0 and maxValue = 0;
3. Calculate currentValue in a loop where we run through the characters of the word;
4. Compare the calculated cost with the maximum one and, if anything, change the maxIndex and maxValue values ​​to the current ones;
5. At the end, return words[maxIndex];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question