Skip to content

Instantly share code, notes, and snippets.

@luiseduardohd
Created July 31, 2020 19:14
Show Gist options
  • Save luiseduardohd/e912edf222bfc86f4b086c91ac5bd008 to your computer and use it in GitHub Desktop.
Save luiseduardohd/e912edf222bfc86f4b086c91ac5bd008 to your computer and use it in GitHub Desktop.
FuturisTech interview problem
/*
* Write a function to read digits in the string. and then report the frecuency of each digit.
* Then show digits sorted by frecuency, and their frecuency.
* */
using System;
using System.Collections.Generic;
using System.Linq;
namespace FuturisTech
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start");
var s = "83798475678346512830974356837456";
var digitsChar = s.ToCharArray();
var frecuencies = new int[] { 0,0,0,0,0,0,0,0,0,0 };
foreach( var digitChar in digitsChar)
{
var digit = Int32.Parse("" + digitChar);
frecuencies[digit] = frecuencies[digit] + 1;
}
var cont = 0;
foreach (var frecuency in frecuencies)
{
Console.WriteLine( "Number "+ cont ++ + " has frecuency: "+ frecuency);
}
var dict = new Dictionary<int, int>();
cont = 0;
foreach (var frecuency in frecuencies)
{
dict.Add(cont++,frecuency);
}
//dict.ToList();
var frecuencyTuples = dict.ToList(); // frecuencies.ToList<int>();
//frecuencyTuples.Sort(new Comparison<KeyValuePair<int, int>>(( a,b ) => { return b.Value - a.Value; }) );
frecuencyTuples.Sort((a, b) => { return b.Value - a.Value; });
//var sortedByFrecuency = dict.
foreach ( var frecuencyTuple in frecuencyTuples)
{
Console.WriteLine("Number: "+ frecuencyTuple.Key+" Frecuency: " + frecuencyTuple.Value);
}
Console.WriteLine("End");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment