plusMinus function from Hacker Rank [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to solve a task from HackerRank, but I'm not able to solve it. Any help is appreciated!
public static void plusMinus(List<int> arr)
{
float noOfPositive = 0;
float noOfNegative = 0;
float noOfZero = 0;
float length = arr.Count;
for(var i = 0; i < arr.Count; i++){
if(i > 0){
noOfPositive += 1;
}else if(i == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
Console.WriteLine($"{noOfPositive/length}\n{noOfNegative/length}\n{noOfZero/length}");
input:
6
-4 3 -9 0 4 1
My Result:
0.8333333
0
0.1666667
Expected:
0.500000
0.333333
0.166667

You are checking the value of i not arr[i].
So your code should be:
for(var i = 0; i < arr.Count; i++){
if(arr[i] > 0){
noOfPositive += 1;
}else if(arr[i] == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
And your expected values are wrong, you are putting in 7 items and expecting half of them to be positive so 3.5 items are positive?

Related

How do you find the positive sum of an array that also has negative numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
public static int PositiveSum(int[] arr)
{
int sum = 0;
for(int i = 0; i > arr.Length; i++)
{
sum+=arr[i];
}
return sum;
}
Example:
[1,-4,7,12] => 1 + 7 + 12 = 20
Note: if there is nothing to sum, the sum defaults to 0.
One way, with minimum changes to your code, is to add a condition inside the for loop that sums the values allowing only positive numbers to be added.
Something you must change is the condition in the loop, i > arr.Length will break the cycle immediately, even if Length is 0, it needs to be i < arr.Length.
public static int PositiveSum(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > 0)
sum += arr[i];
}
return sum;
}
I would try filtering the arr to only contain positive numbers, then use sum on that new list:
public static int PositiveSum(int[] arr)
{
var pos = arr.Where(x => x > 0);
int sum = pos.Sum();
return sum;
}

New Year Chaos HackerRank Practise Problem - C# solution optimization [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
static void minimumBribes(int[] q)
{
Int32 TotalCount = 0;
bool blnSuccess = true;
Int32[] size = Ordering(q);
for (int intI = 0; intI < q.Length; intI++)
{
Int32 Tempvariable = 0;
Int32 TooChaotic = 0;
Int32 index = Index(size,q[intI]);
do
{
if (q[intI] != size[intI])
{
Tempvariable = size[index];
size[index] = size[index - 1];
size[index - 1] = Tempvariable;
index = index - 1;
TooChaotic = TooChaotic + 1;
if (TooChaotic > 2)
{
break;
}
TotalCount = TotalCount + 1;
}
} while (q[intI] != size[intI]);
if (TooChaotic > 2)
{
Console.WriteLine("Too chaotic");
blnSuccess = false;
break;
}
}
if (blnSuccess)
{
Console.WriteLine(TotalCount);
}
}
static int[] Ordering(int[] z)
{
int[] r = new int[z.Length];
r = z.OrderBy(x => x).ToArray();
return r;
}
static int Index(int[] z,int integer)
{
for (int intI = 0; intI < z.Length; intI++)
{
if(z[intI]== integer)
{
return intI;
}
}
return 0;
}
This code is working fine, but its runtime is too long.
I'm getting "Terminated due to timeout" in HackerRank. However, the solution is working fine on the local computer but it's taking more time.
Problem Link:https://www.hackerrank.com/challenges/new-year-chaos/problem.
Sample Input
2 (the number of test cases)
5 (number of people in the queue)
2 1 5 3 4 (n space-separated integers describing the final state of the queue)
5 (number of people in the queue)
2 5 1 3 4 (n space-separated integers describing the final state of the queue).
It must print an integer representing the minimum number of bribes necessary, or Too chaotic if the line configuration is not possible.
Output
3
Too chaotic
Question:
How do I reduce its runtime? Presently, I am using an array.
I've solved it a few weeks ago, this is my solution to the problem (100%)
static void minimumBribes(int[] q) {
int bribe = 0;
bool chaotic = false;
int n = q.Length;
for(int i = 0; i < n; i++)
{
if(q[i]-(i+1) > 2)
{
chaotic = true;
break;
}
for (int j = Math.Max(0, q[i]-2); j < i; j++)
if (q[j] > q[i])
bribe++;
}
if(chaotic)
Console.WriteLine("Too chaotic");
else
Console.WriteLine(bribe);
}
You don't need any other methods than the one provided by the challenge

Program generates 50 random numbers (between 1 to 10) and tells the amount of values that are smaller than 5 and bigger than 5 (C#) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Program generates 50 random numbers (between 1 to 10) and tells the amount of values that are smaller than 5 and bigger than 5 (C#)
Try this:
int n = 50
IList<int> randomNumbers = new List<int>(n);
Random ran = new Random(1);
for (int i = 0; i < n; i++)
{
randomNumbers.Add(ran.Next(1, 10));
}
int lessThan5Count = randomNumbers.Count(c => c < 5);
int greaterThan5Count = randomNumbers.Count(c => c > 5);
You could do it like that:
Random r = new Random();
int n = 50;
int smaller_than_5 = 0;
int bigger_than_5 = 0;
double[] d = new double[n];
for (int i = 0; i < n; i++)
{
d[i] = 1 + r.NextDouble() * 9;
if (d[i] < 5) smaller_than_5++;
else if (d[i] > 5) bigger_than_5++;
}
Hope this helps ...

Array ints and replace the order of number I write [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I try Start the console and after i write 5 numbers is do exsepsion what wrong in code
what the console do is after I write 5 number for example 7,4,9,5,1 ---> 1,5,9,4,7
this what I wrote
static void Main(string[] args)
{
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}
Console.Read();
You missed two things. First - in order to swap two array items, you need to store item somewhere. Otherwise you just replace one item with other. Second - arrays have indexes from 0 to Length - 1. So you need to subtract 1 from second item index:
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}

C# Types of files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Besides a .txt, where else i can insert data using C# ?
string file = GenerarPath(Environment.SpecialFolder.MyDocuments, "Pascal Triangle.txt");
var tw = new StreamWriter(file);
Console.WriteLine("\t\t Pascal Triangle in C#");
Console.WriteLine("\t________________________________________");
Console.Write("\nNumber of Rows: ");
int number = int.Parse(Console.ReadLine());
int x = number * 2;
for (int i = 0; i <= number; ++i, x -= 2)
{
for (int s = 0; s <= x; ++s)
tw.Write(" ");
for (int k = 0; k <= i; ++k)
//Console.Write(String.Format("{0,4:D}", formula(i, k)));
tw.Write(String.Format("{0,4:D}",pascal(i,k)));//formula(i, k)));
tw.WriteLine();
}
tw.Close();
Console.WriteLine("Finished");
Console.ReadKey();
Thank you
Almost anywhere. like databases (Sqlserver, access), word files, txt files as you have done.

Categories

Resources