Friday, January 10, 2020

Import-Module Azure fails

Import-Module : The specified module 'Azure' was not loaded because no valid module file was found in any module

Download the latest PowerShell SDK from here.

Thursday, January 9, 2020

Delete all node_modules folders recursively

In bash

List
find . -name "node_modules" -type d -prune -print | xargs du -chs

Delete
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

Delete all bin/package folders

List:
find . -path "*/bin/package" -type d

Delete
find . -path "*/bin/package" -type d -prune -print -exec rm -rf '{}' \;

Tuesday, December 3, 2019

node-gyp build errors

If you get npm errors like:

node_modules\crypt3\crypt3.cc(5,10): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory  node_modules\crypt3\build\crypt3.vcxproj

Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1 

There are so many articles about resolving node-gyp errors. The one that worked for me and the easiest is

npm install --global --production windows-build-tools

From:
https://github.com/nodejs/node-gyp#on-windows

https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules


Monday, October 7, 2019

The file is locked by NET Core Host error

Error MSB3027 Could not copy "obj\Debug\netcoreapp3.0\Project.Tests.dll" to "bin\Debug\netcoreapp3.0\Project.Tests.dll". Exceeded retry count of 10. Failed. The file is locked by: ".NET Core Host (35124)" Project.Tests C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 4206

Open task manager, find the Visual Studio process, like in the image below.
Locate the sub process .NET Core Host and terminate it.
This solved the building problem with me.


It could appear separately as well


Friday, April 20, 2018

The reasones why i changed my mac book pro to a windows lapop


Everybody loves MacBooks, i was curious and happy to get a MacBook with touch bar at work, but once i got it, my hate to it started and built up and was increasing day after day.

I finally decided to ex-change it with a windows laptop for the below reasons:

  • Gets very hot most of the time, with just chrome and slack running.
  • Keyboard is awful, key press distance is very little, noisy as well.
  • Touch bar means that i need to do 2 steps for simple tasks like changing the volume or brightness.
  • Touch bar place makes it easy to touch its buttons unintentionally, i found myself touching the mute button or the Siri multiple times when pressing the backspace button.
  • The Siri button is really annoying, i have disabled it and still it comes up with a pop every time it is touched.
  • Touch pad is too big, lots of times my hand was resting on it while i was using it. 
  • I heard it is very stable, but mine was crashing every couple of days. Lots of times i start it to find that it is actually restarting instead of resuming from sleep.
  • Mostly annoying: I need my USB ports, not every thing is Type C. I had a hard drive that i couldn't connect, simply because i left the converter at my office. In addition to USB headphones, etc...
  • OSX lacks lots of simple tasks, coming from a windows. I had to do some tweaks to have some of them. Like: Locking the machine with keyboard shortcut, arranging windows and moving between different screens with keyboard shortcut, Minimising windows, etc...
  • I wasn't able to login multiple time after woking form sleep.
  • Software updates were very frequent and always asking for restart.


What i liked:
  • Display is amazing.
  • Speakers are amazing.
  • Touch pad gestures are nice.


Friday, July 21, 2017

Importing all pfx certificates in a folder

Assuming all pfx have the same password

forfiles /p "E:\Certificates" /m *.pfx /c "cmd /c CERTUTIL -f -p "password" -importpfx @file"

Tuesday, July 11, 2017

Sort the odd numbers in array

Given an array of numbers, it is required to sort ascending odd numbers but even numbers must be on their places.

ex:
{ 5, 3, 2, 8, 1, 4 } should give { 1, 3, 2, 8, 5, 4 }
{ 5, 3, 1, 8, 0 } should give { 1, 3, 5, 8, 0 }

Algorithm:
define sorted list
define another list to save the odd number indexes
Iterate through the array for each item
     if item is even skip it
     if item is odd:
           add it to the sorted list
           add the item index to indexes list
loop through the indexes list, for each index
     save the item from the sorted list into the the numbers array at the that index

Note: the sorted list should support duplicate items

Example implementations

Using a list

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace sortOdd
{
    public class Program
    {
        static void Main(string[] args)
        {
            CollectionAssert.AreEqual(new int[] { 1, 3, 2, 8, 5, 4 }, Program.SortArray(new int[] { 5, 3, 2, 8, 1, 4 }));
            CollectionAssert.AreEqual(new int[] { 1, 3, 5, 8, 0 }, Program.SortArray(new int[] { 5, 3, 1, 8, 0 }));
            CollectionAssert.AreEqual(new int[] { }, Program.SortArray(new int[] { }));
        }

        public static int[] SortArray(int[] array)
        {
            var result = new int[array.Length];
            List<int> sortedOdd = new List<int>();
            var indexes = new List<int>();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i]%2 == 0)
                    result[i] = array[i];
                else
                {
                    int index = sortedOdd.BinarySearch(array[i]);
                    if (index < 0)
                        sortedOdd.Insert(~index, array[i]);
                    else
                        sortedOdd.Insert(index, array[i]);
                    indexes.Add(i);
                }
            }
            for (int i = 0; i < indexes.Count; i++)
            {
                result[indexes.ElementAt(i)] = sortedOdd.ElementAt(i);
            }
            return result;
        }
    }
}

