C
C#15mo ago
احمد

❔ ✅ Mind explaining a code to me?

public static int? Closest(int[] arr)
{
var min = arr.Distinct().Where(x => Math.Abs(x) == arr.Min(Math.Abs));
return min.Count() == 1 ? min.First() : (int?) null;
}
public static int? Closest(int[] arr)
{
var min = arr.Distinct().Where(x => Math.Abs(x) == arr.Min(Math.Abs));
return min.Count() == 1 ? min.First() : (int?) null;
}
67 Replies
Angius
Angius15mo ago
It's a bunch of LINQ code .Distinct() gets only unique elements .Where() filters the collection by the predicate .Count() gets the amount of items in the collection .First() gets the first element condition ? iftrue : iffalse is a ternary expression
احمد
احمدOP15mo ago
removes duplicates
Angius
Angius15mo ago
x => stuff is a lambda
احمد
احمدOP15mo ago
i don't get this meaning? but idk what it means in this context
MODiX
MODiX15mo ago
Angius
REPL Result: Success
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.Where(number => number % 2 == 0)
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.Where(number => number % 2 == 0)
Result: WhereArrayIterator<int>
[
2,
4,
6,
8,
10
]
[
2,
4,
6,
8,
10
]
Compile: 574.081ms | Execution: 98.215ms | React with ❌ to remove this embed.
احمد
احمدOP15mo ago
filters with a condition i thought => was to return a value
MODiX
MODiX15mo ago
Angius
REPL Result: Success
int[] numbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
foreach(int number in numbers)
{
string message = number % 2 == 0 ? "even" : "odd";
Console.WriteLine($"Number {number} is {message}");
}
int[] numbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
foreach(int number in numbers)
{
string message = number % 2 == 0 ? "even" : "odd";
Console.WriteLine($"Number {number} is {message}");
}
Console Output
Number 1 is odd
Number 2 is even
Number 3 is odd
Number 4 is even
Number 5 is odd
Number 6 is even
Number 7 is odd
Number 8 is even
Number 9 is odd
Number 10 is even
Number 1 is odd
Number 2 is even
Number 3 is odd
Number 4 is even
Number 5 is odd
Number 6 is even
Number 7 is odd
Number 8 is even
Number 9 is odd
Number 10 is even
Compile: 782.019ms | Execution: 141.873ms | React with ❌ to remove this embed.
Angius
Angius15mo ago
Kinda-sorta The lambda number => number % 2 == 0 will return a boolean If that boolean is false, the element gets filtered out
احمد
احمدOP15mo ago
No description
احمد
احمدOP15mo ago
is number a made up var? number => number... that one
Angius
Angius15mo ago
number => number % 2 == 0;
number => number % 2 == 0;
is basically a shorter, anonymous version of
public bool DoSuff(int number)
{
return number % 2 == 0;
}
public bool DoSuff(int number)
{
return number % 2 == 0;
}
It's a parameter
احمد
احمدOP15mo ago
is it only for lists? like
Angius
Angius15mo ago
No, you can use a lambda whenever, wherever
احمد
احمدOP15mo ago
for (x in var_name){}
for (x in var_name){}
the
x
x
but in this context is it for lists
MODiX
MODiX15mo ago
Angius
REPL Result: Success
var sum = (int a, int b) => a + b;

sum(2, 4)
var sum = (int a, int b) => a + b;

sum(2, 4)
Result: int
6
6
Compile: 596.015ms | Execution: 63.795ms | React with ❌ to remove this embed.
احمد
احمدOP15mo ago
=> is for returning
Angius
Angius15mo ago
If you're talking about LINQ, the .Where() part, then it's for any IEnumerable Including lists It just happens to take a Func<T, bool> as a parameter
احمد
احمدOP15mo ago
so public static int? Closest(int[] arr) { var min = arr.Distinct().Where(x => Math.Abs(x) == arr.Min(Math.Abs)); return min.Count() == 1 ? min.First() : (int?) null; } that's saying Distinct removes all dupes math.abs(x) == arr.Min(Math.Abs)) the absolute value of x is equal to the minimum absolute value in the list?
Angius
Angius15mo ago
Yep
احمد
احمدOP15mo ago
so like
Angius
Angius15mo ago
.Where() removes all elements where that is not true
احمد
احمدOP15mo ago
yeah so it filters to the condition new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} in this list min abs value is 1 and the value of x is in the first iteration 1 yeah bro i'm so lost with the logic too like i was confused with
for (int x = 0; x < 100; x++){}
for (int x = 0; x < 100; x++){}
but i learned the first section is when you define x, next is the condition, and next is what happens when the condition is met without Distinct() would it still filter?
Angius
Angius15mo ago
Yeah
احمد
احمدOP15mo ago
or do you need Distinct() and Where() together so distinct removes dupes and where filters after dupes have been removed?
Angius
Angius15mo ago
Exactly
احمد
احمدOP15mo ago
shoot
Angius
Angius15mo ago
You can chain LINQ methods almost at will
احمد
احمدOP15mo ago
i'm basically new beginner i'm sixth form y12 next yr we gonna make websites so i gotta study c# a lot
Angius
Angius15mo ago
Well, lambdas and LINQ are definitely not beginner topics
احمد
احمدOP15mo ago
i have known lambda from time basics of it so to find the number closest to 0 find the smallest possible value in abs form that's literally it
softmek
softmek15mo ago
No description
احمد
احمدOP15mo ago
what's the point of all that when you're tryna find the number closest to 0
احمد
احمدOP15mo ago
No description
softmek
softmek15mo ago
Am just explaining your answer, You never sent your qn
احمد
احمدOP15mo ago
no i wasn't tryna be rude
softmek
softmek15mo ago
no worries, send the question
احمد
احمدOP15mo ago
what's min.Count() and min.First()
MODiX
MODiX15mo ago
akhahmed
Quoted by
From akhahmed
React with ❌ to remove this embed.
MODiX
MODiX15mo ago
Angius
REPL Result: Success
new int[]{1, 2, 3, 4, 5, 6}.Count()
new int[]{1, 2, 3, 4, 5, 6}.Count()
Result: int
6
6
Compile: 542.876ms | Execution: 51.420ms | React with ❌ to remove this embed.
احمد
احمدOP15mo ago
Simply find the closest value to zero from the list. Notice that there are negatives in the list. List is always not empty and contains only integers. Return None if it is not possible to define only one of such values. And of course, we are expecting 0 as closest value to zero.
MODiX
MODiX15mo ago
Angius
REPL Result: Success
new int[]{1, 2, 3, 4, 5, 6}.First()
new int[]{1, 2, 3, 4, 5, 6}.First()
Result: int
1
1
Compile: 541.726ms | Execution: 67.039ms | React with ❌ to remove this embed.
احمد
احمدOP15mo ago
o
Angius
Angius15mo ago
I highly recommend trying to run the code and observing the results lol Learning by doing and all that jazz
احمد
احمدOP15mo ago
why is he doing min.Count ==1 i know i receive so many
Angius
Angius15mo ago
Checking if the collection has only a single element
احمد
احمدOP15mo ago
errors cause they put it in a clas s
Angius
Angius15mo ago
If it does, then that element is the closest to zero If there's more, that means there are more elements just as close to zero
softmek
softmek15mo ago
sending my solution in a sec, i'll create a function and explain in comments public static int? Closest(int[] arr) { int closestAbsoluteValue = arr.Select(Math.Abs).Min(); // Find the minimum absolute value var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value if (closestValues.Count() == 1) { return closestValues.First(); // If only one closest value found, return it } else { return null; // If multiple values with the same absolute value exist, return None } }
Angius
Angius15mo ago
$code
MODiX
MODiX15mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
احمد
احمدOP15mo ago
$code
public static int? Closest(int[] arr)
{
int closestAbsoluteValue = arr.Select(Math.Abs).Min(); // Find the minimum absolute value

var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value

if (closestValues.Count() == 1)
{
return closestValues.First(); // If only one closest value found, return it
}
else
{
return null; // If multiple values with the same absolute value exist, return None
}
}
public static int? Closest(int[] arr)
{
int closestAbsoluteValue = arr.Select(Math.Abs).Min(); // Find the minimum absolute value

var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value

if (closestValues.Count() == 1)
{
return closestValues.First(); // If only one closest value found, return it
}
else
{
return null; // If multiple values with the same absolute value exist, return None
}
}
MODiX
MODiX15mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
احمد
احمدOP15mo ago
public static int? Closest(int[] arr)
{
int closestAbsoluteValue = arr.Select(Math.Abs).Min(); // Find the minimum absolute value

var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value

if (closestValues.Count() == 1)
{
return closestValues.First(); // If only one closest value found, return it
}
else
{
return null; // If multiple values with the same absolute value exist, return None
}
}
public static int? Closest(int[] arr)
{
int closestAbsoluteValue = arr.Select(Math.Abs).Min(); // Find the minimum absolute value

var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value

if (closestValues.Count() == 1)
{
return closestValues.First(); // If only one closest value found, return it
}
else
{
return null; // If multiple values with the same absolute value exist, return None
}
}
$code
MODiX
MODiX15mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
softmek
softmek15mo ago
look, good
احمد
احمدOP15mo ago
i copied yours 💀 ofc i'm not gonna use it tho
softmek
softmek15mo ago
No description
احمد
احمدOP15mo ago
var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value
var closestValues = arr.Where(x => Math.Abs(x) == closestAbsoluteValue); // Find values with the closest absolute value
last thing i wanna know why is he tryna find the values with the closest abs value
softmek
softmek15mo ago
No description
Angius
Angius15mo ago
Because -2 is as close to zero as +2
احمد
احمدOP15mo ago
oh wait wait
Angius
Angius15mo ago
So you need to treat negative and positive values the same, in terms of proximity to 0
softmek
softmek15mo ago
So, by finding the values with the closest absolute value to zero, the function determines which number or numbers are closest to zero, considering both positive and negative numbers equally in their proximity to zero. If there's a unique value that's closest to zero, the function returns that value. If multiple values share the same closest absolute distance to zero, it indicates that there's no singular closest value to zero, so the function returns None.
احمد
احمدOP15mo ago
shoot shoot shoot public static int? Closest(int[] arr) { var min = arr.Distinct().Where(x => Math.Abs(x) == arr.Min(Math.Abs)); return min.Count() == 1 ? min.First() : (int?) null; } Distince() removes all dupes, Where()... sorts it out so only the smallest value is left, there would be 2 values left if there was a negative variant. IF there is either positive or negative, it will min.First(), and if there are 2 values with equal prox ( e.g. -2, 2 ) then it returns null?
return min.Count() == 1 ? min.First() : (int?) null;
return min.Count() == 1 ? min.First() : (int?) null;
why can't it be
return min.Count() == 1 ? min.First() : null;
return min.Count() == 1 ? min.First() : null;
Math.Abs holds all unique integers, and when == is set, it makes the list smaller because basically a condition has been set where it's equal to the min abs value perfect, thank you so much :))) !close
Accord
Accord15mo ago
Closed!
softmek
softmek15mo ago
(int?) means it's a nullable integer value, null return emply. which means (int?) will not work for non-numerical values,
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?