n4wn4w

C# ADVANCED podgotovka za izpit

May 22nd, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.47 KB | None | 0 0
  1. izpit purvi zadachi ////////////////////////////////////////////////////////////////////////////
  2.  
  3. 3 septemvri  eggs   ////////////////////////////////////////////////////////////////////////////
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string lineContents = String.Empty;
  17.  
  18.             string[] eventTokens;
  19.             string[] STRnumbers1;
  20.             string kor = "";
  21.             int hoi = 0;
  22.  
  23.             int sumEggs = 0;
  24.             for (int i = 0; i < 7; i++)
  25.             {
  26.                 lineContents = Console.ReadLine();
  27.                 eventTokens = lineContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  28.  
  29.                 hoi = int.Parse(eventTokens[0]);
  30.                 kor = eventTokens[1];
  31.  
  32.                 if (kor.Equals("eggs"))
  33.                 {
  34.                     sumEggs += hoi;
  35.                 }
  36.                 else if (kor.Equals("dozens"))
  37.                 {
  38.                     sumEggs += hoi * 12;
  39.                 }
  40.  
  41.             }
  42.            
  43.             int kondio = sumEggs / 12;
  44.             int kila = sumEggs % 12;
  45.  
  46.             Console.WriteLine("{0} dozens + {1} eggs", kondio, kila);
  47.         }
  48.     }
  49. }
  50.  
  51.  
  52.  
  53.  
  54. 26 may  Largest 3 Rectangles   ////////////////////////////////////////////////////////////////
  55.  
  56. using System;
  57. using System.Collections.Generic;
  58. using System.Linq;
  59. using System.Text;
  60.  
  61. namespace ConsoleApplication1
  62. {
  63.     class Program
  64.     {
  65.         static void Main(string[] args)
  66.         {
  67.  
  68.  
  69.             string[] STRnumbers = Console.ReadLine().Split(new char[] { ' ','X','[',']','x' }, StringSplitOptions.RemoveEmptyEntries);
  70.  
  71.            
  72.             int[] nums = Array.ConvertAll<string, int>(STRnumbers, int.Parse);
  73.  
  74.             List<int> listOfStrings = new List<int>();
  75.  
  76.  
  77.             for (int i = 0; i < nums.Length; i += 2)
  78.             {
  79.                 int hoi = nums[i] * nums[i + 1];
  80.                 listOfStrings.Add(hoi);
  81.             }
  82.  
  83.             int sum = 0;
  84.             int bestSum = 0;
  85.             for (int i = 0; i < listOfStrings.Count-2; i++)
  86.             {
  87.                 sum = 0;
  88.                 for (int j = i; j < 3 + i; j++)
  89.                 {
  90.                     sum += listOfStrings[j];
  91.                 }
  92.                 if (sum > bestSum)
  93.                 {
  94.                     bestSum = sum;
  95.                 }
  96.             }
  97.             Console.WriteLine(bestSum);
  98.         }
  99.     }
  100. }
  101.  
  102.  
  103. 3 septemvri bigest 3 prime numbers   /////////////////////////////////////////////////////////////////////////
  104.  
  105.  
  106.  
  107. using System;
  108. using System.Collections.Generic;
  109. using System.Linq;
  110. using System.Text;
  111. using System.Threading.Tasks;
  112.  
  113. class Program
  114. {
  115.     static void Main(string[] args)
  116.     {
  117.         string input = Console.ReadLine();
  118.         string[] inputTokens = input.Split(new char[] { ' ', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
  119.         int[] numbers = inputTokens.Select(int.Parse).ToArray(); // converting string to int[];
  120.  
  121.         List<int> primes = new List<int>();             // Collecting prime numbers
  122.  
  123.         for (int i = 0; i < numbers.Length; i++)
  124.         {
  125.             if (isPrime(numbers[i]))
  126.             {
  127.                 primes.Add(numbers[i]);
  128.             }
  129.         }
  130.    
  131.         primes.Sort();
  132.         int index = 0;
  133.         while (index < primes.Count - 1) // remove repeating numbers
  134.         {
  135.             if (primes[index] == primes[index + 1])
  136.                 primes.RemoveAt(index);
  137.             else
  138.                 index++;
  139.         }
  140.         primes.Reverse();
  141.  
  142.         //foreach (var item in primes)
  143.         //{
  144.         //    Console.WriteLine(item);
  145.         //}
  146.  
  147.         for (int i = 0; i < primes.Count; i++)
  148.         {
  149.             if (primes.Count < 3)
  150.             {
  151.                 Console.WriteLine("No");
  152.                 break;
  153.             }
  154.             else
  155.             {
  156.                 Console.WriteLine(primes[0] + primes[1] + primes[2]);
  157.                 break;
  158.             }
  159.                    
  160.         }
  161.     }
  162.  
  163.  
  164.     public static bool isPrime(int number)
  165.     {    
  166.             if ((number & 1) == 0)
  167.             {
  168.                 if (number == 2)
  169.                 {
  170.                     return true;
  171.                 }
  172.                 else
  173.                 {
  174.                     return false;
  175.                 }
  176.             }
  177.             // Note:
  178.             // ... This version was changed to test the square.
  179.             // ... Original version tested against the square root.
  180.             // ... Also we exclude 1 at the end.
  181.         for (int i = 3; (i * i) <= number; i += 2)
  182.         {
  183.             if ((number % i) == 0)
  184.             {
  185.                 return false;
  186.             }
  187.         }
  188.         return number != 1;
  189.         }
  190.     }
  191.  
  192.  
  193.  
  194. 1 zadacha simple expresion 1 june- java basic  //////////////////////////////////////////////////////////////
  195.  
  196. using System;
  197. using System.Collections.Generic;
  198. using System.Linq;
  199. using System.Text;
  200. using System.Collections;
  201. using System.Text.RegularExpressions;
  202.  
  203. namespace ConsoleApplication1
  204. {
  205.     class Program
  206.     {
  207.         static void Main(string[] args)
  208.         {
  209.  
  210.             string text = Console.ReadLine();
  211.  
  212.             string regex = @"\+|\-";
  213.  
  214.             Queue separators = new Queue();
  215.             Queue digits = new Queue();
  216.  
  217.  
  218.             string[] wordsInSentence = Regex.Split(text, regex);
  219.  
  220.  
  221.             foreach (var w in wordsInSentence)
  222.             {  
  223.                    digits.Enqueue(w.Trim());
  224.             }
  225.  
  226.             MatchCollection separatorsInSentance = Regex.Matches(text, regex);
  227.  
  228.             foreach (var s in separatorsInSentance)
  229.             {
  230.                 separators.Enqueue(s);
  231.             }
  232.  
  233.  
  234.             StringBuilder reversSentance = new StringBuilder();
  235.  
  236.             while (digits.Count > 0 || separators.Count > 0)
  237.             {
  238.                 reversSentance.Append(digits.Dequeue());
  239.                 if (digits.Count == 0)
  240.                 {
  241.                     continue;
  242.                 }
  243.                 reversSentance.Append(" ");
  244.                 reversSentance.Append(separators.Dequeue());    
  245.             }
  246.  
  247.           string builder = reversSentance.ToString();
  248.  
  249.           string[] STRnumbers = builder.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  250.            ///////////////////////////////////////////////////  1 ot variantite
  251.  
  252.          // int[] array = new int[STRnumbers.Length];
  253.          //   decimal[] array = Array.ConvertAll<string, decimal>(STRnumbers, decimal.Parse);
  254.  
  255.         //  decimal sum = array.Sum();
  256.  
  257.         //  Console.WriteLine("{0:F9}",sum);
  258.  
  259.             ///////////////////////////////////////// 2 variant
  260.            List<string> hoi = new List<string>();
  261.                  
  262.            for (int i = 0; i < STRnumbers.Length; i++)
  263.            {
  264.                hoi.Add(STRnumbers[i]);
  265.            }
  266.  
  267.            decimal total = hoi.Sum(x => Convert.ToDecimal(x));
  268.          
  269.            Console.WriteLine("{0:F9}",total);
  270.         }
  271.     }
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278. Problem 4 – Couples Frequency
  279.  
  280. using System;
  281. using System.Collections.Generic;
  282. using System.Linq;
  283.  
  284. public class CouplesFrequency
  285. {
  286.     public static void Main()
  287.     {
  288.         int[] numbers = Console.ReadLine()
  289.             .Split()
  290.             .Select(int.Parse)
  291.             .ToArray();
  292.  
  293.         int totalNumberOfCouples = numbers.Length - 1;
  294.  
  295.         var coupleFrequencies = new Dictionary<string, int>();
  296.  
  297.         for (int i = 1; i < numbers.Length; i++)
  298.         {
  299.             string currentCouple = string.Format(
  300.                 "{0} {1}",
  301.                 numbers[i - 1],
  302.                 numbers[i]);
  303.  
  304.             if (!coupleFrequencies.ContainsKey(currentCouple))
  305.             {
  306.                 coupleFrequencies.Add(currentCouple, 0);
  307.             }
  308.  
  309.             coupleFrequencies[currentCouple]++;
  310.         }
  311.  
  312.         foreach (var coupleFrequency in coupleFrequencies)
  313.         {
  314.             double frequency = coupleFrequency.Value * 100.0 / totalNumberOfCouples;
  315.  
  316.             Console.WriteLine(
  317.                 "{0} -> {1:F2}%",
  318.                 coupleFrequency.Key,
  319.                 frequency);
  320.         }
  321.     }
  322. }
  323.  
  324.  
  325. Problem 11 – Little John  ///////////////////////////////////////////////////////////////////////////
  326. 2 moe reshenie dava 62 to4ki
  327.  
  328. using System;
  329. using System.Linq;
  330. using System.Text;
  331. using System.Text.RegularExpressions;
  332. using System.Collections.Generic;
  333.  
  334. public class LittleJohn
  335. {
  336.     public static void Main()
  337.     {
  338.  
  339.         StringBuilder sb = new StringBuilder();
  340.  
  341.         //for (int i = 0; i < 4; i++)
  342.         //{
  343.         //    sb.AppendFormat(" {0}", Console.ReadLine());
  344.         //}
  345.         string STRnumbers = @">>>----->>abc>>>----->>
  346.                            >>>----->>
  347.                            >----->sdhdkiiiyyyyyyyyy>>>--->>>
  348.                            >>----->";
  349.  
  350.         const string ArrowPattern = @"[^->]((.\s)*?)";
  351.  
  352.         string[] wordsInSentence = Regex.Split(STRnumbers, ArrowPattern);
  353.  
  354.         List<string> listOfStrings = new List<string>();
  355.  
  356.  
  357.         foreach (var w in wordsInSentence)
  358.         {
  359.             if (w != "")
  360.             {
  361.                 listOfStrings.Add(w);
  362.             }
  363.         }
  364.  
  365.  
  366.  
  367.         Regex arrowMatcher = new Regex(ArrowPattern);
  368.  
  369.         var arrows = arrowMatcher.Matches(sb.ToString());
  370.  
  371.         byte largeArrowCount = 0;
  372.         byte mediumArrowCount = 0;
  373.         byte smallArrowCount = 0;
  374.  
  375.         for (int i = 0; i < listOfStrings.Count ; i++)
  376.         {
  377.             if (listOfStrings[i].Equals(">>>----->>"))
  378.             {
  379.                 largeArrowCount++;
  380.             }
  381.             else if (listOfStrings[i].Equals(">>----->"))
  382.             {
  383.                 mediumArrowCount++;
  384.             }
  385.             else if (listOfStrings[i].Equals(">----->"))
  386.             {
  387.                 smallArrowCount++;
  388.             }
  389.         }
  390.  
  391.         //foreach (Match arrow in arrows)
  392.         //{
  393.         //    if (!string.IsNullOrEmpty(arrow.Groups[1].Value))
  394.         //    {
  395.         //        largeArrowCount++;
  396.         //    }
  397.         //    else if (!string.IsNullOrEmpty(arrow.Groups[2].Value))
  398.         //    {
  399.         //        mediumArrowCount++;
  400.         //    }
  401.         //    else
  402.         //    {
  403.         //        smallArrowCount++;
  404.         //    }
  405.         //}
  406.  
  407.         string numberAsString = string.Format(
  408.             "{0}{1}{2}",
  409.             smallArrowCount,
  410.             mediumArrowCount,
  411.             largeArrowCount);
  412.  
  413.         short number = short.Parse(numberAsString);
  414.  
  415.         string binaryNum = Convert.ToString(number, 2);
  416.  
  417.         string reversedBinaryNum = new string(binaryNum.Reverse().ToArray());
  418.  
  419.         string resultAsString = binaryNum + reversedBinaryNum;
  420.  
  421.         short result = Convert.ToInt16(resultAsString, 2);
  422.  
  423.         Console.WriteLine(result);
  424.     }
  425. }
  426.  
  427.  
  428. 2  reshenie za 100 to4ki   //////////////////////////////////////////////////////////////////////////////
  429.  
  430.  
  431. using System;
  432. using System.Linq;
  433. using System.Text;
  434. using System.Text.RegularExpressions;
  435.  
  436. public class LittleJohn
  437. {
  438.     public static void Main()
  439.     {
  440.         StringBuilder sb = new StringBuilder();
  441.  
  442.         for (int i = 0; i < 4; i++)
  443.         {
  444.             sb.AppendFormat(" {0}", Console.ReadLine());
  445.         }
  446.  
  447.         const string ArrowPattern = @"(>>>----->>)|(>>----->)|(>----->)";
  448.  
  449.         Regex arrowMatcher = new Regex(ArrowPattern);
  450.  
  451.         var arrows = arrowMatcher.Matches(sb.ToString());
  452.  
  453.         int largeArrowCount = 0;
  454.         int mediumArrowCount = 0;
  455.         int smallArrowCount = 0;
  456.  
  457.         foreach (Match arrow in arrows)
  458.         {
  459.             if (!string.IsNullOrEmpty(arrow.Groups[1].Value))
  460.             {
  461.                 largeArrowCount++;
  462.             }
  463.             else if (!string.IsNullOrEmpty(arrow.Groups[2].Value))
  464.             {
  465.                 mediumArrowCount++;
  466.             }
  467.             else if (!string.IsNullOrEmpty(arrow.Groups[3].Value))
  468.             {
  469.                 smallArrowCount++;
  470.             }
  471.         }
  472.  
  473.         string numberAsString = string.Format(
  474.             "{0}{1}{2}",
  475.             smallArrowCount,
  476.             mediumArrowCount,
  477.             largeArrowCount);
  478.  
  479.         short number = short.Parse(numberAsString);
  480.  
  481.         string binaryNum = Convert.ToString(number, 2);
  482.  
  483.         string reversedBinaryNum = new string(binaryNum.Reverse().ToArray());
  484.  
  485.         string resultAsString = binaryNum + reversedBinaryNum;
  486.  
  487.         short result = Convert.ToInt16(resultAsString, 2);
  488.  
  489.         Console.WriteLine(result);
  490.     }
  491. }
  492.  
  493. 15 zadacha   Upper Words  ////////////////////////////////////////////////////////////////////////////
  494.  
  495. using System;
  496. using System.Text.RegularExpressions;
  497. using System.Collections.Generic;
  498. using System.Linq;
  499. using System.Text;
  500. using System.Threading.Tasks;
  501.  
  502. namespace UppercaseWords
  503. {
  504.     class UppercaseWords
  505.     {
  506.         static void Main(string[] args)
  507.         {
  508.             string input = Console.ReadLine();
  509.  
  510.             string pattern = @"([a-zA-Z]+)|(\s|\W+|(\d+))*";
  511.             Regex word = new Regex(pattern);
  512.  
  513.             while (input != "END")
  514.             {
  515.                 MatchCollection words = word.Matches(input);
  516.                 StringBuilder result = new StringBuilder();
  517.  
  518.                 foreach (var item in words)
  519.                 {
  520.                     string forAppend = item.ToString();
  521.                     if (IsUpper(forAppend))
  522.                     {
  523.                         if (IsPalindrome(forAppend))
  524.                         {
  525.                             forAppend = DoubleEachLetters(forAppend);
  526.                         }
  527.                         else
  528.                         {
  529.                             forAppend = Reverse(forAppend);
  530.                         }
  531.                     }
  532.                     result.Append(forAppend);
  533.                 }
  534.                 Console.WriteLine(EscapeXmlString(result.ToString()));
  535.                 input = Console.ReadLine();
  536.             }
  537.         }
  538.         public static bool IsUpper(string input)
  539.         {
  540.             for (int i = 0; i < input.Length; i++)
  541.             {
  542.                 if (!Char.IsUpper(input[i]))
  543.                     return false;
  544.             }
  545.             return true;
  546.         }
  547.  
  548.         public static string EscapeXmlString(string value)
  549.         {
  550.             return System.Security.SecurityElement.Escape(value);
  551.         }
  552.  
  553.         public static string Reverse(string s)
  554.         {
  555.             char[] charArray = s.ToCharArray();
  556.             Array.Reverse(charArray);
  557.             return new string(charArray);
  558.         }
  559.  
  560.         private static string DoubleEachLetters(string forAppend)
  561.         {
  562.             StringBuilder doubledString = new StringBuilder();
  563.             for (int i = 0; i < forAppend.Length; i++)
  564.             {
  565.                 char letter = forAppend[i];
  566.                 doubledString.Append(letter);
  567.                 doubledString.Append(letter);
  568.             }
  569.             return doubledString.ToString();
  570.         }
  571.  
  572.         public static bool IsPalindrome(string value)
  573.         {
  574.             string[] a = value.Select(c => c.ToString()).ToArray();
  575.  
  576.             bool isSymmetric = true;
  577.             for (int i = 0; i <a.Length / 2; i++)// na purvoto zavurtane na cikula size = 5 i za tova 5 - 0 - 1 = 4 index koito mi e 1 v slu4aq
  578.             {                                         // pri vtoroto zavurtane mi vzima 1 i 3 element i taka natatuk
  579.                 if (a[i] != a[a.Length - i - 1])
  580.                 {
  581.                     isSymmetric = false;
  582.                     break;
  583.                 }
  584.             }
  585.             return isSymmetric;
  586.             }
  587.         }
  588.     }
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596. 18 zadacha Pin Validation  ///////////////////////////////////////////////////////////////
  597.  
  598.  
  599. using System;
  600. using System.Collections.Generic;
  601. using System.Linq;
  602. using System.Text;
  603. using System.Threading.Tasks;
  604.  
  605. namespace PinValidation
  606. {
  607.     class PinValidation
  608.     {
  609.         static void Main(string[] args)
  610.         {
  611.             string name = Console.ReadLine();
  612.             string gender = Console.ReadLine();
  613.             string egn = Console.ReadLine();
  614.  
  615.  
  616.             int[] egnDigits = new int[egn.Length];
  617.             bool isEgnMadeOfDigits = true;
  618.             for (int i = 0; i < egn.Length; i++)
  619.             {
  620.                 int nextDigit;
  621.                 isEgnMadeOfDigits = int.TryParse(egn[i].ToString(), out nextDigit);
  622.                 if (!isEgnMadeOfDigits)
  623.                 {
  624.                     break;
  625.                 }
  626.                 egnDigits[i] = nextDigit;
  627.             }
  628.  
  629.             if (!isEgnMadeOfDigits)
  630.             {
  631.                 Console.WriteLine("<h2>Incorrect data</h2>");
  632.                return;
  633.             }
  634.  
  635.             bool isLegthCorrect = egn.Length == 10;
  636.             bool isGenderCorrect = (gender == "male" && egnDigits[8] % 2 == 0) || (gender == "female" && egnDigits[8] % 2 == 1);
  637.             int year = int.Parse(egn.Substring(0, 2));
  638.             int month = int.Parse(egn.Substring(2, 2));
  639.             int day = int.Parse(egn.Substring(4, 2));
  640.  
  641.             if (month > 20 && month <= 32)
  642.             {
  643.                 month -= 20;
  644.                 year = 1800 + year;
  645.             }
  646.             else if (month > 40 && month <= 52)
  647.             {
  648.                 month -= 40;
  649.                 year = 2000 + year;
  650.             }
  651.             else
  652.             {
  653.                 year = 1900 + year;
  654.             }
  655.             DateTime date = new DateTime();
  656.             bool isDateCorrect = DateTime.TryParse(String.Format("{0}.{1}.{2}", month, day, year), out date);
  657.  
  658.             int checkSum = (egnDigits[0] * 2 + egnDigits[1] * 4 + egnDigits[2] * 8 + egnDigits[3] * 5 + egnDigits[4] * 10 + egnDigits[5] * 9
  659.                 + egnDigits[6] * 7 + egnDigits[7] * 3 + egnDigits[8] * 6) % 11;
  660.             if (checkSum == 10)
  661.             {
  662.                 checkSum = 0;
  663.             }
  664.  
  665.             bool isCheckSumCorrect = (checkSum == egnDigits[9]);
  666.  
  667.             if (isLegthCorrect && isGenderCorrect && isDateCorrect && isCheckSumCorrect)
  668.             {
  669.                 Console.WriteLine("{{\"name\":\"{0}\",\"gender\":\"{1}\",\"pin\":\"{2}\"}}", name, gender, egn);
  670.             }
  671.             else
  672.             {
  673.                 Console.WriteLine("<h2>Incorrect data</h2>");
  674.             }
  675.         }
  676.     }
  677. }
Advertisement
Add Comment
Please, Sign In to add comment