Give me highest element - python

I got a list like these:
List1: [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
I want a new list, which should content the highest number, before it starts again with 1.
List_new: [9, 29, 15]
I tried this:
List_new = []
for i in range(len(List1)):
j = List1[i]
if j + 1 == '1':
List_new += [j]
else:
continue
print(j)
But I got an empty list back.

Simply with built-in only libs:
from itertools import groupby
result = [max(group) for r, group in groupby(your_list, lambda x: x == 1) if not r]

def max_of_sublists(megalist):
maxitem = 0
for item in megalist:
if item == 1 and maxitem:
yield maxitem
maxitem = 0
if maxitem < item:
maxitem = item
yield maxitem
biglist=[1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
print([x for x in max_of_sublists(biglist)])

Your code has a few issues. Here's a version that works.
list1 = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
list2 = []
for i in range(len(list1)-1):
if list1[i+1] == 1:
list2.append(list1[i])
list2.append(list1[-1]) # adds the last element
This outputs:
>>> list2
[9, 29, 15]

Here is a simple for loop that will answer your question:
List_new = [List1[0]] # initialize with first element
for i in List1[1:]: # simply iterate over list elements, not indices
if i != 1 and i > List_new[-1]:
List_new[-1] = i # current element is the new maximum
elif i == 1:
List_new.append(i) # encountered a 1, start looking for new maximum
See inline comments for explanations.

This problem can be implemented in a one liner using python modules as in the very elegant solution suggested by Andrey. However, if you would like to follow on the logic, check out this solution.
def max_values_between_ones(numbers):
max_values = []
max_value = None
for i in range(len(numbers)):
if numbers[i] == 1:
if max_value != None:
max_values.append(max_value)
max_value = None
# max_value is None when they were no values != 1 before this 1
else:
if max_value != None:
# this part was missing in your code, to get the max value
# you should be comparing the current value with the max value so far
max_value = max(numbers[i], max_value)
else:
# set max_value to any not 1 value
max_value = numbers[i]
# if the list didn't end with 1, add the last max_value
if max_value != None:
max_values.append(max_value)
return max_values
numbers = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
max_values = max_values_between_ones(numbers)
print(max_values)
>> [9, 29, 15]

Like this:
l = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
pos = [item for item in range(0, len(l)) if l[item] == 1]
new_list = []
for n in range(len(pos)):
if n != len(pos) - 1:
new_list.append(l[pos[n]:pos[n+1]])
else:
new_list.append(l[pos[n]:])
print map(lambda x: max(x), new_list)

List1 = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
maxi = 0
List2 = []
for i in range(0,len(List1)):
if maxi < List1[i]:
maxi = List1[i]
if (i == len(List1)-1 or List1[i] == 1) and maxi > 1:
List2.append(maxi)
maxi = 0
print List2

Related

'int' object is not subscriptable error in recursion

I'm trying to make a recursive function where it takes all odd numbers in a list, order them in an ascending way, and then comes the even numbers but descending.
For example:
lst = [1, 2, 3, 4, 6, 11, 13, 14, 15, 17, 18] would return [1, 3, 11, 13, 15, 17, 18, 14, 6, 4, 2]
This is my code:
def REC_fun(lst):
lst2 , lst3 = [] , []
if len(lst) == 1:
return lst[0]
temp = REC_fun(lst[1:])
if temp[0]%2 == 1:
lst2 += [temp[0]]
else:
lst3 += [temp[0]]
return lst2 + lst3[::-1]
ls = [1, 2, 3, 6, 11, 13, 14, 15, 17, 18]
REC_fun(ls)
The problem is probably with temp, I want to check each time if the first number of the list is even or odd and add it accordingly to the empty lists (lst2 and lst3), but I don't know how.. I keep getting this error
~\AppData\Local\Temp/ipykernel_16808/3849482424.py in REC_fun(lst)
7 if len(lst) == 1:
8 return lst[0]
----> 9 temp = make_pyramid(lst[1:])
10 if temp[0]%2 == 1:
11 lst2 += [temp[0]]
~\AppData\Local\Temp/ipykernel_16808/3849482424.py in REC_fun(lst)
8 return lst[0]
9 temp = make_pyramid(lst[1:])
---> 10 if temp[0]%2 == 1:
11 lst2 += [temp[0]]
12 else:
TypeError: 'int' object is not subscriptable
EDIT -
I changed the first return from return lst[0] to return lst and the output is now [18].
Here is your Answer:
lst2, lst3 = [],[]
def REC_fun(lst):
global lst2 ,lst3
if lst[0]%2 == 1:
lst2 += [lst[0]]
else:
lst3 += [lst[0]]
if len(lst) == 1:
return lst
lst = REC_fun(lst[1:])
return lst2 + lst3[::-1]
ls = [1, 2, 3, 4, 6, 11, 13, 14, 15, 17, 18]
print(REC_fun(ls))

Flip half of a list and append it to itself

Im trying to flip half of the elements inside of a list and then spend it onto itself, for example,
if I pass the list [1, 8, 7, 2, 9, 18, 12, 0] into it, I should get [1, 0, 7, 18, 9, 2, 12, 8] out, here is my current code
def flip_half(list):
for i in range(0,len(list)):
if i % 2 != 0:
listflip = list[i]
print(listflip)
return list
You can directly assign slices in python. So if you can determine the slice you want to assign from and to, you can directly change the elements. This assign from every other element in reverse l[::-2] to every other element starting with the second: l[1::2] :
l = [1, 8, 7, 2, 9, 18, 12, 0]
l[1::2] = l[::-2]
print(l)
#[1, 0, 7, 18, 9, 2, 12, 8]
If you wanted to do it in a loop:
def flip_half(list):
out = []
for i in range(0,len(list)):
if i % 2 != 0:
out.append(list[-i])
else:
out.append(list[i])
return out
print(flip_half([1, 8, 7, 2, 9, 18, 12, 0]))
If you need that elements with odd indexes has reverse order then one of next solutions (l - is your list):
Slices
l[1::2] = l[1::2][::-1]
List comprehension
l = [l[i] if i % 2 == 0 else l[-(i + len(l)%2)] for i in range(len(l))]
Function
def flip_half(lst):
res= []
for i in range(len(lst)):
if i % 2 != 0:
res.append(lst[-(i + len(lst) % 2)])
else:
res.append(lst[i])
return res
l = flip_half(l)
Generator function
def flip_half(lst):
for i in range(len(lst)):
if i % 2 != 0:
yield lst[-(i + len(lst) % 2)]
else:
yield lst[i]
l = list(flip_half(l))

Given an array of N elements. The task is to check if the elements in the array are first strictly increasing then strictly decreasing

Example:
Input: arr[] = [-3, 9, 11, 20, 17, 5, 1]
Output: YES
Input: arr[] = [5, 6, 7, 8, 9, 10, 1, 2, 11]
Output: NO
I tried with method but its throwing error as list index out of range.
And also do i need to increment 'i' in every iteration?
def checkType(arr,n):
for i in range(len(arr)):
#print(i)
if (arr[i]<=arr[i+1]): #and (arr[n-1]<=arr[n-2]):
print('YES')
else:
print('NO')
if __name__ == "__main__" :
arr=[-3, 9, 11, 20, 17, 5, 1]
n=len(arr)
checkType(arr,n)
IndexError: list index out of range
You could do it like this:-
arr = [-3, 9, 11, 20, 17, 5, 1]
if sorted(arr) == arr or sorted(arr, reverse=True) == arr:
print('The array is ordered - either ascending or descending')
else:
print('The array is not ordered')
Try this code below:
arr = [-3, 9, 11, 20, 17, 5, 1]
lst = [x - y for x, y in zip(arr, arr[1:])]
y = 1
value = 'YES'
for i in lst:
if ((i < 0) != y) and (y == 1):
y = 0
elif ((i < 0) != y) and (y == 0):
value = 'NO'
break
print(value)
Output:
YES

Append duplicate items at the end of list whithout changing the order

I am new to python and was trying to Append duplicate items at the end of list whithout changing the order
testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56]
def duplicate(alist):
p = len(alist)
duplicate = False
for i in range(0, p):
for j in range (i + 1, p):
if alist[i] == alist[j]:
b = alist.index(alist[j])
a = alist.pop(b)
alist.append(a)
p -= 1
duplicate = True
print alist
if duplicate == False:
print "No duplicate item found"
duplicate(testlist)
OUTPUT : [32, 8, 1, 17, 5, 2, 42, 13, 56, 1, 2]
DESIRED OUTPUT : [1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2]
Any help what wrong I am doing here
I think that in this case the creation of a new list is more efficient and clear in comparison with the permutations in the original list:
testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56]
def duplicate(alist):
filtered, duplicates = [], []
for element in alist:
if element in filtered:
duplicates.append(element)
continue
filtered.append(element)
if not duplicates:
print "No duplicate item found"
return alist
return filtered + duplicates
new_list = duplicate(testlist)
print new_list
You may use the Collections module to get OrderedDict for maintaining the order of elements.
The technique we use here is to create a dictionary to store the number of occurrences of each element in the array and use the dict to lookup the number of occurrences for later use.
In the for loop we look up if there is already element present in the dict using the get method. If true then we increase the counter else initialize the counter to be zero.
import collections
lst = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56]
# Create a dictionary storing the the number of occurences
occurences_dict = collections.OrderedDict()
for i in lst:
occurences_dict[i] = occurences_dict.get(i, 0) + 1
final = occurences_dict.keys() + [k for k, v in occurences_dict.items() if v>1]
print final
>>> [1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2]
I solved it this way. I also made some changes to make the code a bit more Pythonic.
test_list = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56]
def duplicate(a_list):
list_length = len(a_list)
duplicate = False
checked = []
for i in range(list_length):
if a_list.count(a_list[i]) > 1:
if a_list[i] not in checked:
duplicate = True
a_list.append(a_list[i])
checked.append(a_list[i])
if duplicate == False:
print("No duplicate item found")
return None
return a_list
print(duplicate(test_list))
Instead of checking values, compare on index.
Please check this code:
testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56,32]
print("Original list - ",testlist)
tmp_list = []
exclude =[]
for i in range(len(testlist)):
if i == testlist.index(testlist[i]):
tmp_list.append(testlist[i])
else:
exclude.append(testlist[i])
tmp_list.extend(exclude)
testlist = tmp_list
print("Updated list - ",testlist)
Output:
C:\Users\dinesh_pundkar\Desktop>python c.py
Original list - [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56, 32]
Updated list - [1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2, 32]
C:\Users\dinesh_pundkar\Desktop>
I have developed a algo which extends the duplicates in such a way that if duplicate contains duplicate we are extends them to end also
eg - input - [1,1,2,2,3,3,4,5,2,5]
output - [1, 2, 3, 4, 5, 2, 3, 2] - (Solution 2)
others ouput - [1, 2, 3, 4, 5, 2, 2, 3] where 2 repeats (Solution 1)
Solution 1 -
def remove_extend(l):
stack = []
duplicates = []
for i in l:
if i not in stack:
stack.append(i)
elif i in stack:
duplicates.append(i)
stack.extend(duplicates)
return stack
l = [1,2,2,2,3,3,4,5]
ans = remove_extend(l)
print(l)
print(ans)
Solution 2
def check_dict(d):
if d == {}:
return False
# stack = []
for k,v in d.items():
if v == 0:
del d[k]
return True
def add_to_stack(stack, d):
for k,v in d.items():
if v>=1:
stack.append(k)
d[k] -= 1
return [stack,d]
def duplicates_extend2(ls):
d = {}
stack = []
for i in ls:
if i in d:
d[i] += 1
else:
d[i] = 1
print(d)
if_dict = check_dict(d)
while if_dict:
ans = add_to_stack(stack, d)
stack = ans[0]
d = ans[1]
if_dict = check_dict(d)
return stack
ls = [1,1,2,2,3,3,4,5,2,5]
ans = duplicates_extend2(ls)
print(ans)

