Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Task 10*
- * We are given numbers N and M and the following operations:
- * N = N+1
- * N = N+2
- * N = N*2
- * Write a program that finds the shortest sequence of operations
- * from the list above that starts from N and finishes in M.
- * Hint: use a queue.
- * Example: N = 5, M = 16
- * Sequence: 5 ---> 6 ---> 8 ---> 16
- */
- namespace Task10ShortestSequenceOfOperations
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Task10ShortestSequenceOfOperations
- {
- /// <summary>
- /// Test method CalcShortestSequence.
- /// </summary>
- public static void Main()
- {
- List<int> shortestSequence = CalcShortestSequence(5, 16);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(0, 39);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(-1, 34);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(-21, 1);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(-18, 2);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(-21, -13);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(-20, -15);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- shortestSequence = CalcShortestSequence(-22, -12);
- Console.WriteLine(string.Join(" -> ", shortestSequence.OrderBy(i => i)));
- Console.WriteLine();
- }
- /// <summary>
- /// Find the shortest sequence of operations
- /// from the list above that starts from fromN and finishes in toM.
- /// </summary>
- /// <param name="fromN">Start number.</param>
- /// <param name="toM">End number.</param>
- /// <returns>List with shortest sequence.</returns>
- public static List<int> CalcShortestSequence(int fromN, int toM)
- {
- Queue<int> workContainer = new Queue<int>();
- List<int> result = new List<int>();
- workContainer.Enqueue(toM);
- result.Add(toM);
- while (workContainer.Peek() != fromN)
- {
- if (OperationOne(workContainer, result, fromN))
- {
- continue;
- }
- if (OperationTwo(workContainer, result, fromN))
- {
- continue;
- }
- if (OperationThree(workContainer, result, fromN))
- {
- continue;
- }
- }
- return result;
- }
- /// <summary>
- /// Support method to calculate first operation - /2.
- /// </summary>
- /// <param name="inputData">Input data.</param>
- /// <param name="bound">Start number.</param>
- /// <param name="result">Modified inputData.</param>
- /// <returns>Boolean value indicates successful operation.</returns>
- public static bool OperationOne(Queue<int> workContainer, List<int> result, int bound)
- {
- bool isSuccessful = false;
- int currentValue = workContainer.Peek();
- if (currentValue != 2 && currentValue > 0 && currentValue / 2 >= bound && currentValue % 2 == 0)
- {
- currentValue = workContainer.Dequeue() / 2;
- SaveOperation(workContainer, result, currentValue, ref isSuccessful);
- }
- return isSuccessful;
- }
- /// <summary>
- /// Support method to calculate second operation - -2.
- /// </summary>
- /// <param name="inputData">Input data.</param>
- /// <param name="bound">Start number.</param>
- /// <param name="result">Modified inputData.</param>
- /// <returns>Boolean value indicates successful operation.</returns>
- public static bool OperationTwo(Queue<int> workContainer, List<int> result, int bound)
- {
- bool isSuccessful = false;
- int currentValue = workContainer.Peek();
- if ((currentValue - 2 >= bound && ((currentValue <= 0 && bound % 2 == currentValue % 2) || (currentValue > 0 && currentValue - 2 < 0))) ||
- (currentValue > 0 && currentValue - 2 >= bound && currentValue % 2 == 0))
- {
- currentValue = workContainer.Dequeue() - 2;
- SaveOperation(workContainer, result, currentValue, ref isSuccessful);
- }
- return isSuccessful;
- }
- /// <summary>
- /// Support method to calculate third operation - -1.
- /// </summary>
- /// <param name="inputData">Input data.</param>
- /// <param name="bound">Start number.</param>
- /// <param name="result">Modified inputData.</param>
- /// <returns>Boolean value indicates successful operation.</returns>
- public static bool OperationThree(Queue<int> workContainer, List<int> result, int bound)
- {
- bool isSuccessful = false;
- int currentValue = workContainer.Peek();
- if ((currentValue <= 0 && bound % 2 != currentValue % 2) ||
- (currentValue > 0 && currentValue - 1 >= bound && !(currentValue < 0 && bound % 2 != 0)))
- {
- currentValue = workContainer.Dequeue() - 1;
- SaveOperation(workContainer, result, currentValue, ref isSuccessful);
- }
- return isSuccessful;
- }
- /// <summary>
- /// Support method to save data after operation.
- /// </summary>
- /// <param name="firstHolder">First data holder</param>
- /// <param name="secondHolder">Second data holder</param>
- /// <param name="data">Given data</param>
- /// <param name="isSuccessful">Result of operation</param>
- public static void SaveOperation(Queue<int> firstHolder, List<int> secondHolder, int data, ref bool isSuccessful)
- {
- secondHolder.Add(data);
- firstHolder.Enqueue(data);
- isSuccessful = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement