AddisonRodomista
BAN USERHeres my attempt, not sure if its completely sound. Any advice would be greatly appreciated :)
using System;
namespace AliveProblem
{
class MainClass
{
public static int[,] island = new int[4,4];
public static void Main (string[] args)
{
Console.WriteLine(probabilityofalive(1, 1, 5));
}
public static double probabilityofalive(int x, int y, int n) {
//Break case
if (n == 0) return 1;
//corner case
//Top left
if (x == 0 && y == 0) {
return 0.5+0.25*probabilityofalive(x+1, y, n-1)*0.25*probabilityofalive(x, y+1, n-1);
}
//Bottom left
else if (x == 0 && y == island.Length-1) {
return 0.5+0.25*probabilityofalive(x+1, y, n-1)*0.25*probabilityofalive(x, y+1, n-1);
}
//Top right
else if (x == island.Length-1 && y == 0) {
return 0.5+0.25*probabilityofalive(x-1, y, n-1)*0.25*probabilityofalive(x, y-1, n-1);
}
//Bottom right
else if (x == island.Length-1 && y == island.Length-1) {
return 0.5+0.25*probabilityofalive(x-1, y, n-1)*0.25*probabilityofalive(x, y+1, n-1);
}
//Left side
else if (x == 0 && (y != 0 && y!= island.Length-1) ) {
return 0.25+0.25*probabilityofalive(x, y+1, n-1)*0.25*probabilityofalive(x, y-1, n-1)*0.25*probabilityofalive(x+1, y, n-1);
}
//Right side
else if (x == island.Length-1 && (y!=0 && y!= island.Length-1)) {
return 0.25+0.25*probabilityofalive(x, y+1, n-1)*0.25*probabilityofalive(x, y-1, n-1)*0.25*probabilityofalive(x-1, y, n-1);
}
else if (y == 0 && ( x!= 0 && x!=island.Length-1)) {
return 0.25+0.25*probabilityofalive(x-1, y, n-1)*0.25*probabilityofalive(x+1, y, n-1)*0.25*probabilityofalive(x, y+1, n-1);
}
else if (y == island.Length-1 && (x != 0 && x!= island.Length-1)) {
return 0.25+0.25*probabilityofalive(x-1, y, n-1)*0.25*probabilityofalive(x+1, y, n-1)*0.25*probabilityofalive(x, y-1, n-1);
}
else {
return 0.25*probabilityofalive(x-1, y, n-1)*0.25*probabilityofalive(x+1, y, n-1)*0.25*probabilityofalive(x, y+1, n-1)*0.25*probabilityofalive(x, y-1, n-1);
}
Heres a full solution in C#:
1. Sort the array in incrementing order
2. Check each value of the array (starting at 0), and determine if it withholds the stated rules (if even index, should be less than right, if odd should be greater than)
3. If not, continue moving right along indices and find element that withholds rules
4. Continue till exhausted all indices
using System;
namespace SortNegativeArray
{
class MainClass
{
public static int[] nums = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
public static void Main (string[] args)
{
Array.Sort(nums); //First sort the array in incrementing order
PrintArray();
SortNums(0);
PrintArray();
}
public static void SortNums(int index) {
Console.WriteLine("Pass " + index);
if (index == nums.Length) return;
//If its an even number, handle appropraitly
if (index == 0 || (index%2 == 0)) {
//Check less than right → We can assume this since we made it this far
for(int i = index+1; i < nums.Length; i++) {
//If I’m less then the next one, we’re good, break the loop
if (nums[index] < nums[i] && i == index+1) {
break;
}
//Otherwise check if its less than the number if so replace whats already there
else if (nums[index] > nums[i]) {
int temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
break;
}
}
}
//Likewise for odd
else {
//Check greater than right → We can assume this since we made it this far
for(int i = index+1; i < nums.Length; i++) {
if (nums[index] > nums[i] && i == index+1) break;
else if (nums[index] < nums[i]) {
int temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
}
}
}
SortNums(++index);
}
public static void PrintArray() {
Console.WriteLine("--Printing Array--");
foreach(int num in nums) Console.Write(num + " ");
Console.WriteLine("----------------");
}
}
}
- AddisonRodomista February 25, 2014My solution:
1. Create an Item object for every value containing a min value, max value, and referenced lists
2. For every Item object, compare every list.
- If the list is already referenced, return
- If the value within the range of min & max, add this list as reference and return
- If the value is greater than a temporary max for this item, update temporary max and continue
- Update maxValue for item and reference this list, continue
3. For every item, call GetRange() and compare with temporary range, if its smaller, make it new temp
Here's my code in c#:
using System;
using System.Collections.Generic;
namespace ListProblem
{
class MainClass
{
public static int globalNum = 0;
public static List<List<int>> allLists = new List<List<int>>();
public static void Main (string[] args)
{
//Instantiate all the lists that I will use as an example
List<int> list1 = new List<int>(new int[]{4, 10, 15, 24, 26});
List<int> list2 = new List<int>(new int[]{0, 9, 12, 20});
List<int> list3 = new List<int>(new int[]{5, 18, 22, 30});
allLists = new List<List<int>>();
allLists.Add(list1);
allLists.Add(list2);
allLists.Add(list3);
//Track all the lists that will be used to test ranges
List<Item> allItems = new List<Item>();
//Fill these up, assigning every single one to have both the min and max be an element as the starting one, the moment its been assigned, break the list
foreach(List<int> list in allLists) {
foreach(int num in list) {
allItems.Add(new Item(num, num, list));
}
}
//Now run the algorithm to adjust is max value, every time adding it to the list
foreach(Item item in allItems) {
foreach(List<int> list in allLists) {
item.CompareWithList(list);
}
}
//Now determine which item actually has the smallest range
Item tempMin;
for(int i = 0; i < allItems.Count; i++) {
if (!allItems[i].HasAllLists()) continue;
if (i == 0) tempMin = allItems[i];
else {
if (allItems[i].GetRange() < tempMin.GetRange()) {
tempMin = allItems[i];
}
}
}
Console.WriteLine("--------COMPLETED APPLICATION---------");
tempMin.PrintInfo();
}
public class Item {
public int itemNum;
public int minVal;
public int maxVal;
List<List<int>> usedLists;
public Item(int min, int max, List<int> list ) {
itemNum = ++globalNum;
minVal = min;
maxVal = max;
usedLists = new List<List<int>>();
usedLists.Add(list);
}
public int GetRange() {
return (maxVal - minVal);
}
public void PrintInfo() {
Console.WriteLine("---------Print info----------");
Console.WriteLine("Item " + itemNum);
Console.WriteLine("Min: " + minVal);
Console.WriteLine("Max: " + maxVal);
}
public bool HasAllLists() {
return usedLists.Count == allLists.Count;
}
public void CompareWithList(List<int> list) {
//Run through a passed list, comparing the values range to the previous values range
int tempMax = maxVal;
foreach(int num in list) {
if (usedLists.Contains(list)) return; //Already found one in this list, don't bother checking
if (num < minVal) continue; //Smaller than min, don't bother
if (num <= maxVal && num >= minVal) { //Within range, add it to the list
usedLists.Add(list);
continue;
}
if (num >= minVal && num <= tempMax) tempMax = num; //If its smaller than a previous one
if (num > tempMax && tempMax == maxVal) tempMax = num;
}
if (tempMax > maxVal) { //Update the max value to the new one
maxVal = tempMax;
usedLists.Add(list);
}
}
}
}
}
Heres solution in c#:
- AddisonRodomista March 05, 2014