Selection sort in Python yields no output

I'm trying to run selection sort in python, this is the code that I'm using
def main(list):
input_array = [12, 9, 13, 7, 3, 19, 6, 5]
output_array = selection_sort(input_array)
print(output_array)
def selection_sort(param):
for i in range(0, (len(param) - 1)):
min = i
for j in range(i + 1, len(param)):
if param[min] < param[j]:
min = j
if min != i:
temp = param[i]
param[i] = param[min]
param[min] = temp
return param
The output that I get is
Process finished with exit code 0
I'm using PyCharm as the idea, if that's of any consequence.
input_array should be a list, you are making it a set
input_array = [12, 9, 13, 7, 3, 19, 6, 5]
don't use the variable name list, it is the name for the built-in list
don't use min as a variable name, it is the name for the built-in function min
you don't need an argument for your main method here
you are not calling your main method, call it after the definition of selection_sort
change the line
minimum, i, j, temp = 0
to
minimum, i, j, temp = 0, 0, 0, 0
This will sort in ascending order:
def selection_sort(my_list):
for j in range(len(my_list)):
for i in range(j, len(my_list)):
if my_list[i] < my_list[j]:
my_list[j], my_list[i] = my_list[i], my_list[j]
return my_list
def main():
input_array = [12, 9, 13, 7, 3, 19, 6, 5]
output_array = selection_sort(input_array)
print(output_array)
[3, 5, 6, 7, 9, 12, 13, 19]

Categories

Resources