Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack Over Flow #15

Open
DavidZhiXing opened this issue Jul 21, 2021 · 5 comments
Open

Stack Over Flow #15

DavidZhiXing opened this issue Jul 21, 2021 · 5 comments

Comments

@DavidZhiXing
Copy link
Owner

learn from question and answer

@DavidZhiXing
Copy link
Owner Author

@DavidZhiXing DavidZhiXing changed the title ### Stack Over Flow Stack Over Flow Jul 26, 2021
@DavidZhiXing
Copy link
Owner Author

stack overflow

@DavidZhiXing
Copy link
Owner Author

DavidZhiXing commented Aug 10, 2021

convert a list of objects from one type to another using lambda expression

var origList = List<OrigType>(); // assume populated
var targetList = List<TargetType>(); 

foreach(OrigType a in origList) {
    targetList.Add(new TargetType() {SomeValue = a.SomeValue});
}
var targetList = origList
  .Select(x => new TargetType() { SomeValue = x.SomeValue })
  .ToList();

This is using a combination of Lambdas and LINQ to achieve the solution. The Select function is a projection style method which will apply the passed in delegate (or lambda in this case) to every value in the original collection. The result will be returned in a new IEnumerable. The .ToList call is an extension method which will convert this IEnumerable into a List.

If you know you want to convert from List to List then List.ConvertAll will be slightly more efficient than Select/ToList because it knows the exact size to start with:

target = orig.ConvertAll(x => new TargetType { SomeValue = x.SomeValue });

In the more general case when you only know about the source as an IEnumerable, using Select/ToList is the way to go. You could also argue that in a world with LINQ, it's more idiomatic to start with... but it's worth at least being aware of the ConvertAll option.

@DavidZhiXing
Copy link
Owner Author

DavidZhiXing commented Aug 30, 2021

How can you determine a point is between two other points on a line segment?

def distance(a,b):
    return sqrt((a.x - b.x)**2 + (a.y - b.y)**2)

def is_between(a,c,b):
    return -epsilon < (distance(a, c) + distance(c, b) - distance(a, b)) < epsilon
  • Check if the cross product of (b-a) and (c-a) is 0
  • check that the dot product of (b-a) and (c-a) is positive and is less than the square of the distance between a and b
def isBetween(a, b, c):
    crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y)

    # compare versus epsilon for floating point values, or != 0 if using integers
    if abs(crossproduct) > epsilon:
        return False

    dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y)
    if dotproduct < 0:
        return False

    squaredlengthba = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y)
    if dotproduct > squaredlengthba:
        return False

    return True

@DavidZhiXing
Copy link
Owner Author

When should you call yourself a senior developer? [duplicate]
You can call yourself a Senior when:

You can handle the entire software development life cycle, end to end
You lead others, or others look to you for guidance.
You can self manage your projects
Software development is a curious creature unlike other fields.

Sometimes, a fresh punk out of college can run circles around veterans who have 20+ years of "experience". Programming is a bizarre world where code is king.

Some achieve the above in 2 years or less, others take 10 years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant