static void Main()
    {
        Console.WriteLine("Please enter in a single line a sequence of integers separated by an interval: ");
        string line = Console.ReadLine();

        string[] elements = line.Split(' ');

        List <int> seq = new List <int>();

        foreach (string el in elements)
        {
            seq.Add(int.Parse(el));
        }

        Set <int> set = new Set <int>();

        set.AddMany(seq);

        set.RemoveAll(setElement => (seq.FindAll(seqElement => seqElement == setElement).Count % 2 == 1));

        seq.RemoveAll(seqElement => !set.Contains(seqElement));

        Console.WriteLine("The new sequence with removed elements that occur odd number of times is: ");
        Console.WriteLine(String.Join(" ", seq));
    }
Beispiel #2
0
        public void SerializeStrings()
        {
            Set <string> d = new Set <string>();

            d.Add("foo");
            d.Add("world");
            d.Add("hello");
            d.Add("elvis");
            d.Add("elvis");
            d.Add(null);
            d.Add("cool");
            d.AddMany(new string[] { "1", "2", "3", "4", "5", "6" });
            d.AddMany(new string[] { "7", "8", "9", "10", "11", "12" });

            Set <string> result = (Set <string>)InterfaceTests.SerializeRoundTrip(d);

            InterfaceTests.TestReadWriteCollectionGeneric((ICollection <string>)result, new string[] { "1", "2", "3", "4", "5", "6", "cool", "elvis", "hello", "foo", "world", null, "7", "8", "9", "10", "11", "12" }, false);
        }
Beispiel #3
0
        public void AddMany()
        {
            Set <string> set1 = new Set <string>(StringComparer.InvariantCultureIgnoreCase);

            set1.Add("foo");
            set1.Add("Eric");
            set1.Add("Clapton");
            string[] s_array = { "FOO", "x", "elmer", "fudd", "Clapton", null };
            set1.AddMany(s_array);

            InterfaceTests.TestReadWriteCollectionGeneric(set1, new string[] { null, "Clapton", "elmer", "Eric", "FOO", "fudd", "x" }, false);
        }
    static void Main()
    {
        Console.WriteLine("Please enter in a single line a sequence of integers separated by an interval: ");
        string line = Console.ReadLine();
        string[] elements = line.Split(' ');

        List<int> seq = new List<int>();
        foreach (string el in elements)
        {
            seq.Add(int.Parse(el));
        }

        Set<int> set = new Set<int>();
        set.AddMany(seq);

        set.RemoveAll(setElement => (seq.FindAll(seqElement => seqElement == setElement).Count % 2 == 1));

        seq.RemoveAll(seqElement => !set.Contains(seqElement));

        Console.WriteLine("The new sequence with removed elements that occur odd number of times is: ");
        Console.WriteLine(String.Join(" ", seq));
    }
Beispiel #5
0
    static void Main()
    {
        Console.Write("Enter number of elements to read: ");
        string line = Console.ReadLine();
        int    N;

        while (!(int.TryParse(line, out N) && N > 0))
        {
            Console.Write("Please enter a positive integer: ");
            line = Console.ReadLine();
        }
        Console.WriteLine();

        int[] numbers = new int[N];
        for (int idx = 0; idx < N; idx++)
        {
            Console.Write("Enter number {0}: ", idx + 1);
            line = Console.ReadLine();
            while (!(int.TryParse(line, out numbers[idx])))
            {
                Console.Write("Please enter an integer: ");
                line = Console.ReadLine();
            }
            Console.WriteLine();
        }

        Console.Write("{" + String.Join(", ", numbers) + "} -> ");

        int majMinOccurs = N / 2 + 1;

        /*Solution 1 - Predicates*/

        List <int> numbersList = numbers.ToList <int>();
        Set <int>  numbersSet  = new Set <int>();

        numbersSet.AddMany(numbersList);

        try
        {
            int majorant = numbersSet.First(setElement => (numbersList.FindAll(
                                                               listElement => listElement == setElement).Count >= majMinOccurs));
            Console.WriteLine(majorant);
        }
        catch
        {
            Console.WriteLine("No majorant!");
        }

        Console.WriteLine();

        /*Solution 2 - Dictionary*/

        //Dictionary<int, int> occurrences = new Dictionary<int, int>();

        //for (int i = 0; i < N; i++)
        //{
        //    if (!occurrences.Keys.Contains(numbers[i]))
        //    {
        //        occurrences.Add(numbers[i], 1);
        //    }
        //    else
        //    {
        //        occurrences[numbers[i]]++;
        //    }
        //}

        //bool hasMajorant = false;

        //foreach (KeyValuePair<int, int> pair in occurrences)
        //{
        //    if (pair.Value >= majMinOccurs)
        //    {
        //        Console.WriteLine(pair.Key);
        //        hasMajorant = true;
        //        break;
        //    }
        //}

        //if (!hasMajorant)
        //{
        //    Console.WriteLine("No majorant!");
        //}

        //Console.WriteLine();
    }
