fork(2) download
  1. using System;
  2. using System.Globalization;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Test
  7. {
  8. private static IEnumerable<IList<doc>> SplitDocumentList(IEnumerable<doc> allDocuments, int maxMB)
  9. {
  10. var lists = new List<IList<doc>>();
  11. var list = new List<doc>();
  12. foreach (doc document in allDocuments)
  13. {
  14. int totalMB = list.Sum(d => d.sizemb) + document.sizemb;
  15. if (totalMB > maxMB)
  16. {
  17. lists.Add(list);
  18. list = new List<doc>();
  19.  
  20. }
  21. list.Add(document);
  22. }
  23. if (list.Count > 0)
  24. lists.Add(list);
  25. return lists;
  26. }
  27.  
  28. public static void Main()
  29. {
  30. var list = new List<doc>()
  31. {
  32. new doc { file = "dok1", sizemb = 5 },
  33. new doc { file = "dok2", sizemb = 5 },
  34. new doc { file = "dok3", sizemb = 5 },
  35. new doc { file = "dok4", sizemb = 4 },
  36. };
  37.  
  38. int maxTotalFileSize = 9;
  39.  
  40. IEnumerable<IList<doc>> splittedtDocs = SplitDocumentList(list, maxTotalFileSize);
  41.  
  42. foreach(var l in splittedtDocs)
  43. Console.WriteLine(string.Join(",",l.Select(d=>d.file).ToArray()));
  44. }
  45.  
  46. public class doc
  47. {
  48. public string file;
  49. public int sizemb;
  50. }
  51. }
Success #stdin #stdout 0.03s 33888KB
stdin
Standard input is empty
stdout
dok1
dok2
dok3,dok4