Advertisement
gabi11

Functional Programming - 09. List Of Predicates

May 29th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int endDigit = int.Parse(Console.ReadLine());
  14.             var dividers = Console.ReadLine()
  15.                 .Split()
  16.                 .Select(int.Parse)
  17.                 .ToList();
  18.  
  19.             List<Predicate<int>> predicates = new List<Predicate<int>>();
  20.  
  21.             dividers.ForEach(n => predicates.Add(x => x % n == 0));
  22.             List<int> result = new List<int>();
  23.  
  24.             for (int i = 1; i <= endDigit; i++)
  25.             {
  26.                 bool isDivisible = true;
  27.  
  28.                 foreach (var predicate in predicates)
  29.                 {
  30.                     if (!predicate(i))
  31.                     {
  32.                         isDivisible = false;
  33.                         break;
  34.                     }
  35.                 }
  36.  
  37.                 if (isDivisible)
  38.                 {
  39.                     result.Add(i);
  40.                 }
  41.             }
  42.  
  43.             Console.WriteLine(String.Join(" ", result));
  44.         }
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement