S
S
Student20022021-04-27 09:25:37
.NET
Student2002, 2021-04-27 09:25:37

How to generate/iterate over a string within a range?

Everyone will accept, need help with generation within the range
, let's say there are strings "1abc3" you need to get
1abc2
1abc1
1abc0
1abb9
1abb8
1abb7

What I have now. 2 arrays with the alphabet and numbers 0-9
, I take the first character from the end in a loop, and depending on the index in the array, I do a generation, but this way it only goes through 1 character, I tried to write a loop in which all this would be considered but does not exit. At the moment, I only see a way to do as many cycles as there are characters in a line. But this is already noodles ... I haven’t slept for a day, maybe the answer is banal, but I don’t see it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cicatrix, 2021-04-27
@Student2002

And what is the alphabet? 0-9A-Z, 0-9A-z, 0-9A-F or something else?
Can the 4th digit take on literal values? Is there a different alphabet for each digit?

class ComplexValue
    {
        const string m_Alphabet = "abcdefghijklmnopqrstuvwxyz";
        int First { get; set; }
        char[] Middle { get; set; };
        int Last { get; set; }
        public ComplexValue(string input)
        {
            First = int.Parse(input.Substring(0, 1));
            Middle = input.Substring(1, 3).ToCharArray();
            Last = int.Parse(input.Substring(input.Length - 1, 1));
        }

        public string Value() => $"{First}{new string(Middle)}{Last}";

        private int IncrementLast()
        {
            Last++;
            if (Last > 9) { Last = 0; return 1; }
            return 0;
        }

        private int DecrementLast()
        {
            Last--;
            if (Last < 0) { Last = 9; return 1 }
            return 0;
        }

        private char IncrementChar(char c, out bool carry)
        {
            carry = false;
            c++;
            if (c > m_Alphabet[m_Alphabet.Length -1] )
            {
                c = m_Alphabet[0];
                carry = true;
            }
            return c;
        }

        private char DecrementChar(char c, out bool borrow)
        {
            borrow = false;
            c--;
            if (c < m_Alphabet[0])
            {
                c = m_Alphabet[m_Alphabet.Length - 1];
                borrow = true;
            }
            return c;
        }
        
        private int IncrementMiddle()
        {
            int pos = Middle.Length - 1;
            while (pos > 0)
            {
                Middle[pos] = IncrementChar(Middle[pos], out bool carry);
                if (!carry) return 0;
                pos--;
            }
            return 1;
        }

        private int DecrementMiddle()
        {
            int pos = Middle.Length - 1;
            while (pos > 0)
            {
                Middle[pos] = DecrementChar(Middle[pos], out bool borrow);
                if (!borrow) return 0;
                pos--;
            }
            return 1;
        }

        public void Increment()
        {
            if (IncrementLast() > 0)
            {
                if (IncrementMiddle() > 0) First++;
            }
        }

        public void Decrement()
        {
            if (DecrementLast() > 0)
            {
                if (DecrementMiddle() > 0) First--;
            }
        }
    }

V
Vladimir Korotenko, 2021-04-27
@firedragon

personally, I see either a system or a mismatch
, let's say there are lines "1abc3" you need to get
Well, for example GetSequence(1abc3 , 6);
result
1abc2
1abc1
1abc0
1abb9
1abb8
1abb7
That is, this sequence is from 0 to 9 in the low order and possibly from 0 to F in the high order.
What a puzzle.
The simplest option is if it is 0-F then the conversion with a counter, but this will not work here. Maybe you didn't understand the problem? Such perverted tasks I think should not be in real life.
maybe so

static void Main(string[] args)
        {
            var start = "1abc3";
            GetCascading(start, 6);
        }

        static string[] GetCascading(string val, int count)
        {
            var result = new List<string>();
            var intVal = int.Parse(val, System.Globalization.NumberStyles.HexNumber);
            for (var i = 1; i < count + 1; i++) result.Add((intVal - i).ToString("X"));
            return result.ToArray();
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question