Finding the missing number in python - python

I am a beginner in programming. I saw the challenge about missing numbers on Geeks for Geeks. The solutions offered are quite complex for me. I wonder if my solution is correct because it seems too easy. Thanks!
A = [1, 2, 3, 4, 5, 7]
def missing():
for i in range (len(A)):
if A[i+1] != A[i] + 1:
return A[i] + 1
missing()

This is your example but with list comprehension. This will allow you to return a list of all the missing numbers. Keep in mind your sollution only works for gaps == 1.
A = [1, 3, 4, 5, 7]
def missing(A):
x = [A[i] + 1 for i in range (len(A)-1) if A[i+1] != A[i] + 1]
return x
print(missing(A))
output
[2, 6]

I would say that your code is quite unstable, but it may work.
I would say this is working better
A = [1, 2, 3, 4, 5, 7]
def missing():
for i in range(len(A)):
result = 0
if A[i+1] != A[i] + 1:
result A[i] + 1
return result
missing()
But also what if 1 is the missing number?
This code below allows 1 to be the missing number
A = [1, 2, 3, 4, 5, 7]
def missing():
if A[0] != 1:
result = 1
return result
for i in range(len(A)):
result = 0
if A[i+1] != A[i] + 1:
result A[i] + 1
return result
missing()

You can just check that each number in the range of numbers from the first element in the list to the last element in the list exists in the list.
for Loop:
>>> def missing_num(numlist):
numlist.sort()
missing = []
for i in range(numlist[0], numlist[-1]+1):
if i not in numlist:
missing.append(i)
return missing
>>> missing_num([1, 2, 3, 4, 5, 7])
[6]
List Comprehension:
>>> def missing_num(numlist):
numlist.sort()
nums = list(range(numlist[0], numlist[-1]+1))
return [i for i in nums if i not in numlist]
>>> missing_num([1, 2, 3, 4, 5, 7])
[6]
With some randomized lists:
>>> for _ in range(5):
randlist = random.sample(list(range(10)), 7)
print(randlist, missing_num(randlist))
[1, 2, 3, 4, 5, 7, 9] [6, 8]
[0, 1, 2, 5, 6, 7, 8] [3, 4]
[0, 1, 3, 4, 5, 6, 9] [2, 7, 8]
[2, 4, 5, 6, 7, 8, 9] [3]
[0, 2, 3, 4, 5, 7, 8] [1, 6]

Related

Find pairs of consecutive integers in array PYTHON

I'm trying to loop through an array and return an integer every time it shows up twice --- I've been trying to figure this out for days and really need some help
example:
input = [3, 4, 4, 4, 5, 6, 6, 5, 4, 4]
result = [4,6,4]
result=[]
def findPairs(input):
i = 0
while i < len(input):
for j in input:
if input[1]==input[2]:
result.append(j)
i += 1
print(result)
print (findPairs(input))
In response to the more recent clarifications:
def find_pairs(xs):
result = []
i = 0
while i < len(xs) - 1:
if xs[i] == xs[i + 1]:
result.append(xs[i])
i += 1
i += 1
return result
Testing:
>>> xs = [3, 4, 4, 4, 5, 6, 6, 5, 4, 4]
>>> find_pairs(xs)
[4, 6, 4]
Update: Minor off-by-one bug fix.
Try this method with list comprehensions -
import itertools
inp = [3, 4, 4, 4, 5, 6, 6, 5, 4, 4]
l = [i for i in zip(inp, inp[1:]) if i[0]==i[1]] #first get consecutive repeats
out = [k[0] for k, g in itertools.groupby(l)] #sequential grouping of repeated groups to count more than 2 occurances as a single one as well
print(out)
[4, 6, 4]

Count the number of times the positions of two lists have the same element

