From df1002303e50dfd54d7c682367e3cfcb8c593c1f Mon Sep 17 00:00:00 2001
From: wook <rudollee@gmail.com>
Date: Thu, 3 May 2018 03:51:04 +0900
Subject: [PATCH] complete

---
 UnicodeAndPermutation/Form1.Designer.cs       |  85 ++++++++++++-
 UnicodeAndPermutation/Form1.cs                | 120 ++++++++++++++++++
 UnicodeAndPermutation/Form1.resx              | 120 ++++++++++++++++++
 .../UnicodeAndPermutation.csproj              |   3 +
 4 files changed, 325 insertions(+), 3 deletions(-)
 create mode 100644 UnicodeAndPermutation/Form1.resx

diff --git a/UnicodeAndPermutation/Form1.Designer.cs b/UnicodeAndPermutation/Form1.Designer.cs
index 1b7d51f..17009ca 100644
--- a/UnicodeAndPermutation/Form1.Designer.cs
+++ b/UnicodeAndPermutation/Form1.Designer.cs
@@ -28,13 +28,92 @@
 		/// </summary>
 		private void InitializeComponent()
 		{
-			this.components = new System.ComponentModel.Container();
+			this.ListboxWords = new System.Windows.Forms.ListBox();
+			this.lbl2 = new System.Windows.Forms.Label();
+			this.lbl1 = new System.Windows.Forms.Label();
+			this.lblSum = new System.Windows.Forms.Label();
+			this.txtWord = new System.Windows.Forms.TextBox();
+			this.buttonPorcess = new System.Windows.Forms.Button();
+			this.SuspendLayout();
+			// 
+			// ListboxWords
+			// 
+			this.ListboxWords.FormattingEnabled = true;
+			this.ListboxWords.Location = new System.Drawing.Point(31, 67);
+			this.ListboxWords.Name = "ListboxWords";
+			this.ListboxWords.Size = new System.Drawing.Size(112, 316);
+			this.ListboxWords.TabIndex = 9;
+			// 
+			// lbl2
+			// 
+			this.lbl2.AutoSize = true;
+			this.lbl2.Location = new System.Drawing.Point(9, 67);
+			this.lbl2.Name = "lbl2";
+			this.lbl2.Size = new System.Drawing.Size(13, 13);
+			this.lbl2.TabIndex = 6;
+			this.lbl2.Text = "2";
+			// 
+			// lbl1
+			// 
+			this.lbl1.AutoSize = true;
+			this.lbl1.Location = new System.Drawing.Point(9, 38);
+			this.lbl1.Name = "lbl1";
+			this.lbl1.Size = new System.Drawing.Size(13, 13);
+			this.lbl1.TabIndex = 7;
+			this.lbl1.Text = "1";
+			// 
+			// lblSum
+			// 
+			this.lblSum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
+			this.lblSum.Location = new System.Drawing.Point(31, 38);
+			this.lblSum.Name = "lblSum";
+			this.lblSum.Size = new System.Drawing.Size(112, 20);
+			this.lblSum.TabIndex = 8;
+			// 
+			// txtWord
+			// 
+			this.txtWord.Location = new System.Drawing.Point(31, 12);
+			this.txtWord.Name = "txtWord";
+			this.txtWord.Size = new System.Drawing.Size(112, 20);
+			this.txtWord.TabIndex = 5;
+			this.txtWord.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtWord_KeyDown);
+			// 
+			// buttonPorcess
+			// 
+			this.buttonPorcess.Location = new System.Drawing.Point(149, 10);
+			this.buttonPorcess.Name = "buttonPorcess";
+			this.buttonPorcess.Size = new System.Drawing.Size(75, 23);
+			this.buttonPorcess.TabIndex = 4;
+			this.buttonPorcess.Text = "Permute";
+			this.buttonPorcess.UseVisualStyleBackColor = true;
+			this.buttonPorcess.Click += new System.EventHandler(this.buttonPorcess_Click);
+			// 
+			// Form1
+			// 
+			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
 			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-			this.ClientSize = new System.Drawing.Size(800, 450);
-			this.Text = "Form1";
+			this.ClientSize = new System.Drawing.Size(237, 398);
+			this.Controls.Add(this.ListboxWords);
+			this.Controls.Add(this.lbl2);
+			this.Controls.Add(this.lbl1);
+			this.Controls.Add(this.lblSum);
+			this.Controls.Add(this.txtWord);
+			this.Controls.Add(this.buttonPorcess);
+			this.Name = "Form1";
+			this.Text = "Unicode and Permutation";
+			this.ResumeLayout(false);
+			this.PerformLayout();
+
 		}
 
 		#endregion
+
+		private System.Windows.Forms.ListBox ListboxWords;
+		private System.Windows.Forms.Label lbl2;
+		private System.Windows.Forms.Label lbl1;
+		private System.Windows.Forms.Label lblSum;
+		private System.Windows.Forms.TextBox txtWord;
+		private System.Windows.Forms.Button buttonPorcess;
 	}
 }
 
