Posts

Showing posts from 2019

Json deserialize in golang

package  main import  (      "encoding/json"      "fmt" ) type   Message   struct  {     Name  string     Body  string     Time  int64 } func   main () {      var   m  Message      s  :=  `{"Name":"Alice","Body":"Hello","Time":1294706395881547000}`      b  := [] byte (s)      err  := json. Unmarshal (b, &m)      if  err ==  nil  {         fmt. Println (m)     } } // {Alice Hello 1294706395881547000}

Json serialize in golang

package  main import  (      "encoding/json"      "fmt" ) type   Message   struct  {     Name  string     Body  string     Time  int64 } func   main () {      m  := Message{ "Alice" ,  "Hello" ,  1294706395881547000 }      b ,  err  := json. Marshal (m)      if  err ==  nil  {          s  :=  string (b)         fmt. Println (s)     } } // {"Name":"Alice","Body":"Hello","Time":1294706395881547000}

Sort array strings in C#

Array.Sort(v,StringComparer.InvariantCulture); or, v = v.OrderBy(x => x).ToArray();

Remove last character from C# string

ans = ans.Remove(ans.Length - 1);

Using SortedDictionary in C#

In C++ we have map. In C# the equivalent of map is Dictionary. However it's not sorted. So we use SortedDictionary instead. Following is an example of mapping. using System; using System.Collections.Generic; namespace PracticeApp {     class Program     {         static void Main(string[] args)         {             var s = Console.ReadLine()?.Split('+');             var myDictionary = new SortedDictionary<int, int>();             foreach (var item in s)             {                 int a = int.Parse(item);                 myDictionary.TryGetValue(a, out var x);                 myDictionary[a] = x + 1;             }             foreach (var item in myDictionary)             {                 Console.WriteLine(item.Key+" "+item.Value);             }         }     } } Sample input: 3+2+1+1+2+2+3 Sample output: 1 2 2 3 3 2