maximilianorios
BAN USERVery easy program. Written in c#
private static void ReverseArray() {
// in C#, given a list of numbers [4,8,20,11,12,18,50,10,11] and divided in sections of 3
// Reverse each section without using another array
int[] array = { 4,8,20,11,12,18,50,10,11 };
Console.WriteLine("Original array");
for(int i=0;i<array.Length;i++) {
Console.Write("{0} ", array[i]);
}
// Traverse the array stepping by 3
for (int i=0; i<array.Length; i+=3) {
// Just I need to swap first and third
int temp = array[i];
array[i] = array[i+2];
array[i+2] = temp;
}
Console.WriteLine("Reversed");
for(int i=0;i<array.Length;i++) {
Console.Write("{0} ", array[i]);
}
}
Without even checking the rest of the answers mine look pretty similar to one of them. This is the code.
public static void PokerHand() {
// Given an array of five integers that represents a poker hand e.g. {2,2,2,3,3}
// return the value of the hand, valid values are only "pair", "two pair",
// "three of a kind", "full house", "foud of a kind"
int[] cards = { 1, 1, 1, 9, 1 };
Dictionary<int, int> hashTable = new Dictionary<int, int>();
// Sort cards to start
Array.Sort(cards);
for(int i=0;i<cards.Length;i++) {
if(hashTable.ContainsKey(cards[i])) {
hashTable[cards[i]]++;
}
else {
hashTable.Add(cards[i], 1);
}
}
// I could obtain a dictionary of ocurrences. Get the values and remove the 1's
var validCards = hashTable.Values.Where(x => x > 1).ToArray();
Array.Sort(validCards);
// Valid hands are:
// 2 | 2,2 | 3 | 3,2 || 4
string hand_result = "";
if(validCards.Length == 0) {
hand_result = "Nothing!";
}
else if(validCards.Length == 1) {
switch (validCards[0])
{
case 2:
hand_result = "Pair";
break;
case 3:
hand_result = "Three of a kind";
break;
case 4:
hand_result = "Four of a kind";
break;
case 5:
hand_result = "Poker";
break;
}
}
else {
if(validCards[1] == 2) {
hand_result = "Two pairs";
}
else {
hand_result = "Full house";
}
}
Console.WriteLine("The hand is {0}", hand_result);
}
private static void RearrangeArraysEasy() {
List<int> array_integers = new List<int> { 10, 1, 29, 8, 10, 45, 56, 100, 98, 34, 11, 9 };
array_integers.Sort();
List<int> sorted = new List<int>();
int middle = array_integers.Count / 2;
for(int i = 0; i< middle; i++) {
sorted.Add(array_integers[array_integers.Count-1] - i);
sorted.Add(array_integers[i]);
}
if(array_integers.Count % 2 == 1) sorted.Add(array_integers[middle]);
foreach (var i in sorted) {
Console.Write("{0} ", i);
}
}
Internet of the things at a higher level everywhere
- maximilianorios December 22, 2015