के साथ टेलीफोन शब्द को अधिक सुन्दर तरीके से हल करने का प्रयास कर मैंने स्टैक ओवरफ़्लो को देखा है लेकिन काम करने के लिए कुछ भी नहीं मिला है। अगर मैं एक स्पष्ट रूप से स्पष्ट पोस्ट चूक गया तो मैं क्षमा चाहता हूं।रिकर्सन
मुझे एक स्कूल समस्या थी जिसमें फ़ोन नंबर लेने, सभी संभावित शब्द संयोजन प्राप्त करने और फिर इसे एक टेक्स्ट फ़ाइल में लिखना शामिल था। मैंने ऐसा किया और मेरे असाइनमेंट के लिए पूर्ण क्रेडिट मिला। मैं इसे सात नेस्टेड लूप के साथ करने में सक्षम था लेकिन यह बहुत ही सुरुचिपूर्ण नहीं है और बहुत कठोर है। मुझे दूर उड़ा दिया गया और पाठ्यपुस्तक समाधान खोजने के लिए पूरी तरह निराश सात नेस्टेड लूप था। मेरे प्रशिक्षक के पास कोई जवाब नहीं था।
मैंने कई अलग-अलग दृष्टिकोणों की कोशिश की है लेकिन मैं इसे डायल करने में सक्षम नहीं हूं। मैंने एक रिकर्सन और हत्या बिंदु की पहचान की लेकिन कभी भी इसे काम करने में सक्षम नहीं था। मैं पत्र अनुक्रमों का उत्पादन कर सकता हूं लेकिन कहीं भी नहीं होना चाहिए। मैंने अपने प्रयासों पर टिप्पणी की ताकि आप मेरी असफल विचार प्रक्रियाओं को देख सकें :) कृपया एक नज़र डालें और अगर आपके पास कोई विचार है तो मुझे बताएं।
public partial class TelephoneWorderizer : Form
{
protected Dictionary<int, IEnumerable<string>> KeyMappings = new Dictionary<int, IEnumerable<string>>();
protected string[][] ActiveLettersGroups = null;
protected List<string> Words = new List<string>();
protected List<string> RecursiveWords = new List<string>();
protected int Iteration = 0;
public TelephoneWorderizer()
{
InitializeComponent();
this.KeyMappings = this.GetKeyMappings();
}
private void btnGetWords_Click(object sender, EventArgs e)
{
string textBoxContent = textBoxNumber.Text;
int[] digits = this.GetPhoneNumbers(textBoxContent);
List<string> words = this.GetWords(digits);
using (StreamWriter writer = new StreamWriter(@"E:\words.txt"))
{
foreach (var word in words)
{
writer.WriteLine(word);
}
}
textBoxNumber.Clear();
}
private List<string> GetWords(int[] digits)
{
List<string[]> letterGroupings = new List<string[]>();
//digits array of numbers
for (int i = 0, j = digits.Length; i < j; i++)
{
int digit = digits[i];
//if the number has a letter group mapped to it
if (this.KeyMappings.ContainsKey(digit))
{
// letters mapped to a given number
letterGroupings.Add(this.KeyMappings[digit].ToArray());
}
}
this.WordMakerLoop(letterGroupings);
//this.WordMaker(letterGroupings);
return this.Words;
//return this.RecursiveWords;
}
//private void RecursionTest(string word, List<string[]> groups, int letterCtr, int groupCtr)
//{
// string[] Group = groups[groupCtr];
// word += Group[letterCtr];
// letterCtr += 1;
// if (letterCtr < Group.Length - 1)
// {
// letterCtr = 0;
// groupCtr += 1;
// // Hit bottom
// if (groupCtr == groups.Count - 1)
// {
// groupCtr -= 1;
// }
// RecursionTest(word, groups, letterCtr, groupCtr);
// }
//}
private void WordMaker(List<string[]> letterCollections)
{
string newword = "";
int numberLength = letterCollections.Count;
this.ActiveLettersGroups = letterCollections.ToArray();
string[] letterGroup = this.ActiveLettersGroups[0];
this.RecursiveGetWords(newword, 0, 0);
}
/// <summary>
///
/// </summary>
/// <param name="word"></param>
/// <param name="groupIndex"></param>
/// <param name="letterIndex"></param>
private void RecursiveGetWords(string word, int groupIndex, int letterIndex)
{
/*
*
*
*
*/
var numActiveLetterGroups = this.ActiveLettersGroups.Length;
if (this.ActiveLettersGroups.Length > 0 && this.Iteration < numActiveLetterGroups)
{
if (groupIndex < numActiveLetterGroups)
{
var letters = this.ActiveLettersGroups[groupIndex]; // Picks the a letter group ex: A, B, C
if (letterIndex < letters.Length)
{
//var letter1 = letters.Select(x =>
string letter = letters[letterIndex]; // Picks a letter from the group ex: A
word += letter;
this.RecursiveGetWords(word, groupIndex + 1, this.Iteration);
}
else
{
//this.RecursiveWords.Add(word);
//word = "";
//this.RecursiveGetWords(word, 0, 1);
}
}
else
{
this.RecursiveWords.Add(word);
word = "";
this.Iteration++;
this.RecursiveGetWords(word, 0, this.Iteration);
}
}
}
#region
private void WordMakerLoop(List<string[]> letterGroups)
{
string word = "";
// array of string[]
var newGroup = letterGroups.ToArray();
//grabs a letter group
for (int i = 0; i < newGroup.Length; i++)
{
var letterGroup1 = newGroup[i];
//grabs a letter from group 1
for (int j = 0; j < letterGroup1.Length; j++)
{
string letter1 = letterGroup1[j];
if (i + 1 < newGroup.Length)
{
var letterGroup2 = newGroup[i + 1];
//grabs a letter from group 2
for (int k = 0; k < letterGroup2.Length; k++)
{
string letter2 = letterGroup2[k];
if (i + 2 < newGroup.Length)
{
var letterGroup3 = newGroup[i + 2];
//grabs a letter from group 3
for (int l = 0; l < letterGroup3.Length; l++)
{
string letter3 = letterGroup3[l];
if (i + 3 < newGroup.Length)
{
var letterGroup4 = newGroup[i + 3];
//grabs a letter from group 4
for (int m = 0; m < letterGroup4.Length; m++)
{
string letter4 = letterGroup4[m];
if (i + 4 < newGroup.Length)
{
var letterGroup5 = newGroup[i + 4];
//grabs a letter from group 5
for (int n = 0; n < letterGroup5.Length; n++)
{
string letter5 = letterGroup5[n];
if (i + 5 < newGroup.Length)
{
var letterGroup6 = newGroup[i + 5];
//grabs a letter from group 6
for (int o = 0; o < letterGroup6.Length; o++)
{
string letter6 = letterGroup6[o];
if (i + 6 < newGroup.Length)
{
var letterGroup7 = newGroup[i + 6];
//grabs a letter from group 6
for (int p = 0; p < letterGroup7.Length; p++)
{
string letter7 = letterGroup7[p];
word = letter1 + letter2 + letter3 + letter4 + letter5 + letter6 + letter7;
this.Words.Add(word);
word = "";
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
// Sanitizes input text and converts the string into and arra of int
private int[] GetPhoneNumbers(string content)
{
int[] phoneNumbers = null;
string cleanString = this.SanitizeString(content);
int numbers;
if (Int32.TryParse(cleanString, out numbers))
{
//phoneNumbers = this.GetIntArray(numbers).OfType<int>().ToList();
phoneNumbers = this.GetIntArray(numbers);
}
return phoneNumbers;
}
// Removes potential unwanted characters from the phone number
private string SanitizeString(string content)
{
content = content.Replace("-", "");
content = content.Replace("(", "");
content = content.Replace(")", "");
return content;
}
//breaks a number into an array of its individual digits
private int[] GetIntArray(int num)
{
List<int> listOfInts = new List<int>();
while (num > 0)
{
listOfInts.Add(num % 10);
num = num/10;
}
listOfInts.Reverse();
return listOfInts.ToArray();
}
//gets the mappings for the numerical values
private Dictionary<int, IEnumerable<string>> GetKeyMappings()
{
List<string> alphabet = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
Dictionary<int, IEnumerable<string>> mappings = new Dictionary<int, IEnumerable<string>>();
for (int i = 0; i < 10; i++)
{
string[] letters = null;
switch (i)
{
case 0:
case 1:
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 8:
letters = alphabet.Take(3).ToArray();
mappings.Add(i, letters);
alphabet.RemoveRange(0, 3);
break;
case 7:
case 9:
letters = alphabet.Take(4).ToArray();
mappings.Add(i, letters);
alphabet.RemoveRange(0, 4);
break;
default:
break;
}
}
return mappings;
}
#endregion
}
मुझे इस बात पर जोर देना चाहिए कि स्कूल के असाइनमेंट उन लोगों के लिए संदेह में है। मैं यह बेहतर और अधिक कुशल करना चाहता हूं। अगर मैं मदद करता हूं तो मैं अपनी परियोजना को गिटहब पर पोस्ट कर सकता हूं।
ऐसा कुछ लगता है जो [codegolf.se] – ThiefMaster
के लिए एक बहुत अच्छा फिट होगा, समस्या को देखने के लिए बहुत बढ़िया सवाल, और आपके तर्क स्पष्ट रूप से बताए गए हैं, साथ ही साथ कोड फ़ॉर्म में इसे हल करने के आपके प्रयास भी हैं। अक्सर अधिक जानकारी के लिए पूछने वाली टिप्पणियां होती हैं या व्यक्ति को प्रश्न पूछने के लिए पूछने के लिए कहा जाता है, यह आपके पास बहुत ही बढ़िया है। इसके लिए +1। – Crippledsmurf