Pages

Friday, June 28, 2019

Visual Studio Productivity Tips

Every version of Visual Studio introduces new productivity features. If you want to see some of the ones introduced in Visual Studio 2019, check out Kendra's video here. But what about the ones that have been in Visual Studio for a while that you may have missed? To see some of those, watch this video.


Kendra shows:
  • Navigate backward [03:55]
  • Solution Explorer tips [04:55]
  • Editor context menu (Alt + `) [06:40]
  • Keyboard command mapping [09:30]
  • Right-click on .sln > Open With [13:00]
  • Clipboard History (Ctrl + Shift + V) [14:10]
  • Go to recent files (Ctrl + T + R) [15:10]
  • Go to last edit location (Ctrl + Shift + Backspace)[15:20]
  • Multi-caret editing [16:10]
  • Using Git in Team Explorer [19:00]
  • Code snippets (Ctrl + K, Ctrl + X) [24:55]


Monday, June 24, 2019

C Sharp A pure implementation of push and pop functionality of string array



Here's a pure implementation of push and pop functionality of  string[] array in C Sharp, with timings. 


It does not use
 Queue<T> or Stack

For reference, Microsoft's own C# Stack implementation copies to an array! 

Here's a generalized version for objects C# Program to Implement Stack with Push and Pop Operations - Sanfoundry
Stack Data Structure Using Array and Linked List (codesdope.com)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Pushes an item to the top of the stack.
        // 
        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Push"]/*' />
        public void Push(T item) {
            if (_size == _array.Length) {
                T[] newArray = new T[(_array.Length == 0) ? _defaultCapacity : 2*_array.Length];
                Array.Copy(_array, 0, newArray, 0, _size);
                _array = newArray;
            }
            _array[_size++] = item;
            _version++;
        }



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;

namespace FixedLengthStackImplementationofStackofStringsArray
{
    internal class Stack
    {
        static readonly int MAX = 3; //Max number of elements in string[] array
        int count; 

        string[] stack = new string[MAX];

        public Stack() //constructor
        {
            count = 1;           //initialize stack with how many default values 
            stack = new string[] { "x", "y", "z", "w" }; //w is ignored, no error given
        }

        public bool IsEmpty()
        {
            return (count == 0);
        }

        public bool IsFull()
        {
            return (count == MAX);
        }

        public int Length()
        {
            return MAX;
        }
 
        public int Count()
        {
            return count;
        }

        internal void Push(string data)
        {
            if (count > 0)
            {
                for (int i = 0; i <= MAX - 2; i++) //inline copy fast
                    stack[i] = stack[i + 1];

                stack[MAX - 1] = data; //always same place at top, which in this case is last element of array!

                if (count < MAX) 
                    count++;
                else
                    Console.WriteLine("Max array size exceeded. Roll off the end.");
                
                Console.WriteLine("count=" + count + ", Push = " + data);
                
            }
            else
            {
                count++;
                stack[MAX - 1] = data;

                Console.WriteLine("count=" + count + ", Push = " + data);
            }

        }

        internal string Pop()
        {

            if (count > 0)
                count--;
            else 
                return stack[MAX - 1]; 

            string popValue = stack[MAX - 1]; //pop value on top of array

            Console.WriteLine("count = " + count + ", Pop = " + popValue);

            for (int i = MAX - 1; i >= 1; i--) //inline copy fast
            {
                stack[i] = stack[i - 1];
            }

            stack[0] = "empty"; //initialize bottom of stack, gets empty string

            return popValue;

        }

        internal string Peek() //traditional definition of top
        {
            return stack[MAX - 1];
        }

        internal string Top() 
        {
            return stack[MAX - 1];
        }
        internal void PrintTop()
        {
            Console.WriteLine("The topmost element of Stack is : {0}", stack[MAX - 1]);
        }
        internal void PrintStack()
        {
            Console.WriteLine("Printing Stack with {0} items:", count);
            for (int i = MAX - 1; i >= 0; i--)
            {
                Console.Write("arr[" + i + "]=" + stack[i] + ",");
            }
            Console.WriteLine();
            Console.WriteLine();
        }

    }

    public static class Program
    {
        public static void Main()
        {
            Stack s = new Stack();

            Console.WriteLine("Pop/Push Array Test");
            Console.WriteLine("Arr Len = " + s.Length().ToString());
            Console.WriteLine("Top of stack is at index = " + (s.Length()-1).ToString());
            s.PrintTop();
            s.PrintStack();
            

            s.Push("a");
            s.PrintTop();
            s.PrintStack();
            s.Push("b");
            s.PrintTop();
            s.PrintStack();
            s.Push("c");
            s.PrintTop();
            s.PrintStack();

            s.PrintStack();
            s.Push("d");
            s.PrintTop();
            s.PrintStack();
            s.Push("e");
            s.PrintStack();
            s.PrintTop();
            s.Push("f");
            s.PrintStack();
            s.PrintTop();
            s.Push("g");
            s.PrintStack();
            s.PrintTop();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();

            Console.ReadKey(); 
        }
    }
}

1st version below
This investigates using Array.Copy as Microsoft's own C# solution for Stack implementation, but is slow, compared to method above.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;using System.Diagnostics;using System.Linq;public static class Program 
{
 public static void PushStringCopyArray(ref string[] array, string pushvalue) {
  
        string[] temp = new string[array.Length];
        temp[0] = pushvalue;

        Array.Copy(array, 0, temp, 1, array.Length - 1);

        array = temp;
 }

 public static void PushStringArray(ref string[] array, string pushvalue) {
  
        int newLength = array.Length;
        string[] temp = new string[array.Length];

        temp[0] = pushvalue; //push new value on top of array

        for (int i = 0; i < array.Length - 1; i++)
        temp[i + 1] = array[i];

        array = temp;

 }
 /// <summary>
 /// Pop value from top of string[] ref array
 /// </summary>
 public static string PopStringArray(ref string[] array) {
  
        int newLength = array.Length;
        //string[] temp = new string[array.Length]; //default value is ""
        string[] temp = Enumerable.Repeat("#", array.Length).ToArray(); //set a default value

        string popvalue = array[0]; ////push new value on top of array

        for (int i = array.Length - 1; i >= 1; i--)
        temp[i - 1] = array[i];

        array = temp;

        return popvalue;
 }

 public static void PrintArray(ref string[] array) {
        for (int i = 0; i < array.Length; i++)
        Console.Write("a[" + i + "]=" + array[i] + ",");
        Console.WriteLine();
        Console.WriteLine();
 }

public static void Main() {
    Stopwatch sw = new Stopwatch();
    string[] test = new string[] {"z","a","b","c"};

    Console.WriteLine("Pop/Push Array Test");
    Console.WriteLine("Arr Len=" + test.Length);
    PrintArray(ref test);
    PushStringArray(ref test, "1st");
    PrintArray(ref test);
    PushStringArray(ref test, "2nd");
    PrintArray(ref test);
    PushStringArray(ref test, "3rd");
    PrintArray(ref test);

    sw.Start();
    PushStringArray(ref test, "4th");
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks + " ticks. WOW! For loop, but still O(n).");
    PrintArray(ref test);

    sw.Start();
    PushStringCopyArray(ref test, "5th");
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks + " ticks. Array.Copy:  I thought this would be better.");
    PrintArray(ref test);

    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
}
}

Thursday, June 20, 2019

How to install Microsoft Edge Canary "Chrome" released on Windows 7

Microsoft Edge preview build is now available on Windows 7

Get it here - https://www.microsoftedgeinsider.com/en-us/

Installer connects to internet to download entire browser. 




















Option given to import your bookmarks from another



Choose Customize Import, to select your browser in this case Chrome

























Does it remembering Passwords?

I checked that this would sign into Outlook and Gmail for my email. It pulled up the email address quickly. However, initially the passwords did not populate! So I was disappointed, however this time I just did some other work, came back to it and it appeared! 


Yes, after about 1 minute, the password was recalled, and I logged in correctly to Gmail and Outlook.com

So yah, it works! Saving a huge amount of time.


Checking About for version info 






















Looking up this error;


There must be some organization silo sync errors still.

Why do want to use this? PRIVACY

In a nutshell, much better privacy that Google Chrome. There still logs being sent back to Google plex, but not as much as Google Chrome.




Tuesday, June 18, 2019

A Pure Implementation Of Push And Pop for a String[] Array not using Queue or Stack

Here's a pure implementation of push and pop functionality of  string[] array in C Sharp, with timings. 

It does not use Queue<T> or Stack

For reference, Microsoft's own C# Stack implementation copies to an array! 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Pushes an item to the top of the stack.
        // 
        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Push"]/*' />
        public void Push(T item) {
            if (_size == _array.Length) {
                T[] newArray = new T[(_array.Length == 0) ? _defaultCapacity : 2*_array.Length];
                Array.Copy(_array, 0, newArray, 0, _size);
                _array = newArray;
            }
            _array[_size++] = item;
            _version++;
        }



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;

namespace FixedLengthStackImplementationofStackofStringsArray
{
    internal class Stack
    {
        static readonly int MAX = 3; //Max number of elements in string[] array
        int count; 

        string[] stack = new string[MAX];

        public Stack() //constructor
        {
            count = 1;           //initialize stack with how many default values 
            stack = new string[] { "x", "y", "z", "w" }; //w is ignored, no error given
        }

        public bool IsEmpty()
        {
            return (count == 0);
        }

        public bool IsFull()
        {
            return (count == MAX);
        }

        public int Length()
        {
            return MAX;
        }
 
        public int Count()
        {
            return count;
        }

        internal void Push(string data)
        {
            if (count > 0)
            {
                for (int i = 0; i <= MAX - 2; i++) //inline copy fast
                    stack[i] = stack[i + 1];

                stack[MAX - 1] = data; //always same place at top, which in this case is last element of array!

                if (count < MAX) 
                    count++;
                else
                    Console.WriteLine("Max array size exceeded. Roll off the end.");
                
                Console.WriteLine("count=" + count + ", Push = " + data);
                
            }
            else
            {
                count++;
                stack[MAX - 1] = data;

                Console.WriteLine("count=" + count + ", Push = " + data);
            }

        }

        internal string Pop()
        {

            if (count > 0)
                count--;
            else 
                return stack[MAX - 1]; 

            string popValue = stack[MAX - 1]; //pop value on top of array

            Console.WriteLine("count = " + count + ", Pop = " + popValue);

            for (int i = MAX - 1; i >= 1; i--) //inline copy fast
            {
                stack[i] = stack[i - 1];
            }

            stack[0] = "empty"; //initialize bottom of stack, gets empty string

            return popValue;

        }

        internal string Peek() //traditional definition of top
        {
            return stack[MAX - 1];
        }

        internal string Top() 
        {
            return stack[MAX - 1];
        }
        internal void PrintTop()
        {
            Console.WriteLine("The topmost element of Stack is : {0}", stack[MAX - 1]);
        }
        internal void PrintStack()
        {
            Console.WriteLine("Printing Stack with {0} items:", count);
            for (int i = MAX - 1; i >= 0; i--)
            {
                Console.Write("arr[" + i + "]=" + stack[i] + ",");
            }
            Console.WriteLine();
            Console.WriteLine();
        }

    }

    public static class Program
    {
        public static void Main()
        {
            Stack s = new Stack();

            Console.WriteLine("Pop/Push Array Test");
            Console.WriteLine("Arr Len = " + s.Length().ToString());
            Console.WriteLine("Top of stack is at index = " + (s.Length()-1).ToString());
            s.PrintTop();
            s.PrintStack();
            

            s.Push("a");
            s.PrintTop();
            s.PrintStack();
            s.Push("b");
            s.PrintTop();
            s.PrintStack();
            s.Push("c");
            s.PrintTop();
            s.PrintStack();

            s.PrintStack();
            s.Push("d");
            s.PrintTop();
            s.PrintStack();
            s.Push("e");
            s.PrintStack();
            s.PrintTop();
            s.Push("f");
            s.PrintStack();
            s.PrintTop();
            s.Push("g");
            s.PrintStack();
            s.PrintTop();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();

            Console.ReadKey(); 
        }
    }
}

1st version below
This investigates using Array.Copy as Microsoft's own C# solution for Stack implementation, but is slow, compared to method above.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;using System.Diagnostics;using System.Linq;public static class Program 
{
 public static void PushStringCopyArray(ref string[] array, string pushvalue) {
  
        string[] temp = new string[array.Length];
        temp[0] = pushvalue;

        Array.Copy(array, 0, temp, 1, array.Length - 1);

        array = temp;
 }

 public static void PushStringArray(ref string[] array, string pushvalue) {
  
        int newLength = array.Length;
        string[] temp = new string[array.Length];

        temp[0] = pushvalue; //push new value on top of array

        for (int i = 0; i < array.Length - 1; i++)
        temp[i + 1] = array[i];

        array = temp;

 }
 /// <summary>
 /// Pop value from top of string[] ref array
 /// </summary>
 public static string PopStringArray(ref string[] array) {
  
        int newLength = array.Length;
        //string[] temp = new string[array.Length]; //default value is ""
        string[] temp = Enumerable.Repeat("#", array.Length).ToArray(); //set a default value

        string popvalue = array[0]; ////push new value on top of array

        for (int i = array.Length - 1; i >= 1; i--)
        temp[i - 1] = array[i];

        array = temp;

        return popvalue;
 }

 public static void PrintArray(ref string[] array) {
        for (int i = 0; i < array.Length; i++)
        Console.Write("a[" + i + "]=" + array[i] + ",");
        Console.WriteLine();
        Console.WriteLine();
 }

public static void Main() {
    Stopwatch sw = new Stopwatch();
    string[] test = new string[] {"z","a","b","c"};

    Console.WriteLine("Pop/Push Array Test");
    Console.WriteLine("Arr Len=" + test.Length);
    PrintArray(ref test);
    PushStringArray(ref test, "1st");
    PrintArray(ref test);
    PushStringArray(ref test, "2nd");
    PrintArray(ref test);
    PushStringArray(ref test, "3rd");
    PrintArray(ref test);

    sw.Start();
    PushStringArray(ref test, "4th");
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks + " ticks. WOW! For loop, but still O(n).");
    PrintArray(ref test);

    sw.Start();
    PushStringCopyArray(ref test, "5th");
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks + " ticks. Array.Copy:  I thought this would be better.");
    PrintArray(ref test);

    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
}
}

Generic Stack Implementation with timings https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack?view=netcore-3.1

Monday, June 17, 2019

How to set-up Tidy to properly read HTML pages and encodings

After many hours of painstaking analysis, it turns out https://github.com/markbeaton/TidyManaged package cannot read a HTML string correctly.

It could be an implementation error of the Tidymanaged library or a subtle fact about .NET  4.0 Framework, strings are actually stored as UTF-16 and the conversion screws up HTML entities in particular. This was supposed to be fixed in this version of the framework and above, but perhaps persists.

So say cleanHTML contains HTML from a webpage, which was encoded in UTF-8. 

Well turns out, this gets incorrectly formatted when pushed into this method; 

TidyManaged.Document.FromString(cleanHtml);

You have to use a MemoryStream to feed UFT-8 encoding properly.

TidyManaged.Document.FromStream(HTMLinput);

 See code below to properly handle HTML documents for tidy in dotNet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
            
            //After many hours of painstaking analysis, this how to get HTML to be passed correctly 
            byte[] encodedBytesUTF8 = Encoding.UTF8.GetBytes(htmlText);
            MemoryStream HTMLinput = new MemoryStream(encodedBytesUTF8);
            
            try
            {
                //THIS DOES NOT WORK, it re-encodes the string improperly, after many hours of painstaking anaylsis
                //tidydoc = TidyManaged.Document.FromString(cleanHtml); 
                
                tidydoc = TidyManaged.Document.FromStream(HTMLinput);
            }
            catch (Exception tex)
            {
                return "tidy HTML parsing error: " + tex.Message;
            }
           
            using ( tidydoc )
            {
                //http://api.html-tidy.org/tidy/quickref_5.0.0.html
                
                tidydoc.ShowWarnings = false;
                tidydoc.Quiet = true;
                tidydoc.ForceOutput = true;
                tidydoc.OutputBodyOnly = AutoBool.Auto;

                tidydoc.DocType = TidyManaged.DocTypeMode.Omit;
                tidydoc.DropFontTags = false;
                tidydoc.UseLogicalEmphasis = false;
                tidydoc.LowerCaseLiterals = false;
                
                tidydoc.OutputXhtml = false;
                tidydoc.OutputXml = false;
                
                tidydoc.MakeClean = false;
                
                tidydoc.DropEmptyParagraphs = false;
                tidydoc.CleanWord2000 = false;

                tidydoc.QuoteAmpersands = false; //This option specifies if Tidy should output unadorned & characters as &amp;.
                tidydoc.AsciiEntities = false;   //Can be used to modify behavior of -c (--clean yes) option. If set to "yes" when using -c, &emdash;, &rdquo;, and other named character entities are downgraded to their closest ascii equivalents.
                tidydoc.PreserveEntities = true; //This option specifies if Tidy should preserve the well-formed entities as found in the input.
                tidydoc.OutputNumericEntities = true; //This option specifies if Tidy should output entities other than the built-in HTML entities (&amp;, &lt;, &gt; and &quot;) in the numeric rather than the named entity form

                tidydoc.JoinStyles = false;
                tidydoc.JoinClasses = false;
                
                tidydoc.Markup = true; //prettify open

                tidydoc.WrapAt = 0;
                tidydoc.IndentSpaces = 4;
                tidydoc.IndentBlockElements = TidyManaged.AutoBool.Yes; // this increases file size! (but makes it better to read)


                tidydoc.InputCharacterEncoding = TidyManaged.EncodingType.Utf8;
                tidydoc.CharacterEncoding = TidyManaged.EncodingType.Utf8; //For raw, Tidy will output values above 127 without translating them into entities.
                tidydoc.OutputCharacterEncoding = TidyManaged.EncodingType.Utf8;

                
                tidydoc.JoinStyles = false;
                tidydoc.MergeDivs = AutoBool.No;
                tidydoc.MergeSpans = AutoBool.No;
                tidydoc.OutputHtml = true;
                

                tidydoc.UseXmlParser = false;
                tidydoc.AddTidyMetaElement = false;
                
                
                try
                {
                    tidydoc.CleanAndRepair(); //required
                }
                catch (Exception car)
                {
                    return "tidy HTML clean && repair error: " + car.Message;
                }
                try
                {
                    cleanHtml = tidydoc.Save();
                }
                catch (Exception save)
                {
                    return "tidy HTML save error: " + save.Message;
                }
            }

C Sharp Creating a Stack using string array

Here's a pure implementation of push and pop functionality of  string[] array in C Sharp, with timings. 

It does not use Queue<T> or Stack

For reference, Microsoft's own C# Stack implementation copies to an array! 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Pushes an item to the top of the stack.
        // 
        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Push"]/*' />
        public void Push(T item) {
            if (_size == _array.Length) {
                T[] newArray = new T[(_array.Length == 0) ? _defaultCapacity : 2*_array.Length];
                Array.Copy(_array, 0, newArray, 0, _size);
                _array = newArray;
            }
            _array[_size++] = item;
            _version++;
        }



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;

namespace FixedLengthStackImplementationofStackofStringsArray
{
    internal class Stack
    {
        static readonly int MAX = 3; //Max number of elements in string[] array
        int count; 

        string[] stack = new string[MAX];

        public Stack() //constructor
        {
            count = 1;           //initialize stack with how many default values 
            stack = new string[] { "x", "y", "z", "w" }; //w is ignored, no error given
        }

        public bool IsEmpty()
        {
            return (count == 0);
        }

        public bool IsFull()
        {
            return (count == MAX);
        }

        public int Length()
        {
            return MAX;
        }
 
        public int Count()
        {
            return count;
        }

        internal void Push(string data)
        {
            if (count > 0)
            {
                for (int i = 0; i <= MAX - 2; i++) //inline copy fast
                    stack[i] = stack[i + 1];

                stack[MAX - 1] = data; //always same place at top, which in this case is last element of array!

                if (count < MAX) 
                    count++;
                else
                    Console.WriteLine("Max array size exceeded. Roll off the end.");
                
                Console.WriteLine("count=" + count + ", Push = " + data);
                
            }
            else
            {
                count++;
                stack[MAX - 1] = data;

                Console.WriteLine("count=" + count + ", Push = " + data);
            }

        }

        internal string Pop()
        {

            if (count > 0)
                count--;
            else 
                return stack[MAX - 1]; 

            string popValue = stack[MAX - 1]; //pop value on top of array

            Console.WriteLine("count = " + count + ", Pop = " + popValue);

            for (int i = MAX - 1; i >= 1; i--) //inline copy fast
            {
                stack[i] = stack[i - 1];
            }

            stack[0] = "empty"; //initialize bottom of stack, gets empty string

            return popValue;

        }

        internal string Peek() //traditional definition of top
        {
            return stack[MAX - 1];
        }

        internal string Top() 
        {
            return stack[MAX - 1];
        }
        internal void PrintTop()
        {
            Console.WriteLine("The topmost element of Stack is : {0}", stack[MAX - 1]);
        }
        internal void PrintStack()
        {
            Console.WriteLine("Printing Stack with {0} items:", count);
            for (int i = MAX - 1; i >= 0; i--)
            {
                Console.Write("arr[" + i + "]=" + stack[i] + ",");
            }
            Console.WriteLine();
            Console.WriteLine();
        }

    }

    public static class Program
    {
        public static void Main()
        {
            Stack s = new Stack();

            Console.WriteLine("Pop/Push Array Test");
            Console.WriteLine("Arr Len = " + s.Length().ToString());
            Console.WriteLine("Top of stack is at index = " + (s.Length()-1).ToString());
            s.PrintTop();
            s.PrintStack();
            

            s.Push("a");
            s.PrintTop();
            s.PrintStack();
            s.Push("b");
            s.PrintTop();
            s.PrintStack();
            s.Push("c");
            s.PrintTop();
            s.PrintStack();

            s.PrintStack();
            s.Push("d");
            s.PrintTop();
            s.PrintStack();
            s.Push("e");
            s.PrintStack();
            s.PrintTop();
            s.Push("f");
            s.PrintStack();
            s.PrintTop();
            s.Push("g");
            s.PrintStack();
            s.PrintTop();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();
            Console.WriteLine("Item popped from Stack : {0}", s.Pop());
            s.PrintStack();

            Console.ReadKey(); 
        }
    }
}

1st version below
This investigates using Array.Copy as Microsoft's own C# solution for Stack implementation, but is slow, compared to method above.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;using System.Diagnostics;using System.Linq;public static class Program 
{
 public static void PushStringCopyArray(ref string[] array, string pushvalue) {
  
        string[] temp = new string[array.Length];
        temp[0] = pushvalue;

        Array.Copy(array, 0, temp, 1, array.Length - 1);

        array = temp;
 }

 public static void PushStringArray(ref string[] array, string pushvalue) {
  
        int newLength = array.Length;
        string[] temp = new string[array.Length];

        temp[0] = pushvalue; //push new value on top of array

        for (int i = 0; i < array.Length - 1; i++)
        temp[i + 1] = array[i];

        array = temp;

 }
 /// <summary>
 /// Pop value from top of string[] ref array
 /// </summary>
 public static string PopStringArray(ref string[] array) {
  
        int newLength = array.Length;
        //string[] temp = new string[array.Length]; //default value is ""
        string[] temp = Enumerable.Repeat("#", array.Length).ToArray(); //set a default value

        string popvalue = array[0]; ////push new value on top of array

        for (int i = array.Length - 1; i >= 1; i--)
        temp[i - 1] = array[i];

        array = temp;

        return popvalue;
 }

 public static void PrintArray(ref string[] array) {
        for (int i = 0; i < array.Length; i++)
        Console.Write("a[" + i + "]=" + array[i] + ",");
        Console.WriteLine();
        Console.WriteLine();
 }

public static void Main() {
    Stopwatch sw = new Stopwatch();
    string[] test = new string[] {"z","a","b","c"};

    Console.WriteLine("Pop/Push Array Test");
    Console.WriteLine("Arr Len=" + test.Length);
    PrintArray(ref test);
    PushStringArray(ref test, "1st");
    PrintArray(ref test);
    PushStringArray(ref test, "2nd");
    PrintArray(ref test);
    PushStringArray(ref test, "3rd");
    PrintArray(ref test);

    sw.Start();
    PushStringArray(ref test, "4th");
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks + " ticks. WOW! For loop, but still O(n).");
    PrintArray(ref test);

    sw.Start();
    PushStringCopyArray(ref test, "5th");
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks + " ticks. Array.Copy:  I thought this would be better.");
    PrintArray(ref test);

    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
    Console.WriteLine(PopStringArray(ref test));
    PrintArray(ref test);
}
}

Friday, June 14, 2019

Why hackers want to get into your computer, mobile device


  • Clients are always tell me I don't need antivirus software? (Especially, Mac, FYI YTD Macs has 2209 vulnerabilities vs Windows at 900)
  • I have nothing to hide on computer or mobile device? 
  • So why do hackers want to hack my computer so badly? 


This is the same argument used by anti-vaccinators, I don't need it, I never get sick, I am strong enough. 

Well you maybe the unwitting the carrier of this virus, and the spreader of the disease to your closes family and friends. Now you have become the infection. And most likely you won't even know it, because symptoms are not showing. Active carriers who do not present signs or symptoms of disease despite infection are called asymptomatic carriers, in medical parlance.


In computer parlance, asymptomatic carriers are called zombies. 

Most of the times hackers want, you personal information, but there's another reason. They want to be able to control your computer to do things. Get enough of them together, and you can carry out attacks. An army of zombie computers can be used in DDos attacks. DDos attacks is basically flooding a service or website at the same time with millions of request, overloading that sevice or site. Each zombie computer has to come from somewhere, and guess what if you don't have antivirus you are one of them. Mac users!

Here's a real world consequence of your zombie machine; 

From SlashDot, takedown of Telegram via a DDos Attack to prevent Hong Kong protest for continued freedoms!

The distributed denial of service attack that 
hit Telegram Wednesday came from China, the secure messaging app's founder said. Pavel Durov's tweet suggested that the country's government may have done it to disrupt protests in Hong Kong. From a report:In a DDoS attack, an online service gets bombarded with traffic from networks of bots, to the point where it's overwhelmed and legit users get frozen out. In an explanation Wednesday, Telegram compared it to an "army of lemmings" jumping the line at a McDonald's and making innumerable "garbage requests." Durov said, "IP addresses coming mostly from China. Historically, all state actor-sized DDoS (200-400 Gb/s of junk) we experienced coincided in time with protests in Hong Kong (coordinated on Telegram). This case was not an exception." Tens of thousands took to Hong Kong's streets to oppose a government plan that'd allow extraditions to mainland China. People are worried that it would bring the semiautonomous former British colony under the Chinese government's thumb. These protesters relied on encrypted messaging services, which let them mask their identities from Chinese authorities, to communicate.