Skip to content

Instantly share code, notes, and snippets.

@Atrejoe
Forked from dyln/Program.cs
Created November 1, 2017 19:00
Show Gist options
  • Save Atrejoe/a379f8ba82c0f230a41587152f9b4c8c to your computer and use it in GitHub Desktop.
Save Atrejoe/a379f8ba82c0f230a41587152f9b4c8c to your computer and use it in GitHub Desktop.
my solution to the question asked in the interview
/********************* Coded by: Caner 'dyln' Eren **************************/
using System;
using System.Collections;
using System.Collections.Generic;
namespace hwapp{
class Program{
static void Main(string[] args){
Console.WriteLine("!!!!!!!!!");
int [][] arr = takeInput();
Console.WriteLine("!!!!!!!!!");
//main loop
for (int i = 0; i < arr.Length; i++){
double count=0.00;
int [] temp=arr[i];
List<int[]> list = new List<int[]>();
permutate(temp,temp.Length,0,list);
for (int j = 0; j < list.ToArray().Length; j++){
count = count + calculateV(list.ToArray()[j]);
}
Console.WriteLine( string.Format("{0:0.00}", count / list.ToArray().Length) );
}
}
//this method updates the initially empty list recursively with all possible parmutations of array s
//initially: n is the length of s, i is 0
public static void permutate(int[] s, int n, int i, List<int[]>list){
if (i >= n - 1){
int [] temp = new int[s.Length];
for(int x=0;x<s.Length;x++){
temp[x]=s[x];
}
list.Add(temp);
temp=null;
}
else{
permutate(s, n, i + 1,list);
for (int j = i + 1; j < n; j++)
{
int tmp = s[i];
s[i] = s[j];
s[j] = tmp;
permutate(s, n, i + 1,list);
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
//this method calculates V for one array
//{1,2,3} --> 6
//{3,2,1} --> 3
public static int calculateV(int [] arr){
int count=0;
for (int i = 0; i < arr.Length; i++){
if(isCollapsed(arr,i)){
count = collapsedStep(arr,i)+count ;
}
else{
count=(count+i)+1;
}
}
return count;
}
//working version of the method that I tried to explain in the interview
//takes an array and index of element, compare it backwards to other elements
//returns boolean
public static Boolean isCollapsed(int [] arr, int index){
int temp = arr[index];
for (int i = index; i >=1; i--){
if(arr[i-1]>=temp){
return true;
}
}
return false;
}
//just like isCollapsed method
//calculates the distance if collapsed
//returns step count
public static int collapsedStep(int [] arr, int index){
int temp = 0;
for (int i = index; i >= 1; i--){
temp++;
if (arr[i-1] >= arr[index]){
return temp;
}
}
return temp;
}
//This method takes all the console input and returns as a jagged array
public static int [][] takeInput(){
int T = Convert.ToInt32(Console.ReadLine());
if(T<1 || T>100){
Console.WriteLine("!!! This input (T) should be between 1 and 100 !!!");
Environment.Exit(0);
}
int[][] arr = new int[T][];
for (int i = 0; i < T; i++){
int N = Convert.ToInt32(Console.ReadLine());
if(N<0 || N>50){
Console.WriteLine("!!! This input (N) should be between 1 and 50 !!!");
Environment.Exit(0);
}
try{
string Y = Console.ReadLine();
//split the string
string [] tempS = Y.Split(' ');
int [] temp = new int[N];
arr[i] = new int[N];
//assign elements to main jagged array as int
for (int j = 0; j < tempS.Length; j++){
temp[j]=Int32.Parse(tempS[j]);
arr[i][j] =temp[j];
if(arr[i].Length>50){
//i think this will never show up, but still...
Console.WriteLine("!!! Maximum number of elements is 50 !!!");
Environment.Exit(0);
}
}
}
catch{
Console.WriteLine("!!Elements should be separated with just one space AND should be number!!");
Console.WriteLine("... and don't lie to me :/ ");
Environment.Exit(0);
}
}
return arr;
}
//I had to use these a lot for testing and seeing.
static void printArr(int[] arr){
for (int i = 0; i < arr.Length; i++){
Console.Write(arr[i] + " ");
}
}
public static void printArray( int [][] Array){
for (int i = 0; i < Array.Length; i++){
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < Array[i].Length; j++){
System.Console.Write("{0}{1}", Array[i][j], j == (Array[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment