This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
88 lines (68 loc) · 1.99 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
public static class Extensions
{
const uint INT_SIZE = 32;
private static System.Random RandomEngine = new System.Random();
public static List<int> GetBits(this int n)
{
var result = new List<int>();
for (uint i = 0; i < INT_SIZE; i++)
{
result.Add(n & 1);
n = n >> 1;
}
return result;
}
public static int GetParity(this int n)
{
var ones = 0;
for (uint i = 0; i < INT_SIZE; i++)
{
ones += n & 1;
n = n >> 1;
}
return ones % 2;
}
public static List<char> GetSample(this char[] symbols, double p = 0.5)
{
var result = new List<char>();
var size = RandomEngine.Next(100);
for (int i = 0; i < size; i++)
result.Add(RandomEngine.NextDouble() > p ? symbols[0] : symbols[1]);
return result;
}
public static List<List<char>> GetSamples(this char[] symbols, int n, double p = 0.5)
{
var result = new List<List<char>>();
for (int i = 0; i < n; i++)
result.Add(symbols.GetSample(p));
return result;
}
public static char[] RandTernary(int size){
var result = new char[size];
var alphabet = new char[] {'0', '1', '2'};
for (int i = 0; i < size; i++)
{
result[i] = alphabet[RandomEngine.Next() % 3];
}
return result;
}
public static double TernaryToInt(this char[] a)
{
var result = 0.0;
for (int i = 0; i < a.Length; i++)
{
result += int.Parse(a[i].ToString()) * Math.Pow(3, i);
}
return result;
}
public static List<string> Pack(this (char[] a, char[] b) vals){
var result = new List<string>();
for (int i = 0; i < vals.a.Length; i++)
{
result.Add(new string(new[] {vals.a[i], vals.b[i]}));
}
return result;
}
}