diff --git a/UnicodeAndPermutation/Form1.cs b/UnicodeAndPermutation/Form1.cs
index fac8c18..290c9ba 100644
--- a/UnicodeAndPermutation/Form1.cs
+++ b/UnicodeAndPermutation/Form1.cs
@@ -4,6 +4,7 @@ using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
+using System.Net;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
@@ -12,9 +13,128 @@ namespace UnicodeAndPermutation
 {
 	public partial class Form1 : Form
 	{
+		private List<string> Words = new List<string>();
 		public Form1()
 		{
 			InitializeComponent();
+			txtWord.Focus();
 		}
+
+		#region Show Permuted real words
+		private async void ShowRealwords()
+		{
+			Words.Clear();
+			Permute(txtWord.Text.ToCharArray());
+			var wordFiltered = new List<string>();
+			foreach (var word in Words)
+			{
+				var json = await GetWordsFromDic(word);
+				if (json.Length - word.Length > 100)
+				{
+					wordFiltered.Add(word);
+				}
+			}
+
+			if (wordFiltered.Any())
+			{
+				ListboxWords.DataSource = wordFiltered;
+			}
+		}
+		#endregion
+
+		#region Sum Unicode Numbers
+		private void SumUnicodeNumbers(string word)
+		{
+			int n = 0;
+			foreach (var c in word)
+			{
+				n += (int)c;
+			}
+
+			lblSum.Text = n.ToString();
+		}
+		#endregion
+
+		#region Permutation
+		/// <summary>
+		/// 1. 길이가 할당되지 않은 경우, 단어의 길이를 계산
+		/// 2. 새롭게 조합된 string을 collection에 추가
+		/// 3. 재귀적으로 Permute 함수를 호출하여 새로운 조합을 찾아냄
+		/// </summary>
+		/// <param name="chars"></param>
+		/// <param name="k"></param>
+		/// <param name="length"></param>
+		private void Permute(char[] chars, int k = 0, int length = -1)
+		{
+			if (length == -1)
+			{
+				length = chars.Length;
+			}
+
+			if (k == length)
+			{
+				Words.Add(new string(chars));
+				Words = Words.Distinct().ToList();
+				return;
+			}
+
+			for (int i = k; i < length; i++)
+			{
+				Swap(ref chars[k], ref chars[i]);
+				Permute(chars, k + 1, length);
+				Swap(ref chars[k], ref chars[i]);
+			}
+		}
+
+		/// <summary>
+		/// XOR Opertation을 이용하여 두 character를 치환
+		/// </summary>
+		/// <param name="a"></param>
+		/// <param name="b"></param>
+		private void Swap(ref char a, ref char b)
+		{
+			if (a == b) return;
+
+			a ^= b;
+			b ^= a;
+			a ^= b;
+		}
+		#endregion
+
+		#region Get word information via open api
+		/// <summary>
+		/// https://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&pretty=false&phrase= ?
+		/// 비동기적으로 단어의 뜻을 json 파일로 받아서 return
+		/// </summary>
+		/// <param name="word"></param>
+		/// <returns>Json format</returns>
+		private async Task<string> GetWordsFromDic(string word)
+		{
+			using (var client = new WebClient())
+			{
+				var uri = new Uri($"https://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&pretty=false&phrase={word}");
+				return await client.DownloadStringTaskAsync(uri);
+			}
+		}
+		#endregion
+
+		#region Events
+		private void buttonPorcess_Click(object sender, EventArgs e)
+		{
+			SumUnicodeNumbers(txtWord.Text);
+			ShowRealwords();
+		}
+
+		private void txtWord_KeyDown(object sender, KeyEventArgs e)
+		{
+			switch (e.KeyData)
+			{
+				case Keys.Enter:
+					SumUnicodeNumbers(txtWord.Text);
+					ShowRealwords();
+					break;
+			}
+		}
+		#endregion
 	}
 }
diff --git a/UnicodeAndPermutation/Form1.resx b/UnicodeAndPermutation/Form1.resx
new file mode 100644
index 0000000..1af7de1
--- /dev/null
+++ b/UnicodeAndPermutation/Form1.resx
@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>
\ No newline at end of file
diff --git a/UnicodeAndPermutation/UnicodeAndPermutation.csproj b/UnicodeAndPermutation/UnicodeAndPermutation.csproj
index 81cc0aa..5e27ac1 100644
--- a/UnicodeAndPermutation/UnicodeAndPermutation.csproj
+++ b/UnicodeAndPermutation/UnicodeAndPermutation.csproj
@@ -53,6 +53,9 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <EmbeddedResource Include="Form1.resx">
+      <DependentUpon>Form1.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Properties\Resources.resx">
       <Generator>ResXFileCodeGenerator</Generator>
       <LastGenOutput>Resources.Designer.cs</LastGenOutput>