Using sorted list 

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace sortOdd
{

    public class Program
    {
        static void Main(string[] args)
        {
            CollectionAssert.AreEqual(new int[] { 1, 3, 2, 8, 5, 4 }, Program.SortArray(new int[] { 5, 3, 2, 8, 1, 4 }));
            CollectionAssert.AreEqual(new int[] { 1, 3, 5, 8, 0 }, Program.SortArray(new int[] { 5, 3, 1, 8, 0 }));
            CollectionAssert.AreEqual(new int[] { }, Program.SortArray(new int[] { }));
        }

        public static int[] SortArray(int[] array)
        {
            var result = new int[array.Length];
            var sortedOdd = new SortedSet<int>(new DuplicateKeyComparer<int>());
            var indexes = new List<int>();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i]%2 == 0)
                    result[i] = array[i];
                else
                {
                    sortedOdd.Add(array[i]);
                    indexes.Add(i);
                }
            }
            for (int i = 0; i < indexes.Count; i++)
            {
                result[indexes.ElementAt(i)] = sortedOdd.ElementAt(i);
            }
            return result;
        }
    }

    public class DuplicateKeyComparer<TKey>:IComparer<TKey> where TKey : IComparable
    {
        public int Compare(TKey x, TKey y)
        {
            int result = x.CompareTo(y);

            if (result == 0)
                return 1;  
            return result;
        }
    }
}

Monday, July 10, 2017

Level order traversal in spiral form

Printing tree in spiral form, or a zigzag form

would print 1 2 3 4 5 6 7
Algorithm

printSpiral(tree)
  set level = tree root
  while level contains nodes
     printLevel(level, ltr)
     set level = getNextLevel(level)
     ltr == !ltr 
Function to get next level
getNextLevel(level)
set nextLevel as empty list
foreach node in level
     if(node.left!=null) nextLevel.add(node.left)
     if(node.right!=null) nextLevel.add(node.right)
Function to print all nodes at a given level
printLevel(level, ltr)
if(!ltr)
    reverse level
foreach node in level
    print node.data

Example code in Java

package com.algorithms.treespiral;

import
java.util.ArrayList;
import
java.util.Collections;

public class
Main {
   
/* Driver program to test the above functions */
   
public static void main(String[] args)
    {
        BinaryTree tree =
new BinaryTree();
       
tree.root = new Node(1);
       
tree.root.left = new Node(2);
       
tree.root.right = new Node(3);
       
tree.root.left.left = new Node(7);
       
tree.root.left.right = new Node(6);
       
tree.root.right.left = new Node(5);
       
tree.root.right.right = new Node(4);
       
System.out.println("Spiral order traversal of Binary Tree is ");
       
tree.printSpiral(tree.root);
   
}
}

class Node
{
   
int data;
   
Node left, right;

    public
Node(int d)
    {
       
data = d;
       
left = right = null;
   
}
}

class BinaryTree
{
    Node
root;

   
/* print spiral order traversal of a tree  */
   
void printSpiral(Node node)
    {
       
boolean ltr = false;
       
ArrayList<Node> layer = new ArrayList<>();
       
layer.add(node);
        while
(layer.size() != 0)
        {
            printLevel(layer
,ltr);
           
layer = getNextLevel(layer);
           
ltr = !ltr;
       
}
    }

   
/* gets the next layer in the tree */
   
ArrayList<Node> getNextLevel(ArrayList<Node> node) {
        ArrayList<Node> nextLayer =
new ArrayList<>();
        for
(Node n:node) {
           
if(n.left != null)
                nextLayer.add(n.
left);
            if
(n.right != null) {
                nextLayer.add(n.
right);
            
}
        }
       
return nextLayer;
   
}

   
/* prints tree level taking direction into account */
   
void printLevel(ArrayList<Node> node, boolean ltr) {
       
if (ltr != true) {
            Collections.reverse(node)
;
       
}
       
for (Node n:node) {
            System.
out.print(n.data + " ");
       
}
    }
}

Wednesday, June 21, 2017

IIS problems

Some commands to help troubleshoot windows hosted web applications 

URL reservations: 

netsh http show urlacl
netsh http delete urlacl url=https://+:443/
netsh http add urlacl url=https://+:443/ user=everyone

Firewall

netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=443 remoteip=any action=allow

Certificates

Show certificates
netsh http show sslcert ipport=0.0.0.0:44347

delete cert
netsh http delete sslcert ipport=0.0.0.0:44347

Add cert
netsh http add sslcert ipport=0.0.0.0:44347 certhash=d4e23174e12ec96e8d3b7cacf7a4f83da9c08e12 appid={214124cd-d05b-4309-9af9-9caa44b2b74a}

Network 
netstat -a -n -o | findstr 443
netstat -ab  | findstr 443

Apppool
Look at the app pools status
c:\Windows\System32\inetsrv\appcmd.exe list apppools

Check event viewer

In my case i found the following error
An error occurred while using SSL configuration for endpoint 127.0.0.1:443.  The error status code is contained within the returned data.


Monday, June 12, 2017

Git tips

git config push.default current
Pushing to the server with he same branch name automatically

Some usefull aliases 
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cob checkout -b
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.df 'difftool'
git config --global alias.mf 'mergetool'