I am digging the speed of the new PowerShell 6.0

Standard

Yesterday, I had the pleasure of installing PowerShell 6.0 (pwsh) on my Development machine. You can find it here pwsh (I dig the new name. It is very unix-like)

I work for Microsoft. And a big part of my day is finding the right project/file to modify so I can knock work-items off my list. I use this PowerShell query often to find file names under a directory.

PS5.1 § ferasd-dev C:> ls -Recurse -Filter *.cs

This commands takes a long time for large directories. With pwsh, I noticed the increase in speed. So I thought I would measure the percentage of increase on my machine and share that with you. Here are the commands I ran:

PS5.1 § ferasd-dev {C:\}> $x = 0; $i = 10; 1..$i | %{
           $x += (Measure-Command {ls -Recurse -Filter *.cs -ErrorAction SilentlyContinue}).TotalMilliseconds
};
$x/$i

The result I got was 10037.42759 milliseconds using PowerShell 5.1.

When I run the same script in pwsh, I get 8813.46947milliseconds.

The increase ~12%. But it compounds when working with large directory with massive amount of files.

FA

Advertisements

Soccer wearable technology

Standard

Lately, I have been looking into ways to up my soccer game. I feel that I am not improving. I took it upon myself to make it one of my 2018 goals. How can I measure such progress? Do I rely on my feelings? Did not get tired until the game ended? Scored x goals? Feel faster? Lost weight?

I need someone to watch me. I need someone to time my sprinting speed. I am pretty sure I cannot convince anyone to come to my pickup or Microsoft Soccer League games just to gather statistics about me. I guess only technology will save me. Here is what I found for measuring my soccer game:

  • Zepp

Zepp is a small devise that you wear on your shin. It will give you rough stats on your kicks, distance run, speed, and some other minor stats. Speed and distance is an important metric to up my game, so I was interested in Zepp. (Will definitely get it for my kid if he is interested in soccer)

  • Adidas miCoach

Smart ball from Adidas! Who does not want that? It is perfect to work on your knuckle ball kick, curve, and power kicks.

  • Playertek

This is just a GPS tracker as far as I can tell. It is used by professional soccer players. A little pricey. But I believe it give more accurate results than Zepp.

 

Hope you enjoyed this post. Please let me know what you think, or if you have other products @_ferasd on twitter.

 

FA

Threading in C# (Ultra Basic).

Standard

Baby steps

I come from a Ruby background and threading there was as easy as:

# Ruby Code
Thread.new { puts "hello from thread" }

I am pleased to tell you that in C#, it is easy as well.

// C# code
new Thread( () = Console.WriteLine("hello from thread") ).Start()

The .Start() at the end is a little annoying as it is redundant. But I am sure I will find out why it is there in the near future.

Congratulations, you have started a thread and wrote to the console.

This website helped me out a lot. Thank you random person on internet.

Albahari on Threading

Lastly, this song is awesome

Daily stand-ups 

Standard

Daily stand-up is meeting that happens every day (usually the first thing in the morning) where all team members are required to attend.

Daily stand-ups try to achieve a couple of goals from the engineering perspective. They should

  1. Give a status update on what each engineer is doing.
  2. Identify any blockers.
  3. Ask for help if needed.
  4. Short term future plan.

Let me break down what each of these means. I will also add what really happens during stand-ups.

Give a status update.

Goal: Sync all engineers together. Engineers are to talk only at a high-level and not waste time on details. Each engineer will listen to what other teammates are working on and get a sense of the overall product development.

What actually happens: Every engineer is thinking what they are going to say when their turn comes. After their turn, thy are thinking about what they will work on after stand-up. Nobody is nobody is listening to what other engineers are doing. Probably, only the team lead and project managers are paying attention.
Identify any blockers.

Goal: Inform teammates of any blockers that are development or resource related. For example, one could say “I am blocked by John’s work”  or “I am waiting to get a larger server to test my code”.

What actually happens: This comes up very rarely. Because people use email to communicate any blockers as soon as thy happen. No one waits a whole day just to tell others at stand-up. Even after identifying any blockers at stand-up, they still need to follow up with emails.
Ask for help

Goal: Engineers who are mentally/technically blocked (not by resources) ask for guidance.

What really happens: When an engineer is stuck, they ask for help via email during a work day. No engineer will publicly ask for help during stand-up. Most engineers ask discretely or just spend time pounding on the problem harder until they figure it out.

Short term future plan

Goal: identify what each engineer will do next.

What really happens: Engineers will usually work on whatever is in their backlog. This is already known by the only people who are listening (team lead and project managers).
Having said all of that, I think stand-ups are fun to have as it gets you closer to your teammates. However, if you really want to know my feelings towards daily stand-ups, read the red text.

Word Histogram

Standard

 

Given a document and a word, return the number of occurrences of that word in the document. What if this function is called many times?

Here we have to define what a word means. Is ‘Cat’ equivalent to ‘cat’ or ‘Cats’? Do we care about punctuation? e.g. do we consider ‘end’ equivalent to ‘end.’? Do we care about equivalency of abbreviated words, like ‘do not’ and ‘don’t’?