If you have two lists,
a = [1, 2, 3, 4, 5]
b = [1, 3, 2, 4, 7]
how can you count the number of times elements at a certain position coincide? For example 1 and 4 in the above example would have 2 cases of elements coinciding.
sum(a_ == b_ for a_, b_ in zip(a, b))
zip can give you the elements that share a position, and you can use sum to count the number of times they match:
a = [1, 2, 3, 4, 5]
b = [1, 3, 2, 4, 7]
print(sum(x == y for x, y in zip(a, b))) # 2
You can use below code and you will get positions which coincide and get sum of them as well.
a = [1, 2, 3, 4, 5]
b = [1, 3, 2, 4, 7]
print(len([i for i,val in enumerate(zip(a,b)) if val[0]==val[1]]))
to get positions you can use
print([i for i,val in enumerate(zip(a,b)) if val[0]==val[1]])
one more version:
a = [1, 2, 3, 4, 5]
b = [1, 3, 2, 4, 7]
print(sum(a[i] == b[i] for i in range(len(a))))
How about this?
# lists:
a = [1, 2, 3, 4, 5]
b = [1, 3, 2, 4, 7]
# initialize variables:
number_of_collisions = 0
# iterate over each element:
for i in range(len(a)):
if a[i] == b[i]:
number_of_collisions += 1
print(number_of_collisions)

How to split a nested list of ints into a a list

lst = [[1, 5],
[2, 2]
this is my nested list, I need to make a list of the points of this:
output = [[1, 5, 2, 2]
here is my attempt at this which works for this case but fails if I have an example where the row length is 6 or greater than 4
new_lst = []
for x in range(len(lst)):
for y in range(0, len(lst[x]), 2):
new_lst.append([lst[x][y],lst[x][y+1]])
counter_a = 0
counter_b = 1
output = []
while counter_b - 4 <= len(lst):
output.append(new_lst[counter_a] + new_lst[counter_a + 2])
output.append(new_lst[counter_b] + new_lst[counter_b + 2])
counter_a += 4
counter_b += 4
print(output)
How about this? This is general for all lists with size nxm where n and m are even numbers. The logic is to iterate with a step of 2 in both row and column, then take the block of 2x2 elements and append it to the output list.
lst = [[1, 6, 5, 6],
[2, 5, 6, 8],
[7, 2, 8, 1],
[4, 4, 7, 3]]
output = []
for j in range(0, len(lst), 2):
for i in range(0, len(lst[0]), 2):
output.append([lst[j][i], lst[j][i+1], lst[j+1][i], lst[j+1][i+1]])
output : [[1, 6, 2, 5], [5, 6, 6, 8], [7, 2, 4, 4], [8, 1, 7, 3]]
Try using:
print([x for i in list(zip(*[[i[:2], i[2:]] for i in lst])) for x in [i[0] + i[1], i[2] + i[3]]])

Remove integer on a list if it is smaller than the previous integer on the list

I have an unordered list of numbers
num_list=[3 4 5 1 2 5 6 3 1 2 6 9]
I want to run through the list and remove all numbers that are smaller than the previous such that the list is ordered in ascending order after deleting such numbers. How can I do that?
Expected output:
num_list=[3 4 5 5 6 6 9]
One simple approach would be to iteratively add values from num_list to a new list if they satisfy the condition of being greater than the last appended value:
out = [num_list[0]]
for i in num_list[1:]:
if i >= out[-1]:
out.append(i)
print(out)
# [3, 4, 5, 5, 6, 6, 9]
Short approach with functools.reduce function:
import functools
num_list = [3,4,5,1,2,5,6,3,1,2,6,9]
res = functools.reduce(lambda x, y: x if y < x[-1] else x + [y],
num_list[1:], [num_list[0]])
print(res)
The output:
[3, 4, 5, 5, 6, 6, 9]
A simple approach using list.pop
num_list = [3, 4, 5, 1, 2, 5, 6, 3, 1, 2, 6, 9]
i = 1
while (i < len(num_list)):
if num_list[i] < num_list[i - 1]:
num_list.pop(i)
else:
i += 1
print(num_list)
# [3, 4, 5, 5, 6, 6, 9]
A comprehension could be used for this:
num_list = [3, 4, 5, 1, 2, 5, 6, 3, 1, 2, 6, 9]
(x for i, x in enumerate(num_list) if all(x >= j for j in num_list[:i]))
Though it's not as efficient as yatu's answer.
Version with itertools.groupby:
from itertools import groupby
num_list=[3, 4, 5, 1, 2, 5, 6, 3, 1, 2, 6, 9]
out = [num_list[0]]
[out.extend(g) for v, g in groupby(num_list[1:], lambda k: k>=out[-1]) if v]
print(out)
Prints:
[3, 4, 5, 5, 6, 6, 9]
Here is another one-liner solution using itertools.accumulate.
from itertools import accumulate
result = [n for n, cur_max in zip(l, accumulate(l, max)) if n >= cur_max]
Despite being concise, it is actually much less efficient than the following solution using an explicit for loop. This is also the most efficient solution as of now.
cur_max = l[0]
result = []
for e in l:
if e >= cur_max:
cur_max = e
result.append(e)

Algorithm to return a reordered range for data rendering

Here are examples:
given: 1,2,3 [list or range of numbers]
return: 2,1,3 [reordered list]
given: 1,2,3,4,5
return: 3 1 5 2 4
given: 1,2,3,4,5,6,7
return: 4 1 7 2 6 3 5 OR 4 7 1 5 3 2 6 or similar
given: 1,2,4,5,6,7,8,9
return: 5,1,9,3,7,2,8,4,6 or similar
In rendering you start with the center, then the most extreme cases, and become more and more detailed. This is NOT random. I'm in python, but theres got to be a name for this in comp sci. Help appreciated.
Edit to add
even case -
given: 1234
return: 2,1,4,3 OR 3,1,4,2 OR 2,4,1,3 OR 3,4,1,2
A valid, although ungraceful solution:
def sigorder(lst):
result = []
l = len(lst)
if l <= 2:
return lst
if l > 2:
result.append(lst[l/2])
result.append(lst[0])
result.append(lst[-1])
right = sigord(lst[l/2+1:-1])
left = sigord(lst[1:l/2])
result.extend(slicezip(left, right))
return result
Inner, recursive function:
def sigord(lst):
result = []
if len(lst) < 3:
return lst
else:
l = len(lst)
result.append(lst[l/2])
left = sigord(lst[0:l/2])
right = sigord(lst[l/2 + 1:len(lst)])
result.extend(slicezip(left, right))
return result
slicezip() (Note: conveniently handles the potential unevenness of the left/right lists automagically)
def slicezip(a, b):
result = [0]*(len(a)+len(b))
result[::2] = a
result[1::2] = b
return result
Outputs for lists length 4-9 :
[3, 1, 4, 2]
[3, 1, 5, 2, 4]
[4, 1, 6, 2, 5, 3]
[4, 1, 7, 2, 5, 3, 6]
[5, 1, 8, 3, 6, 2, 7, 4]
[5, 1, 9, 3, 7, 2, 6, 4, 8]
This should do it:
def extreme_cases(upd_itrr, new_itrr):
new_itrr.append(min(upd_itrr))
new_itrr.append(max(upd_itrr))
upd_itrr.remove(min(upd_itrr))
upd_itrr.remove(max(upd_itrr))
if len(upd_itrr) >= 2:
extreme_cases(upd_itrr, new_itrr)
return upd_itrr, new_itrr
def reordered_range(itr):
new_itr = []
center = 0
if len(itr) % 2 != 0:
center = itr[len(itr) // 2]
elif len(itr) % 2 == 0:
center = itr[(len(itr) // 2) - 1]
new_itr.append(center)
upd_itr = itr[:]
upd_itr.remove(center)
upd_itr, new_itr = extreme_cases(upd_itr, new_itr)
if upd_itr:
new_itr.append(upd_itr[0])
return new_itr
print(reordered_range([1, 2, 3]))
print(reordered_range([1, 2, 3, 4]))
print(reordered_range([1, 2, 3, 4, 5]))
print(reordered_range([1, 2, 3, 4, 5, 6, 7]))
print(reordered_range([1, 2, 4, 5, 6, 7, 8, 9]))
Output:
[2, 1, 3]
[2, 1, 4, 3]
[3, 1, 5, 2, 4]
[4, 1, 7, 2, 6, 3, 5]
[5, 1, 9, 2, 8, 4, 7, 6]
Another solution:
import numpy as np
from copy import copy
def bisecting_order(lst):
# bisecting order of an unordered list
result = []
l = len(lst)
if l < 3:
return lst
result.append(closest(lst,np.mean(lst)))
result.append(min(lst))
result.append(max(lst))
# get bisections
while len(result)!=len(lst):
temp_list = copy(result)
temp_list.sort()
for i in xrange(len(temp_list)-1):
newnum = closest(lst,np.mean([temp_list[i],temp_list[i+1]]))
if newnum in result:
continue
else:
result.append(newnum)
return result
def closest(mylist,mynum):
return min(mylist, key=lambda x:abs(x-mynum))

Categories