Related
Given an integer array, find all the consecutive subsequences of alternating odd and even numbers.
Also print the total number of such subsequences.
All the subsequences should be unique.
The numbers in the list may or may not be unique.
Example:
array = [1,2,5]
output1: [[1], [1,2], [2], [2,5], [5], [1,2,5]]
output2: 6
My Code:
res = []
lst = [1,2,5]
for i in range(len(lst)-1):
res.append([lst[i]])
if abs(lst[i] - lst[i+1]) % 2 == 1:
res.append([lst[i], lst[i+1]])
print(res)
Output: [[1], [1, 2], [2], [2, 5]]
How can I get the remaining subsequences?
You care about duplicates:
def alternating_sublists(xs: list[int]) -> list[list[int]]:
results = []
for i in range(len(xs)):
if [xs[i]] not in results:
results.append([xs[i]])
for j in range(i+1, len(xs)):
if (xs[j] - xs[j-1]) % 2 != 0:
if xs[i:j+1] not in results:
results.append(xs[i:j+1])
else:
break
return results
print(list(alternating_sublists([1, 2, 5])))
print(list(alternating_sublists([1, 2, 2, 2, 1])))
print(list(alternating_sublists([1, 2, 3, 2, 3, 2, 1])))
Output:
[[1], [1, 2], [1, 2, 5], [2], [2, 5], [5]]
[[1], [1, 2], [2], [2, 1]]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 2], [1, 2, 3, 2, 3], [1, 2, 3, 2, 3, 2], [1, 2, 3, 2, 3, 2, 1], [2], [2, 3], [2, 3, 2], [2, 3, 2, 3], [2, 3, 2, 3, 2], [2, 3, 2, 3, 2, 1], [3], [3, 2], [3, 2, 3], [3, 2, 3, 2], [3, 2, 3, 2, 1], [2, 3, 2, 1], [3, 2, 1], [2, 1]]
It's not extremely efficient (there's many lookups of lists already in the result). Depending on the application you may want a more complex data structure to save expensive 'list in large list' tests.
The basic logic is this:
each sequence has to start at some index, so try sequences starting at all possible indices for i in range(len(xs)):
the sequence with length 1 always meets your rule, so add it if it wasn't there yet
the other sequences start at index i and end at index i+1 or greater for j in range(i+1, len(xs)):
break from the loop whenever the modulo is 0 for the last two items in list you're about to add, since this sequence doesn't meet the rule, and longer ones wouldn't either.
Slightly faster and shorter, using tuples internally, but essentially the same:
def alternating_sublists2(xs: list[int]) -> list[list[int]]:
results = set()
for i in range(len(xs)):
results.add((xs[i],))
for j in range(i+1, len(xs)):
if (xs[j] - xs[j-1]) % 2 != 0:
results.add(tuple(xs[i:j+1]))
else:
break
return [list(t) for t in results]
shorter as the previous if statements are now internal to set.add()
faster because looking up tuples is faster than looking up strings, and testing membership of a set is faster than testing membership of a list
not quite as fast as you might like, since it then has to convert the result back to a list of lists, to get the result you required.
However, no guarantees on the order of the sublists in the result, so this is no good if you need the sublists in the order they are first found.
Here's a recursive solution to the problem. It iterates the elements of the list, adding the results from recursing the balance of the list when there is a change from odd to even between the current element and the next:
def odd_even(list, start=None):
result = []
for i, val in enumerate(list):
if start is None or i == 0:
if [val] not in result:
result.append([val])
if len(list) > i+1 and (list[i+1] - val) % 2 == 1:
for res in odd_even(list[i+1:], val):
if [val] + res not in result:
result = result + [[val] + res]
return result
print(odd_even([1, 2, 5]))
print(odd_even([1, 2, 2, 2, 1]))
print(odd_even([1, 2, 3, 2, 3, 2, 1]))
Output:
[[1], [1, 2], [1, 2, 5], [2], [2, 5], [5]]
[[1], [1, 2], [2], [2, 1]]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 2], [1, 2, 3, 2, 3], [1, 2, 3, 2, 3, 2], [1, 2, 3, 2, 3, 2, 1], [2], [2, 3], [2, 3, 2], [2, 3, 2, 3], [2, 3, 2, 3, 2], [2, 3, 2, 3, 2, 1], [3], [3, 2], [3, 2, 3], [3, 2, 3, 2], [3, 2, 3, 2, 1], [2, 3, 2, 1], [3, 2, 1], [2, 1]]
Your accepted output is silly, because it's obvious that every subsequence of a "good" sequence is also "good" and there's no need to enumerate them all. Let's concentrate on finding longest alternating sequences:
def split(a):
buf = [a[0]]
for i in range(1, len(a)):
if a[i] % 2 != a[i - 1] % 2:
buf.append(a[i])
else:
yield buf
buf = [a[i]]
if buf:
yield buf
test = [1, 2, 5, 7, 3, 8, 9, 9, 10, 11]
result = list(split(test))
# [[1, 2, 5], [7], [3, 8, 9], [9, 10, 11]]
To get your expected answer, take each list from the result and generate all sublists of it. This is another, much simpler task.
This looks like a gray code sequence with additional twist:
https://en.wikipedia.org/wiki/Gray_code
Code:
import math
def powerOf2(k):
if k == 0:
return 1
else:
return 2*powerOf2(k-1)
def gray_encode(n):
return n ^ n >> 1
def count_required_sequence(lst):
n = len(lst)
sequence_nr = powerOf2(n)
results = []
results_sequence = -1
for i in range(sequence_nr):
gray = gray_encode(i)
gray_r = list("{:>010b}".format(gray))[::-1]
#print(gray_r)
count = sum(el == "1" for el in gray_r)
if count > 1:
results_sequence += 1
results.append(list())
for k in range(len(gray_r)):
if k < len(gray_r)-1:
if gray_r[k] == "1" and gray_r[k+1] == "1":
if abs(lst[k] - lst[k+1]) % 2 == 1:
results[results_sequence].append(lst[k])
results[results_sequence].append(lst[k+1])
is_there_count1 = results.count(list(set(results[results_sequence])))
results[results_sequence] = list(set(results[results_sequence]))
is_there_count = results.count(results[results_sequence])
if is_there_count > 1 or is_there_count1 > 1:
index = results.index(list(set(results[results_sequence])))
results.pop(results_sequence)
results_sequence -= 1
elif count == 1 :
results_sequence += 1
results.append(list())
pos = [index for index,value in enumerate(gray_r) if value == "1" ]
results[results_sequence].append(lst[pos[0]])
results = (list(filter(lambda a: a != [], results)))
print("results: {}".format(results))
# Driver code
if __name__ == "__main__" :
# lst = [ 1, 2, 5, 6, 7];
lst = [ 1, 2, 5 ];
count_required_sequence(lst);
Output:
results: [[1], [1, 2], [2], [2, 5], [1, 2, 5], [5]]
Change 010b to a bigger number is len(lst) is bigger then 10
gray_r = list("{:>010b}".format(gray))[::-1]
I would like to create a random dictionary starting from the following array:
list = [1,2,3,4,5]
In particular, I would like to have as keys of the dictionary all the elements of the array and as corresponding keys, randomly pick some values from that array except the value that corresponds to the key
An example of expected output should be something like:
Randomdict = {1: [2, 4], 2: [1,3,5] 3: [2] 4: [2,3,5] 5: [1,2,3,4]}
And last but not least all keys should have at least 1 value
It can be done with the random module and comprehensions:
from random import sample, randrange
d = {i: sample([j for j in lst if i != j], randrange(1, len(lst) - 1))
for i in lst}
If you first use random.seed(0) for reproducible data, you will get:
{1: [3, 2], 2: [4, 3], 3: [2, 4], 4: [3, 1]}
{1: [3], 2: [1], 3: [4, 1], 4: [1, 3]}
{1: [3, 2], 2: [3, 4], 3: [4], 4: [2, 3]}
Something like this? Might needs some tweaks
from random import randrange, sample
q = [1, 2, 3, 4, 5]
a = {}
for i in q:
n = randrange(len(q)-1)
a[i] = sample(q, n)
print(a)
For example, let's say I have a list:
lst = [1, 2, 3, 3, 4, 3, 5, 6]
Is there any function that returns all the indexes of the 3s?(which would return [2, 3, 5])
I've changed the list in order to build a dictionary of all items that appeared more than once.
from collections import Counter
lst = [1, 2, 3, 3, 4, 3, 5, 2, 6]
# only values that appears more than once
c = Counter(lst) - Counter(set(lst))
res = {}
for i, elem in enumerate(lst):
if elem in c:
item = res.get(elem)
if item:
item.append(i)
else:
res[elem] = [i]
print(res)
output :
{2: [1, 7], 3: [2, 3, 5]}
A better approach is to use defaultdict :
from collections import defaultdict
lst = [1, 2, 3, 3, 4, 3, 5, 2, 6]
d = defaultdict(list)
for i, elem in enumerate(lst):
d[elem].append(i)
print({k: v for k, v in d.items() if len(v) > 1})
output :
{2: [1, 7], 3: [2, 3, 5]}
You can simply use function enumerate(lst)
it returns elements index_number and value.
for example
lst[0] = 1
the case above index_number is 0
and value is 1
so you can just use enumerate function for returning index number using if condition
(returns it when the value is 3)
lst = [1, 2, 3, 3, 4, 3, 5, 6]
lst_result = [i for i, v in enumerate(lst) if v == 3]
print(lst_result)
your outcome will be
[2,3,5]
I was trying to figure out a solution for a similar problem and this is what I came up with.
def locate_index(item_name, list_name):
"""Determine the indexes of an item if in a list."""
locations = []
for i in range(len(list_name)):
if list_name[i] == item_name:
locations.append(list_name.index(item_name, i, len(list_name)))
print(locations)
Example:
lst = [1, 2, 3, 3, 4, 3, 5, 6]
list2 = [1, 2, 3, 3, 4, 3, 5, 6, 6, 6, 6]
locate_index(3, lst) ---- a)
locate_index(6, list2) ---- b)
Output
a)
[2, 3, 5]
b)
[7, 8, 9, 10]
Given an array of integers, I want to determine the number of distinct groups of integers wherein the values ascend.
Given array myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 2]
There are 4 distinct groups of integers wherein the values ascend. i.e.
[1, 2, 3, 4], [2, 2, 3], [1, 2] and [2]
Can some with experience guide me on how to achieve this in python?
Another possible answer (also assuming [1, 4] is supposed to be there instead of [2]):
In [14]: def find_ascending_groups(my_list):
...: groups = []
...: current_group = [my_list[0]]
...: for i in range(1, len(my_list)):
...: if current_group[-1] <= my_list[i]:
...: current_group.append(my_list[i])
...: else:
...: if len(current_group) > 1:
...: groups.append(current_group)
...: current_group = [my_list[i]]
...: if len(current_group) > 1:
...: groups.append(current_group)
...: print(groups)
...:
In [15]: find_ascending_groups(myList)
[[1, 2, 3, 4], [2, 2, 3], [1, 2], [1, 4]]
Here is one way to do it:
group = []
list_of_groups = []
myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 2]
for i in range(len(myList)):
if i == len(myList)-1:
if len(group) > 0:
group.append(myList[i])
list_of_groups.append(group)
else:
if myList[i] <= myList[i+1]:
group.append(myList[i])
else:
group.append(myList[i])
if len(group) > 1:
list_of_groups.append(group)
group = []
print(list_of_groups)
Result with different test cases -
myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 2]
Output: [[1, 2, 3, 4], [2, 2, 3], [1, 2], [1, 4]
myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 5]
Output: [[1, 2, 3, 4], [2, 2, 3], [1, 2], [1, 4, 5]]
myList = [1, 2]
Output: [[1, 2]]
myList = [1, 2, 3, 4]
Output: [[1, 2, 3, 4]]
myList = [5, 2, 3, 4]
Output: [[2, 3, 4]]
myList = [5, 2]
Output: []
Assuming [1, 4] is supposed to be there instead of [2], you can do something like this:
n [48]: ascending_ints = []
...: t = []
...: start_idx = 0
...: while start_idx < len(myList) - 1:
...: first = True
...: for end_idx in range(start_idx + 1, len(myList), 1):
...: if myList[end_idx] >= myList[start_idx]:
...: if first:
...: t.append(myList[start_idx])
...: first = False
...: t.append(myList[end_idx])
...: else:
...: if t:
...: ascending_ints.append(t)
...: t = []
...: start_idx = end_idx
...: break
...: start_idx += 1
...:
In [49]: ascending_ints
Out[49]: [[1, 2, 3, 4], [2, 2, 3], [1, 2], [1, 4]]
This just makes the start_idx start at 0, end_idx begin at 1 and increment both. If myList[end_idx] >= myList[start_idx] then it appends it to t. If it's the first time it's happened for this iteration, it needs to also include the value at start_idx. Once the condition is no longer true, check if anything is in t, if it is, append it to ascending_ints, reset t, set start_idx = end_idx and break the for-loop.
Here is another solution that is more efficient in Python.
def find_ascending_groups(l):
groups = []
last_ascending = (l[0] <= l[1])
l.append(l[-1] - 1) # Add an additional element to make sure the last group is appended
start = 0
for i in range(1, len(l)):
is_ascending = (l[i - 1] <= l[i])
if is_ascending != last_ascending:
if is_ascending:
start = i - 1 # l[i - 1] is a local minimum, set group start
else:
groups.append(l[start:i]) # l[i - 1] is a local maximum, generate group
last_ascending = is_ascending
l.pop() # Remove added element
return groups
EDIT
I wrote a small split_when function which was recently merged into the more-itertools library. split_when takes an iterable and a function. The function takes all pairs of consecutive elements and returns whether the iterable should be splitted between those elements:
>>> myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 2]
>>> import more_itertools
>>> list(more_itertools.split_when(myList, lambda x, y: x > y))
[[1, 2, 3, 4], [3], [2, 2, 3], [1, 2], [1, 4], [2]]
# 4>3 3>2 3>1 2>1 4>2
PREVIOUS ANSWER
For the record, you can use a fold, that is functools.reduce in Python:
>>> myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 2]
>>> import functools
>>> functools.reduce(lambda acc, x: acc[:-1] + [acc[-1]+[x]] if acc and x >= acc[-1][-1] else acc + [[x]], myList, [])
[[1, 2, 3, 4], [3], [2, 2, 3], [1, 2], [1, 4], [2]]
Not very readable though, but if you write the lambda as a distinct function, you get a better idea of what's happening:
>>> def f(acc, x):
... if acc: # there is at least one closed group
... *prev, cur = acc # closed groups and current group
... *_, y = cur # y is the last element of the current group
... if x >= y: # if ascending
... return prev + [cur + [x]] # return with x added to the current group
...
... return acc + [[x]] # otherwise, return with x in a new group
...
>>> functools.reduce(f, myList, [])
[[1, 2, 3, 4], [3], [2, 2, 3], [1, 2], [1, 4], [2]]
I am trying to create a function that returns a dictionary that describes a pascal triangle.
For example,
pascal(3)
would give me
{1: [1], 2: [1,1], 3: [1,2,1]}
I currently know how to create a function that returns the list of elements
in a certain row for n equal to or greater than 2
def pascal(n):
if n == 0:
return {}
elif n == 1:
return {1:[1]}
else:
row = [1] + [list(pascal(n-1))[i] + list(pascal(n-1))[i+1] for i in range(n-2)] + [1]
return row
With this function,
pascal(3)
gives me
[1,2,1]
Is it possible to change my function in such a way that
pascal(3)
returns the desired result of
{1: [1], 2: [1,1], 3: [1,2,1]}
Any help would be appreciated.
You can use zip to pair the returning list from the recursive call with the same list but at one index apart, padded with 0:
def pascal(n):
if n == 1:
return {1: [1]}
p = pascal(n - 1)
p[n] = list(map(sum, zip([0] + p[n - 1], p[n - 1] + [0])))
return p
so that:
for n in range(1, 6):
print(pascal(n))
outputs:
{1: [1]}
{1: [1], 2: [1, 1]}
{1: [1], 2: [1, 1], 3: [1, 2, 1]}
{1: [1], 2: [1, 1], 3: [1, 2, 1], 4: [1, 3, 3, 1]}
{1: [1], 2: [1, 1], 3: [1, 2, 1], 4: [1, 3, 3, 1], 5: [1, 4, 6, 4, 1]}
If you are open to an iterative solution, I cooked up up the following.
from itertools import chain
def pascal(n):
pad = (0,)
result = {1: [1]}
for i in range(2, n + 1):
previous = list(chain(pad, result[i - 1], pad))
result[i] = [sum(pair) for pair in zip(previous, previous[1:])]
return result
Demo:
>>> for n in range(1, 6):
...: print(pascal(n))
...:
...:
{1: [1]}
{1: [1], 2: [1, 1]}
{1: [1], 2: [1, 1], 3: [1, 2, 1]}
{1: [1], 2: [1, 1], 3: [1, 2, 1], 4: [1, 3, 3, 1]}
{1: [1], 2: [1, 1], 3: [1, 2, 1], 4: [1, 3, 3, 1], 5: [1, 4, 6, 4, 1]}
With a bit more lines, but also better memory efficiency:
from itertools import chain, tee
def pascal(n):
pad = (0,)
result = {1: [1]}
for i in range(2, n + 1):
previous = chain(pad, result[i - 1], pad)
c1, c2 = tee(previous)
next(c2)
result[i] = [sum(pair) for pair in zip(c1, c2)]
return result
Lastly, having a dict with consecutive integer keys is not very useful, you could just use a list into which you index starting at 0. Final solution:
def pascal(n):
pad = (0,)
result = [[1]]
for i in range(1, n):
previous = chain(pad, result[i - 1], pad)
c1, c2 = tee(previous)
next(c2)
result.append([sum(pair) for pair in zip(c1, c2)])
return result
Demo:
>>> for n in range(1, 6):
...: print(pascal(n))
...:
[[1]]
[[1], [1, 1]]
[[1], [1, 1], [1, 2, 1]]
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
edit: improved efficiency by not creating two tuples per iteration, instantiating pad once is enough.
I would be careful using recursion like that - it's very inefficient. You are calling the function twice in a loop in the function body. It's important to think about how many times the function will be called to evaluate certain values of n.
It's obvious that when n = 1, the function is called once.
When n = 2, the function is called once, and then the function calls itself twice for a total of 3 calls.
For n = 3 the function is called once, and then the functions calls itself twice, and then these two calls each call the function four times... So that's 11 calls.
So the number of calls is numCalls = 1 + 2 + 2*4 + 2*4*6 + ... + 2*4*6*...*2n)
This sequence grows extremely fast... When n is 20 that's 1308293051285742128434781 Calls
Recursion isn't always evil, you just have to be careful, this solution calls itself n times:
def genPascalDict(nMax):
if nMax < 2:
return {1: [1]}
else:
pascalDict = genPascalDict(nMax - 1)
lastRow = pascalDict[nMax - 1]
pascalDict[nMax] = [1] + [lastRow[n + 1] + lastRow[nMax - n - 2] for n in range(nMax - 2)] + [1]
return pascalDict
You can make it fast while building your dict as a side effect:
_cache = {}
def pascal(n):
try:
result = _cache[n]
except KeyError:
if n == 0:
result = []
elif n == 1:
result = [1]
else:
previous = pascal(n - 1)
result = [1] + [previous[i] + previous[i + 1] for i in range(n - 2)] + [1]
_cache[n] = result
return result
pascal(500)
print(_cache)
You don't need to compute pascal(n) more than once: it's not like it changes. So remember what your final answer was by storing it in a caching dictionary, said dictionary being what you actually wanted in the first place.
This takes about .08s to build the dictionary on my laptop.
You can use a closure with recursion:
def pascal(n:int) -> dict:
def _pascal(_s, _e, _last, _base={1:[1], 2:[1, 1]}):
return _last if not _e-_s else _pascal(_s+1, _e, {**_last, **{_s:_base.get(_s, [1, *[_last[_s-1][i]+_last[_s-1][i+1] for i in range(len(_last)-1)], 1])}})
return _pascal(1, n+1, {})
print(pascal(3))
Output:
{1: [1], 2: [1, 1], 3: [1, 2, 1]}