For this problem, I will define a word to be a string of characters separated by spaces. In addition, I will strip any punctuation that occurs at the end of the mentioned string and will assume everything is lowercase.

Let us consider the case that this function will not be frequently called. Since it will be frequently called, this suggest that we favor optimizing memory as we will only we using it a few times. Here are the high-level algorithm steps that will solve this problem:

  1. Scan document to next occurrence of a word until the end of the document.
  2. increment a counter
  3. go to step 1.
  4. return counter

This approach will take us O(d) worst-time where d is the size of the document. If we are not careful in implementing this algorithm, we could end up with O(w*d) worst-time where w is the length of the word and d is the length of the document. Here is the solution

# C++/JAVA way in ruby syntax.
def word_histo(doc, word)
  doc_pointer = 0
  counter = 0
  while doc_pointer < doc.length
    word_pointer = 0
    while doc[doc_pointer] == word[word_pointer] && doc_pointer < doc.length && word_pointer < word.length
      doc_pointer += 1
      word_pointer += 1
    end

    # in order to increment the counter, we need the following to be true:
    # 1. word_pointer == word.length (we found a all characters in doc)
    # 2. doc_pointer == doc.length OR doc[doc_pointer + 1] is not a an a-z character  (to avoid partial matching)
    if word_pointer == word.length
      if doc_pointer == doc.length || doc[doc_pointer + 1] !=~ /[[:alpha:]]/
        counter += 1
      end
    end
    doc_pointer += 1
  end
  counter
end

You can also implement the above solution with the help of the String#scan function in ruby. However, it returns an array so it has to keep track of all occurrences and that could be memory intensive. Here is the solution the Rubyist way

def word_histo(doc, word)
  doc.scan(/\b#{word}\b/).length
end

Now, in order to implement the solution that allows us to call this function, we need to give up memory for speed. We store every word in a hash that maps to its count. I will not bother optimizing the solution as it will look as ugly as the previous solution, here is a more ruby way to solve the problem:

def doc_histo(doc)
  histogram = Hash.new { 0 }
  doc.split(' ').each do |d|
    histogram[d] += 1
  end
  histogram
end

def word_histo(doc_histogram, word)
  doc_histogram[word]
end

We call doc_histo once. Then we use the hash generated to query for multiple words. This ensures a worst-time of O(d) for preprocessing, then O(1) for each query (ignoring the fact the we have to read the input of size w). But we keep O(d) memory.

Swap Numbers

Standard

 

Write a function that takes two numbers and returns the numbers in the swapped order without using temporary memory?

The catch here is we are not allowed to use a temporary memory. So the following solution is not acceptable:

# NOT OKAY
def swapper(a, b)
    t = a
    a = b
    b = a
    [a, b]
end

# btw, it is cheating to just return [b, a].

So how to we store information about two numbers in one ‘buffer’? Bit manipulation is the answer.

The thing you need to remember is the XOR operator. We note that if we XOR a number with itself, we get 0. Let us use this to our benefit. We can swap the two numbers if we can rewrite this equation and somehow not lose any information

# a is the first number, b is the second number
a = a^b^a
b = b^a^b

Note that in the first case we XOR ‘a’ twice to get rid if it. So essentially, we are saying a = b. But this will screw up our second equation as it will become

b = b^(b)^b

And that is just equal to b = b. But, if we have an intermediate step, we can rewrite our function

def swapper(a, b)
  a = a^b
  b = b^a # which is b^(a^b) = a
  a = a^b # which is (a^b)^a = b
  [a, b]
end

 

 

 

Intersection

Standard

 

Given two line segment (defined by four points), find their intersection if there is any?

Here, we can use our knowledge of pre-calculus to solve this problem. First, let us transform the input to a linear equation of the form that we all love y = m x + b. We can do this by finding the slope m first, then plugging in a point and the computed m and solve for the y-intercept b. We do this for both line segments and then equate them together to find the intersection point (x,y). Here is my code to solve this problem:

def intersection(x0s, y0s, x0f, y0f, x1s, y1s, x1f, y1f)
  # compute slopes
  epsilon = 0.0
  if y0f - y0s == 0
    epsilon = 0.00001
  end
  m0 = (x0f - x0s) / (y0f - y0s + epsilon)
  epsilon = 0.0
  if y0f - y0s == 0
    epsilon = 0.00001
  end
  m1 = (x1f - x1s) / (y1f - y1s + epsilon)

  # return nil if lines are parallel
  return [nil, nil] if m0 == m1

  # compute offset
  b0 = y0s - (m0 * x0s)
  b1 = y1s - (m1 * x1s)

  # compute intersection point
  x = (b1 - b0) / (m0 - m1)
  y = m0 * x + b0

  # check if point is sensible
  test_x = x < [x0s, x0f, x1s, x1f].max &&  x > [x0s, x0f, x1s, x1f].min
  test_y = y < [y0s, y0f, y1s, y1f].max &&  y > [y0s, y0f, y1s, y1f].min
  unless test_x && test_y
    return [nil, nil]
  end

  [x, y]
end