Beispiel #6
0
		/// <summary>
		/// Does the real work of morphing the specified word.
		/// </summary>
		/// <param name="word">The word.</param>
		/// <param name="prev">The previous word.</param>
		/// <param name="next">The next word.</param>
		/// <param name="trace">The trace.</param>
		/// <param name="selectTraceMorphs"></param>
		/// <returns>All valid word synthesis records.</returns>
		ICollection<WordSynthesis> MorphAndLookupToken(string word, string prev, string next, TraceManager trace, string[] selectTraceMorphs)
		{
			PhoneticShape input;
			try
			{
				// convert the word to its phonetic shape; it could throw a missing phonetic shape exception
				input = SurfaceStratum.CharacterDefinitionTable.ToPhoneticShape(word, ModeType.ANALYSIS);
			}
			catch (MissingPhoneticShapeException mpse)
			{
				var me = new MorphException(MorphException.MorphErrorType.INVALID_SHAPE, this,
					string.Format(HCStrings.kstidInvalidWord, word, SurfaceStratum.CharacterDefinitionTable.ID, mpse.Position+1,
									word.Substring(mpse.Position), mpse.PhonemesFoundSoFar));
				me.Data["shape"] = word;
				me.Data["charDefTable"] = SurfaceStratum.CharacterDefinitionTable.ID;
				me.Data["position"] = mpse.Position;
				me.Data["phonemesFoundSoFar"] = mpse.PhonemesFoundSoFar;
				throw me;
			}

			var inputAnalysis = new WordAnalysis(input, SurfaceStratum);

			if (trace != null)
				trace.BeginAnalyzeWord(word, inputAnalysis);

			var candidates = new Set<WordSynthesis>();
			var inAnalysis = new Set<WordAnalysis>();
			var outAnalysis = new Set<WordAnalysis>();
			inAnalysis.Add(inputAnalysis);

			// Unapply rules
			for (int i = m_strata.Count - 1; i >= 0; i--)
			{
				outAnalysis.Clear();
				foreach (WordAnalysis wa in inAnalysis)
				{
					if (trace != null)
						trace.BeginUnapplyStratum(m_strata[i], wa);

					foreach (WordAnalysis outWa in m_strata[i].Unapply(wa, trace, selectTraceMorphs, candidates))
					{
						// promote each analysis to the next stratum
						if (i != 0)
							outWa.Stratum = m_strata[i - 1];

						if (trace != null)
							trace.EndUnapplyStratum(m_strata[i], outWa);

						outAnalysis.Add(outWa);
					}
				}

				inAnalysis.Clear();
				inAnalysis.AddMany(outAnalysis);
			}

			var allValidSyntheses = new Set<WordSynthesis>();
			// Apply rules for each candidate entry
			foreach (WordSynthesis candidate in candidates)
			{
				var inSynthesis = new Set<WordSynthesis>();
				var outSynthesis = new Set<WordSynthesis>();
				for (int i = 0; i < m_strata.Count; i++)
				{
					// start applying at the stratum that this lex entry belongs to
					if (m_strata[i] == candidate.Root.Stratum)
						inSynthesis.Add(candidate);

					outSynthesis.Clear();
					foreach (WordSynthesis cur in inSynthesis)
					{
						if (trace != null)
							trace.BeginApplyStratum(m_strata[i], cur);

						foreach (WordSynthesis outWs in m_strata[i].Apply(cur, trace))
						{
							// promote the word synthesis to the next stratum
							if (i != m_strata.Count - 1)
								outWs.Stratum = m_strata[i + 1];

							if (trace != null)
								trace.EndApplyStratum(m_strata[i], outWs);

							outSynthesis.Add(outWs);
						}
					}

					inSynthesis.Clear();
					inSynthesis.AddMany(outSynthesis);
				}

				foreach (WordSynthesis ws in outSynthesis)
				{
					if (ws.IsValid(trace))
						allValidSyntheses.Add(ws);
				}
			}

			var results = new Set<WordSynthesis>();
			// sort the resulting syntheses according to the order of precedence of each allomorph in
			// their respective morphemes
			var sortedSyntheses = new List<WordSynthesis>(allValidSyntheses);
			sortedSyntheses.Sort();

			WordSynthesis prevValidSynthesis = null;
			bool allFreeFluctuation = true;
			foreach (WordSynthesis cur in sortedSyntheses)
			{
				// enforce the disjunctive property of allomorphs by ensuring that this word synthesis
				// has the highest order of precedence for its allomorphs while also allowing for free
				// fluctuation
				if (prevValidSynthesis == null || AreAllomorphsNondisjunctive(cur, prevValidSynthesis))
				{
					AddResult(word, results, cur, trace);
					allFreeFluctuation = true;
				}
				else if (allFreeFluctuation && CheckFreeFluctuation(cur, prevValidSynthesis))
				{
					AddResult(word, results, cur, trace);
				}
				else
				{
					allFreeFluctuation = false;
				}
				prevValidSynthesis = cur;
			}
			return results;
		} // end MorphAndLookupToken
