Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
39
Task/Entropy/C-sharp/entropy-1.cs
Normal file
39
Task/Entropy/C-sharp/entropy-1.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace Entropy
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static double logtwo(double num)
|
||||
{
|
||||
return Math.Log(num)/Math.Log(2);
|
||||
}
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
label1:
|
||||
string input = Console.ReadLine();
|
||||
double infoC=0;
|
||||
Dictionary<char,double> table = new Dictionary<char, double>();
|
||||
|
||||
|
||||
foreach (char c in input)
|
||||
{
|
||||
if (table.ContainsKey(c))
|
||||
table[c]++;
|
||||
else
|
||||
table.Add(c,1);
|
||||
|
||||
}
|
||||
double freq;
|
||||
foreach (KeyValuePair<char,double> letter in table)
|
||||
{
|
||||
freq=letter.Value/input.Length;
|
||||
infoC+=freq*logtwo(freq);
|
||||
}
|
||||
infoC*=-1;
|
||||
Console.WriteLine("The Entropy of {0} is {1}",input,infoC);
|
||||
goto label1;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Task/Entropy/C-sharp/entropy-2.cs
Normal file
43
Task/Entropy/C-sharp/entropy-2.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
namespace Entropy
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static double logtwo(double num)
|
||||
{
|
||||
return Math.Log(num)/Math.Log(2);
|
||||
}
|
||||
static double Contain(string x,char k)
|
||||
{
|
||||
double count=0;
|
||||
foreach (char Y in x)
|
||||
{
|
||||
if(Y.Equals(k))
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
label1:
|
||||
string input = Console.ReadLine();
|
||||
double infoC=0;
|
||||
double freq;
|
||||
string k="";
|
||||
foreach (char c1 in input)
|
||||
{
|
||||
if (!(k.Contains(c1.ToString())))
|
||||
k+=c1;
|
||||
}
|
||||
foreach (char c in k)
|
||||
{
|
||||
freq=Contain(input,c)/(double)input.Length;
|
||||
infoC+=freq*logtwo(freq);
|
||||
}
|
||||
infoC/=-1;
|
||||
Console.WriteLine("The Entropy of {0} is {1}",input,infoC);
|
||||
goto label1;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue