C
C#13mo ago
Ahmed

❔ ✅ 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
Angius13mo 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
Ahmed
AhmedOP13mo ago
removes duplicates
Angius
Angius13mo ago
x => stuff is a lambda
Ahmed
AhmedOP13mo ago
i don't get this meaning? but idk what it means in this context
MODiX
MODiX13mo 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.
Ahmed
AhmedOP13mo ago
filters with a condition i thought => was to return a value
MODiX
MODiX13mo 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
Angius13mo ago
Kinda-sorta The lambda number => number % 2 == 0 will return a boolean If that boolean is false, the element gets filtered out
Ahmed
AhmedOP13mo ago
No description
Ahmed
AhmedOP13mo ago
is number a made up var? number => number... that one
Angius
Angius13mo 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
Ahmed
AhmedOP13mo ago
is it only for lists? like
Angius
Angius13mo ago
No, you can use a lambda whenever, wherever
Ahmed
AhmedOP13mo ago
for (x in var_name){}
for (x in var_name){}
the
x
x
but in this context is it for lists
MODiX
MODiX13mo 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.
Ahmed
AhmedOP13mo ago
=> is for returning
Angius
Angius13mo 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
Ahmed
AhmedOP13mo 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
Angius13mo ago
Yep
Ahmed
AhmedOP13mo ago
so like
Angius
Angius13mo ago
.Where() removes all elements where that is not true
Ahmed
AhmedOP13mo 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
Angius13mo ago
Yeah
Ahmed
AhmedOP13mo ago
or do you need Distinct() and Where() together so distinct removes dupes and where filters after dupes have been removed?
Angius
Angius13mo ago
Exactly
Ahmed
AhmedOP13mo ago
shoot
Angius
Angius13mo ago
You can chain LINQ methods almost at will
Ahmed
AhmedOP13mo 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
Angius13mo ago
Well, lambdas and LINQ are definitely not beginner topics
Ahmed
AhmedOP13mo 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
softmek13mo ago
No description
Ahmed
AhmedOP13mo ago
what's the point of all that when you're tryna find the number closest to 0
Ahmed
AhmedOP13mo ago
No description
softmek
softmek13mo ago
Am just explaining your answer, You never sent your qn
Ahmed
AhmedOP13mo ago
no i wasn't tryna be rude
softmek
softmek13mo ago
no worries, send the question
Ahmed
AhmedOP13mo ago
what's min.Count() and min.First()
MODiX
MODiX13mo ago
akhahmed
Quoted by
From akhahmed
React with ❌ to remove this embed.
MODiX
MODiX13mo 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.
Ahmed
AhmedOP13mo 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
MODiX13mo 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.
Ahmed
AhmedOP13mo ago
o
Angius
Angius13mo ago
I highly recommend trying to run the code and observing the results lol Learning by doing and all that jazz
Ahmed
AhmedOP13mo ago
why is he doing min.Count ==1 i know i receive so many
Angius
Angius13mo ago
Checking if the collection has only a single element
Ahmed
AhmedOP13mo ago
errors cause they put it in a clas s
Angius
Angius13mo 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
softmek13mo 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
Angius13mo ago
$code
MODiX
MODiX13mo 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/
Ahmed
AhmedOP13mo 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
MODiX13mo 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/
Ahmed
AhmedOP13mo 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
MODiX13mo 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
softmek13mo ago
look, good
Ahmed
AhmedOP13mo ago
i copied yours 💀 ofc i'm not gonna use it tho
softmek
softmek13mo ago
No description
Ahmed
AhmedOP13mo 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
softmek13mo ago
No description
Angius
Angius13mo ago
Because -2 is as close to zero as +2
Ahmed
AhmedOP13mo ago
oh wait wait
Angius
Angius13mo ago
So you need to treat negative and positive values the same, in terms of proximity to 0
softmek
softmek13mo 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.
Ahmed
AhmedOP13mo 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
Accord13mo ago
Closed!
softmek
softmek13mo ago
(int?) means it's a nullable integer value, null return emply. which means (int?) will not work for non-numerical values,
Accord
Accord13mo 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.
Want results from more Discord servers?
Add your server