Beispiel #7
0
		/// <summary>
		/// Does the real work of morphing the specified word.
		/// </summary>
		/// <param name="word">The word.</param>
		/// <param name="prev">The previous word.</param>
		/// <param name="next">The next word.</param>
		/// <param name="trace">The trace.</param>
		/// <returns>All valid word synthesis records.</returns>
		ICollection<WordSynthesis> MorphAndLookupToken(string word, string prev, string next, out WordAnalysisTrace trace)
		{
			// convert the word to its phonetic shape
			PhoneticShape input = SurfaceStratum.CharacterDefinitionTable.ToPhoneticShape(word, ModeType.ANALYSIS);
			// if word contains invalid segments, the char def table will return null
			if (input == null)
			{
				MorphException me = new MorphException(MorphException.MorphErrorType.INVALID_SHAPE, this,
					string.Format(HCStrings.kstidInvalidWord, word, SurfaceStratum.CharacterDefinitionTable.ID));
				me.Data["shape"] = word;
				me.Data["charDefTable"] = SurfaceStratum.CharacterDefinitionTable.ID;
				throw me;
			}

			// create the root of the trace tree
			trace = new WordAnalysisTrace(word, input.Clone());

			Set<WordSynthesis> candidates = new Set<WordSynthesis>();
			Set<WordAnalysis> inAnalysis = new Set<WordAnalysis>();
			Set<WordAnalysis> outAnalysis = new Set<WordAnalysis>();
			inAnalysis.Add(new WordAnalysis(input, SurfaceStratum, trace));

			// Unapply rules
			for (int i = m_strata.Count - 1; i >= 0; i--)
			{
				outAnalysis.Clear();
				foreach (WordAnalysis wa in inAnalysis)
				{
					if (m_traceStrataAnalysis)
					{
						// create the stratum analysis input trace record
						StratumAnalysisTrace stratumTrace = new StratumAnalysisTrace(m_strata[i], true, wa.Clone());
						wa.CurrentTrace.AddChild(stratumTrace);
					}
					foreach (WordAnalysis outWa in m_strata[i].Unapply(wa, candidates))
					{
						// promote each analysis to the next stratum
						if (i != 0)
							outWa.Stratum = m_strata[i - 1];

						if (m_traceStrataAnalysis)
							// create the stratum analysis output trace record for the output word synthesis
							outWa.CurrentTrace.AddChild(new StratumAnalysisTrace(m_strata[i], false, outWa.Clone()));

						outAnalysis.Add(outWa);
					}
				}

				inAnalysis.Clear();
				inAnalysis.AddMany(outAnalysis);
			}

			Set<WordSynthesis> allValidSyntheses = new Set<WordSynthesis>();
			// Apply rules for each candidate entry
			foreach (WordSynthesis candidate in candidates)
			{
				Set<WordSynthesis> inSynthesis = new Set<WordSynthesis>();
				Set<WordSynthesis> outSynthesis = new Set<WordSynthesis>();
				for (int i = 0; i < m_strata.Count; i++)
				{
					// start applying at the stratum that this lex entry belongs to
					if (m_strata[i] == candidate.Root.Stratum)
						inSynthesis.Add(candidate);

					outSynthesis.Clear();
					foreach (WordSynthesis cur in inSynthesis)
					{
						if (m_traceStrataSynthesis)
						{
							// create the stratum synthesis input trace record
							StratumSynthesisTrace stratumTrace = new StratumSynthesisTrace(m_strata[i], true, cur.Clone());
							cur.CurrentTrace.AddChild(stratumTrace);
						}
						foreach (WordSynthesis outWs in m_strata[i].Apply(cur))
						{
							// promote the word synthesis to the next stratum
							if (i != m_strata.Count - 1)
								outWs.Stratum = m_strata[i + 1];

							if (m_traceStrataSynthesis)
								// create the stratum synthesis output trace record for the output analysis
								outWs.CurrentTrace.AddChild(new StratumSynthesisTrace(m_strata[i], false, outWs.Clone()));

							outSynthesis.Add(outWs);
						}
					}

					inSynthesis.Clear();
					inSynthesis.AddMany(outSynthesis);
				}

				foreach (WordSynthesis ws in outSynthesis)
				{
					if (ws.IsValid)
						allValidSyntheses.Add(ws);
				}
			}

			Set<WordSynthesis> results = new Set<WordSynthesis>();
			// sort the resulting syntheses according to the order of precedence of each allomorph in
			// their respective morphemes
			List<WordSynthesis> sortedSyntheses = new List<WordSynthesis>(allValidSyntheses);
			sortedSyntheses.Sort();

			WordSynthesis prevValidSynthesis = null;
			foreach (WordSynthesis cur in sortedSyntheses)
			{
				// enforce the disjunctive property of allomorphs by ensuring that this word synthesis
				// has the highest order of precedence for its allomorphs, also check that the phonetic
				// shape matches the original input word
				if ((prevValidSynthesis == null || !cur.Morphs.SameMorphemes(prevValidSynthesis.Morphs))
					&& SurfaceStratum.CharacterDefinitionTable.IsMatch(word, cur.Shape))
				{
					if (m_traceSuccess)
						// create the report a success output trace record for the output analysis
						cur.CurrentTrace.AddChild(new ReportSuccessTrace(cur));
					// do not add to the result if it has the same root, shape, and morphemes as another result
					bool duplicate = false;
					foreach (WordSynthesis ws in results)
					{
						if (cur.Duplicates(ws))
						{
							duplicate = true;
							break;
						}
					}
					if (!duplicate)
					{
						results.Add(cur);
					}
				}
				prevValidSynthesis = cur;
			}
			return results;
		}
