Advertisement
grubcho

Decode radio frequences

Jul 3rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You are given an array of doubles (one line, space-separated). Your task is to convert the double values to their character //representations and insert them into a list like so:
  7. //•   Example: 83.105
  8. //•   Extract the left part of the number and convert it to its ASCII code representation: 83  S
  9. //•   Extract the right part of the number and convert I to its ASCII code representation: 105  i
  10. //•   Insert the left character at the position equal to the index of the double number in the original array.  Index of double: 0  insert S at index 0
  11. //•   Insert the right character at the position equal to the index of the double number in the original array, counted in reverse: index //of the double: 0  insert “i” at index 0, counted in reverse (so, the end of the list)
  12. //Repeat the aforementioned algorithm for each element of the double array, until you run out of elements. When you do, print the list //with no delimiter.
  13. //If any of the parts of the double number are 0 (such as 86.0 or 0.97), ignore them and do not insert them anywhere.
  14.  
  15. namespace Decode_radio_frequences
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             string[] frequences = Console.ReadLine().Split(' ').ToArray();
  22.             List<char> result = new List<char>();
  23.  
  24.             for (int i = 0; i < frequences.Length; i++)
  25.             {
  26.                 char left = TakeLeft(frequences[i]);
  27.                 char right = TakeRight(frequences[i]);
  28.                 if ((int)left != 0)
  29.                 {
  30.                     result.Insert(i, left);
  31.                 }
  32.                 if ((int)right != 0)
  33.                 {
  34.                     result.Insert(result.Count - i, right);
  35.                 }                
  36.             }
  37.             Console.WriteLine(string.Join("", result));            
  38.         }
  39.         static char TakeLeft(string left)
  40.         {
  41.             int[] leftRight = left.Split('.').Select(int.Parse).ToArray();
  42.             int leftPart = leftRight[0];
  43.             char leftChar = (char)leftPart;
  44.             return leftChar;
  45.         }
  46.         static char TakeRight(string right)
  47.         {
  48.             int[] leftRight = right.Split('.').Select(int.Parse).ToArray();
  49.             int rightPart = leftRight[1];
  50.             char rightChar = (char)rightPart;
  51.             return rightChar;
  52.         }
  53.     }        
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement