Iterate over pairs in a list (circular fashion) in Python
def pairs(lst): i = iter(lst) first = prev = item = i.next() for item in i: yield prev, item prev = item yield item, first Works on any non-empty sequence, no indexing required.
def pairs(lst): i = iter(lst) first = prev = item = i.next() for item in i: yield prev, item prev = item yield item, first Works on any non-empty sequence, no indexing required.
That’s because num is a Char, i.e. the resulting values are the ascii value of that char. This will do the trick: val txt = “82389235” val numbers = txt.map { it.toString().toInt() } The map could be further simplified: map(Character::getNumericValue)
[input[i:i+n] for i in range(0, len(input), n)] # Use xrange in py2k where n is the length of a chunk. Since you don’t define what might happen to the final element of the new list when the number of elements in input is not divisible by n, I assumed that it’s of no importance: with … Read more
This code Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename) async { final file = await File(filename); String contents = await file.readAsString(); User user = User.fromJson(json.decode(contents)); String name = user.NameLast + “, ” + user.NameFirst; print(name); l.add(name); } return l; } returns the list l … Read more
There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance: var list = [‘a’, ‘b’, ‘c’, ‘d’]; list.shuffle(); print(‘$list’); The collection package comes with a shuffle function/extension that also supports specifying a sub range to … Read more
The following is based on my previous answer to “Remove duplicates in list (Prolog)” and on this previous answer to the question “Prolog union for A U B U C”. list_item_subtracted_count0_count/5 is derived from list_item_subtracted/3. list_counts/2 is derived from list_setB/2, which were both defined here. list_item_subtracted_count0_count([], _, [], N,N). list_item_subtracted_count0_count([A|As], E, Bs1, N0,N) :- if_(A … Read more
Wadler, School of Haskell, LYAH, HaskellWiki, Quora and many more describe the list monad. Compare: (=<<) :: Monad m => (a -> m b) -> m a -> m b for lists with concatMap :: (a -> [b]) -> [a] -> [b] for m = []. The regular (>>=) bind operator has the arguments flipped, … Read more
Here is a pure version using dif/2 which implements sound inequality. dif/2 is offered by B-Prolog, YAP-Prolog, SICStus-Prolog and SWI-Prolog. firstdup(E, [E|L]) :- member(E, L). firstdup(E, [N|L]) :- non_member(N, L), firstdup(E, L). member(E, [E|_L]). member(E, [_X|L]) :- member(E, L). non_member(_E, []). non_member(E, [F|Fs]) :- dif(E, F), non_member(E, Fs). The advantages are that it can also … Read more
How about (defun get-file (filename) (with-open-file (stream filename) (loop for line = (read-line stream nil) while line collect line)))