Beispiel #8
0
    static void Main()
    {
        Console.Write("Enter number of elements to read: ");
        string line = Console.ReadLine();
        int N;
        while (!(int.TryParse(line, out N) && N > 0))
        {
            Console.Write("Please enter a positive integer: ");
            line = Console.ReadLine();
        }
        Console.WriteLine();

        int[] numbers = new int[N];
        for (int idx = 0; idx < N; idx++)
        {
            Console.Write("Enter number {0}: ", idx + 1);
            line = Console.ReadLine();
            while (!(int.TryParse(line, out numbers[idx])))
            {
                Console.Write("Please enter an integer: ");
                line = Console.ReadLine();
            }
            Console.WriteLine();
        }

        Console.Write("{" + String.Join(", ", numbers) + "} -> ");

        int majMinOccurs = N / 2 + 1;

        /*Solution 1 - Predicates*/

        List<int> numbersList = numbers.ToList<int>();
        Set<int> numbersSet = new Set<int>();

        numbersSet.AddMany(numbersList);

        try
        {
            int majorant = numbersSet.First(setElement => (numbersList.FindAll(
                listElement => listElement == setElement).Count >= majMinOccurs));
            Console.WriteLine(majorant);
        }
        catch
        {
            Console.WriteLine("No majorant!");
        }

        Console.WriteLine();

        /*Solution 2 - Dictionary*/

        //Dictionary<int, int> occurrences = new Dictionary<int, int>();

        //for (int i = 0; i < N; i++)
        //{
        //    if (!occurrences.Keys.Contains(numbers[i]))
        //    {
        //        occurrences.Add(numbers[i], 1);
        //    }
        //    else
        //    {
        //        occurrences[numbers[i]]++;
        //    }
        //}

        //bool hasMajorant = false;

        //foreach (KeyValuePair<int, int> pair in occurrences)
        //{
        //    if (pair.Value >= majMinOccurs)
        //    {
        //        Console.WriteLine(pair.Key);
        //        hasMajorant = true;
        //        break;
        //    }
        //}

        //if (!hasMajorant)
        //{
        //    Console.WriteLine("No majorant!");
        //}

        //Console.WriteLine();
    }