Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace DrunkFibonacci
{
Expand All @@ -11,8 +12,7 @@ internal static class DrunkFibonacci
/// <param name="len">Длина массива</param>
public static int[] CreateIntArray(int len)
{
// на создание массивов заданной длины
throw new NotImplementedException();
return new int[len];
}

/// <summary>
Expand All @@ -23,8 +23,10 @@ public static int[] CreateIntArray(int len)
/// <param name="step">Шаг прогрессии.</param>
public static void FillIntArray(int[] arr, int seed, int step)
{
// на задание значений массива
throw new NotImplementedException();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = seed + step * i;
}
}

/// <summary>
Expand All @@ -34,7 +36,7 @@ public static void FillIntArray(int[] arr, int seed, int step)
public static int[] GetFirstFiveFibonacci()
{
// на создание массива с инициализацией
throw new NotImplementedException();
return new int[] {1, 1, 2, 3, 5};
}

/// <summary>
Expand All @@ -49,7 +51,12 @@ его следует инициализировать одной и той же

Задача на ленивую генерацию последовательностей.
*/
throw new NotImplementedException();
var seed = 123;
var rand = new Random(seed);
while (true)
{
yield return rand.Next();
}
}

/// <summary>
Expand All @@ -67,7 +74,34 @@ Вероятность считается так. На каждом i-ом эт
из последовательности GetDeterministicRandomSequence и проверяешь, есть ли у числа Y единичные биты числа 42.
При вычислении сложения переполнение типа разрешено и всячески поощряется.
*/
throw new NotImplementedException();
var last_two = new int[] {1, 1};
var current_el = 3;
var random_sequence = GetDeterministicRandomSequence();
yield return 1;
yield return 1;
foreach (var random_element in random_sequence)
{
if ((current_el - 4) % 6 == 0)
{
last_two[0] = last_two[1];
last_two[1] = 300;
}
else
{
var tmp = last_two[0] + last_two[1];
last_two[0] = last_two[1];
last_two[1] = tmp;
}

last_two[1] = (last_two[1] & (~(random_element & 42)));

if (current_el % 6 != 0)
{
yield return last_two[1];
}

current_el++;
}
}

/// <summary>
Expand All @@ -78,7 +112,7 @@ При вычислении сложения переполнение типа
public static int GetMaxOnRange(int from, int cnt)
{
// научишься пропускать и брать фиксированную часть последовательности, агрегировать. Максимум есть среди готовых функций агрегации.
throw new NotImplementedException();
return GetDrunkFibonacci().Skip(from).Take(cnt).Max();
}

/// <summary>
Expand All @@ -88,7 +122,7 @@ public static int GetMaxOnRange(int from, int cnt)
public static List<int> GetNextNegativeRange(int from = 1)
{
// научишься пропускать и брать по условию, превращать в список (см. ToList).
throw new NotImplementedException();
return GetDrunkFibonacci().Skip(from).SkipWhile(x => x >= 0).TakeWhile(x => x < 0).ToList();
}

/// <summary>
Expand All @@ -97,7 +131,7 @@ public static List<int> GetNextNegativeRange(int from = 1)
public static IEnumerable<int> GetXoredWithLaggedItself()
{
// узнаешь о существовании функции Zip.
throw new NotImplementedException();
return GetDrunkFibonacci().Zip(GetDrunkFibonacci().Skip(42), (first, second) => first ^ second);
}

/// <summary>
Expand All @@ -106,7 +140,21 @@ public static IEnumerable<int> GetXoredWithLaggedItself()
public static IEnumerable<int[]> GetInChunks()
{
// ни чему особо не научишься, просто интересная задачка :)
throw new NotImplementedException();
var chunk = new int[16];
int count = 0;
foreach (var element in GetDrunkFibonacci())
{
if (count < 16)
{
chunk[count] = element;
count++;
}
else
{
yield return chunk;
count = 0;
}
}
}

/// <summary>
Expand All @@ -122,7 +170,7 @@ которая сглаживает (flatten) последовательност
Вообще говоря, SelectMany умеет много чего и мегаполезна.
Она в какой-то степени эквивалентна оператору `bind` над монадами (в данном случае над монадами последовательностей).
*/
throw new NotImplementedException();
return GetInChunks().SelectMany(chunk => chunk.OrderBy(x => x).Take(3));
}

/// <summary>
Expand Down