there is code in php

$words=Array(); // Заполнить звенья for($i=0; $i<count($tmp); $i++) { if ($tmp[$i+1]!='') { $words[$tmp[$i]][]=$tmp[$i+1]; $words[$tmp[$i]]=array_unique($words[$tmp[$i]]); } } 

where tmp is an array of words.

It is very necessary to transfer this business to C # ...

  • five
    > It is very necessary to transfer this business to> C # ... so learn c #, why should I help you if you don't know the basics? - johniek_comp
  • Ok, I will ask more specifically. warning (do not know php *) Why do we declare a Word as a simple array, but can we refer to it as a two-dimensional array? - SOUR
  • 3
    @SOUR, in general, this code is nonsense. PHP should produce an ahtung appeal as a two-dimensional one, and the fact that inside the cycles, under the drugs, was written, meaningless and mercilessly. - Sh4dow
  • one
    @zenith, for example, $ tmp [$ i] = 'foo', $ tmp [$ i + 1] = 'bar' if ($ tmp [$ i + 1]! = '') // Warning: Undefined index .. . $ words [$ tmp [$ i]] [] = $ tmp [$ i + 1]; // add to the array NULL (lol) 'bar' (string !!!); PHP: Warning: ...; and even if it works, $ words [$ tmp [$ i]] = array_unique ($ words [$ tmp [$ i]]); // $ words ['foo'] = array_unique (array ('bar')) Total from $ tmp = array ('a', 'b', 'c', 'd'): $ words = array (' a '=> array (' b '),' b '=> array (' c '),' c '=> array (' d ')); Is it not a drug?) - Sh4dow
  • 2
    Not dope, just bydlokod. > if ($tmp[$i+1]!='') The author apparently invented isset($tmp[$i+1]) > $words[$tmp[$i]][]=$tmp[$i+1]; But this is a regular way of working with multidimensional arrays . T.ch. here even without a notice will work. > $words[$tmp[$i]]=array_unique($words[$tmp[$i]]); Also popular govnokod: excessive resource-intensive operation inside the loop. It would be correct: foreach ($ tmp as $ i => $ w) {if (isset ($ tmp [$ i + 1])) {$ words [$ w] [] = $ tmp [$ i + 1] ; }} $ words = array_map ("array_unique", $ words) - Ilya Pirogov

1 answer 1

I am writing here the normal code in php

 $words = array(); for ($n = 0; $n < count($tmp)-1; $n++) { // идем по всем словам, кроме последнего $w1 = $tmp[$n]; // это и следующее $w2 = $tmp[$n+1]; // только для наглядности if (!is_array($words[$w1])) // если массива тут еще нет, $words[$w1] = array(); // создаем if (!in_array($w2, $words[$w1])) // если слова еще нет в списке, $words[$w1][] = $w2; // добавляем } ?><pre><? print_r($words); // любуемся ?></pre><? 

On the example of the link gives the result

 Array ( [ИЗ-ЗА] => Array ( [0] => ЛЕСА [1] => ГОР [ЛЕСА] => Array ( [0] => ИЗ-ЗА ) [ГОР] => Array ( [0] => ЕДЕТ ) [ЕДЕТ] => Array ( [0] => ДЕДУШКА ) [ДЕДУШКА] => Array ( [0] => ЕГОР ) [ЕГОР] => Array ( [0] => САМ ) [САМ] => Array ( [0] => НА ) [НА] => Array ( [0] => ЛОШАДКЕ [1] => КОРОВКЕ [2] => ТЕЛЯТКАХ [3] => КОЗЛЯТКАХ ) [ЛОШАДКЕ] => Array ( [0] => ЖЕНА ) [ЖЕНА] => Array ( [0] => НА ) [КОРОВКЕ] => Array ( [0] => ДЕТИ ) [ДЕТИ] => Array ( [0] => НА ) [ТЕЛЯТКАХ] => Array ( [0] => ВНУКИ ) [ВНУКИ] => Array ( [0] => НА ) ) 

Passing the baton to sisarperam :)

PS: translated into uppercase, because in php keys are case sensitive, "Because"! = "Because"

  • Thanks for chewing on! I will calmly translate this. ps Plus in the future :) - SOUR
  • one
    For sporting interest. So on python it would look like: words = defaultdict (set) for cur, prev in zip (tmp [: - 1], tmp [1:]): words [prev] .add (cur) - Ilya Pirogov
  • Sharpe solution :) var hTable = new Dictionary <string, Dictionary <int, string >> (); for (int i = 0; i <str.Count () - 1; i ++) {string w1 = str [i]; string w2 = str [i + 1]; if (hTable.ContainsKey (w1) == false) hTable [w1] = new Dictionary <int, string> (); if (hTable [w1] .ContainsValue (w2) == false) hTable [w1] .Add (i, w2); } - SOUR
  • And why as the value of the Dictionary<int, string> ? It would be enough StringCollection . Or even better to use HashSet<string> , this should be faster and you will not need to check ContainsValue() : var dic = new Dictionary <string, HashSet <string >> (); for (int i = 0; i <str.Count () - 1; i ++) {if (dic.TryGetValue (str [i], out hashSet)) {hashSet.Add (str [i + 1]); } else {dic [str [i]] = new HashSet <string> ({str [i + 1]}); }} And for the str variable, it would be more suitable LinkedList<string> - Ilya Pirogov
  • Labu has already passed, but for the future I will consider, thank you :) - SOUR