Skip to content

Instantly share code, notes, and snippets.

@alien3d
Created November 21, 2017 08:42
Show Gist options
  • Save alien3d/f494e542771b044c306d2b16cb988acb to your computer and use it in GitHub Desktop.
Save alien3d/f494e542771b044c306d2b16cb988acb to your computer and use it in GitHub Desktop.
using System;
/**
* Dummy Games..
* Rulez:
* 1) You SHALL NOT declare any additional using directives other than "using System".
* Yes..
* 2) You are allowed to using the string class related functions, like: insert(),
* appened, trim()and etc for the implementation
* / not sure what is insert,append anyway.. i prefer base function like math,system,string,string builder
* 3) You SHALL NOT invoke the other .NET specific libraries to perform the conversion except rule 2.
* // Use system only
* 4) Your code style is graded, hence it is encouraged to structure the code professionally
* and equipped with relevant comments.
* Yeah dummy comment is availble
* 5) Usage of helper functions are encouraged.
* Not sure what is concept of helper .. I know javasript people use that word only
*
* All
*/
namespace dummy
{
class Program
{
static void Main(string[] args)
{
int x = 254;
int y = 16;
int z = x / y;
int a = x % y;
// int wouldn't work because we need double/float
Console.WriteLine("Divide result : "+z+" Modulo result : "+a);
double zz = Convert.ToDouble(x) / Convert.ToDouble(y);
double aa = (double)x % (double)y;
Console.WriteLine("Divide result : " + zz + " Modulo result : " + aa);
// but still we don't know how to convert divide result to hex.
// So we create a method which return .
Console.WriteLine("Divide result : " + GetHexString((zz)) + " Modulo result : " + GetHexString(aa));
// something still missing why does no value;
// So we split it yeah.
string[] s = zz.ToString().Split('.');
Console.WriteLine("S array first : " + s[0] + " S array second " + s[1]);
double zzz = Convert.ToDouble(s[0]);
Console.WriteLine("Divide result : " + GetHexString((zzz)) + " Modulo result : " + GetHexString(aa));
// so finally the result
Console.WriteLine("254 base 16 :[" + GetHexString((zzz)) + GetHexString((aa))+"]");
// but but this is to simple.. but on how large number
// seem so wrong.. yeah because we divided 16 one time only..
// So loop till drop
int remainderHex;
string resultHex = string.Empty;
int zHex = x;
while (zHex > 0)
{
remainderHex = zHex % 16; // module.. Can be divided till last drop i mean even number
zHex /= 16; // lazy zHex = Zhex /16
resultHex = GetHexString(remainderHex) + resultHex;
}
Console.WriteLine("Hex: {0}", resultHex);
int remainderOc;
string resultOc = string.Empty;
int xOc = x;
while (xOc > 0)
{
remainderOc = xOc % 8;
xOc /= 8;
resultOc = remainderOc.ToString() + resultOc;
}
Console.WriteLine("Oc: {0}", resultOc);
int remainderBin;
string resultBin = string.Empty;
int xBin =x;
while (xBin > 0)
{
remainderBin = xBin % 2;
xBin /= 2;
resultBin = remainderBin.ToString() + resultBin;
}
Console.WriteLine("Binary: {0}", resultBin);
// So now we create a method contain the interview..
// what if return an empty string if value is negative.
Console.Write(" Answer MyIntToString(254, 16) return \"empty string\" : " + MyIntToString(-9, 16) + "\n");
Console.Write(" Answer MyIntToString(254, 16) return \"FE\" : " + MyIntToString(254, 16)+"\n");
Console.Write(" Answer MyIntToString(254, 8) return \"376\" : " + MyIntToString(254, 8) + "\n");
Console.Write(" Answer MyIntToString(254, 2) return \"11111110\" : " + MyIntToString(254, 2) + "\n");
Console.ReadLine();
// Opp freaking long day
}
public static string MyIntToString(int value, int numbase)
{
string interview = string.Empty;
switch (numbase)
{
case 16:
int remainderHex;
string resultHex = string.Empty;
int zHex = value;
while (zHex > 0)
{
remainderHex = zHex % 16; // module.. Can be divided till last drop i mean even number
zHex /= 16; // lazy zHex = Zhex /16
resultHex = GetHexString(remainderHex) + resultHex;
}
interview = resultHex;
break;
case 8:
int remainderOc;
string resultOc = string.Empty;
int xOc = value;
while (xOc > 0)
{
remainderOc = xOc % 8;
xOc /= 8;
resultOc = remainderOc.ToString() + resultOc;
}
interview = resultOc;
break;
case 2:
int remainderBin;
string resultBin = string.Empty;
int xBin = value;
while (xBin > 0)
{
remainderBin = xBin % 2;
xBin /= 2;
resultBin = remainderBin.ToString() + resultBin;
}
interview = resultBin;
break;
default:
interview = "Not in interview request don't make a joke yeah";
break;
}
return interview;
}
private void GetHexStringWrite(double zz)
{
string returnValue = string.Empty;
var value = Convert.ToInt32(zz);
switch (value)
{
case 1:
returnValue = (1).ToString();
break;
case 2:
returnValue = (2).ToString();
break;
case 3:
returnValue = (3).ToString();
break;
case 4:
returnValue = (4).ToString();
break;
case 5:
returnValue = (5).ToString();
break;
case 6:
returnValue = (6).ToString();
break;
case 7:
returnValue = (7).ToString();
break;
case 8:
returnValue = (8).ToString();
break;
case 9:
returnValue = (9).ToString();
break;
case 10:
returnValue = ("A").ToString();
break;
case 11:
returnValue = ("B").ToString();
break;
case 12:
returnValue = ("C").ToString();
break;
case 13:
returnValue = ("D").ToString();
break;
case 14:
returnValue = ("E").ToString();
break;
case 15:
returnValue = ("F").ToString();
break;
}
Console.Write(" testing " + returnValue);
}
private static string GetHexString(double zz)
{
string returnValue = string.Empty;
var value = Convert.ToInt32(zz);
switch (value)
{
case 1:
returnValue = (1).ToString();
break;
case 2:
returnValue = (2).ToString();
break;
case 3:
returnValue = (3).ToString();
break;
case 4:
returnValue = (4).ToString();
break;
case 5:
returnValue = (5).ToString();
break;
case 6:
returnValue = (6).ToString();
break;
case 7:
returnValue = (7).ToString();
break;
case 8:
returnValue = (8).ToString();
break;
case 9:
returnValue = (9).ToString();
break;
case 10:
returnValue = ("A").ToString();
break;
case 11:
returnValue = ("B").ToString();
break;
case 12:
returnValue = ("C").ToString();
break;
case 13:
returnValue = ("D").ToString();
break;
case 14:
returnValue = ("E").ToString();
break;
case 15:
returnValue = ("F").ToString();
break;
}
return returnValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment