1 ) input: 3 , return [1, 2, 3]
then continue
2) input: 2, return [2, 4, 3]
then continue
3) input: 6, return [3, 6, 6, 4, 5, 6]
then continue
4) input: 1, return [4, 6, 6, 4, 5, 6]
then continue
5) input: 1, return [5, 6, 6, 4, 5, 6]
My code :
1)
list_num = [ int(i) for i in input('enter numbers divided by space ').split()]
print(list_num)
lst = []
# number of elemetns as input
n = int(input("Enter number of elements : "))
# iterating till the range
for i in range(0, n):
ele = int(input())
lst.append(ele) # adding the element
print(lst)
test_list1 = []
n2 = int(input("Enter number of elements2 : "))
for i2 in range(0, n2):
ele2 = int(input())
test_list1.append(ele2) # adding the element
print(test_list1)
res_list = [lst[i] + test_list1[i2] for i in range(len(lst))]
print(res_list)
but not dynamic
Your code 2 seems to be doing a pair-wise list addition.
But the computation of your res_list is using the i2 index, it should be just i.
Without the fix : [4, 5] plus [0, 1] gives [5, 6].
With the fix : [4, 5] plus [0, 1] gives [4, 6].
Related
The odd_numbers function returns a list of odd numbers between 1 and n, inclusively. Fill in the blanks in the function, using list comprehension. Hint: remember that list and range counters start at 0 and end at the limit minus 1.
def odd_numbers(n):
return [x for x in ___ if ___]
print(odd_numbers(5))
# Should print [1, 3, 5]
print(odd_numbers(10))
# Should print [1, 3, 5, 7, 9]
print(odd_numbers(11))
# Should print [1, 3, 5, 7, 9, 11]
print(odd_numbers(1))
# Should print [1]
print(odd_numbers(-1))
# Should print []
Answer:
def odd_numbers(n):
return [x for x in range(n + 1) if x % 2 == 1]
print(odd_numbers(5))
# Should print [1, 3, 5]
print(odd_numbers(10))
# Should print [1, 3, 5, 7, 9]
print(odd_numbers(11))
# Should print [1, 3, 5, 7, 9, 11]
print(odd_numbers(1))
# Should print [1]
print(odd_numbers(-1))
# Should print []
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]
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]
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]]])
I want to solve my programming problem, the problem goes like so:
input: 3 #if i input as first input example 3, output is [1, 2, 3]
[1, 2, 3]
input: 2 #2nd input example 2 output is [1, 2] + [1, 2, 3] = [2, 4, 6]
[2, 4, 3]
input: 6 #3rd input [2, 4, 6] + [1, 2, 3, 4, 5, 6] = [3, 6, 6, 4, 5, 6]
[3, 6, 6, 4, 5, 6]
My code:
while True:
a = input('Input : ')
n = range (1,a+1,1)
print n
Outputs:
Input : 3
[1, 2, 3]
Input : 2
[1, 2]
Input : 6
[1, 2, 3, 4, 5, 6]
How can I solve this problem?
Building on your existing code, I would use itertools.izip_longest (Python 2, for 3 use zip.longest):
>>> import itertools
>>> nxt = []
>>> while True:
a = input('Input : ')
n = range(1, a+1, 1) # could change to range(1, a+1)
nxt = map(sum, itertools.izip_longest(n, nxt, fillvalue=0))
print nxt
Which yields:
Input : 3
[1, 2, 3]
Input : 2
[2, 4, 3]
Input : 6
[3, 6, 6, 4, 5, 6]
You can use map
result = []
while True:
a = input('Input : ')
n = range(1, a+1)
result = [(x or 0) + (y or 0) for x,y in map(None, n, result)]
print result
and result would be:
Input : 3
[1, 2, 3]
Input : 2
[2, 4, 3]
Input : 6
[3, 6, 6, 4, 5, 6]
but i can not use external code
Then you can write some code that does exactly what izip_longest does with the zero padding when the new entry is lengthier than the previous result.
The sums are performed in a list comprehension where the values and indices from the input list are gotten by applying enumerate on the entry in the comprehension. Values from the accumulated list are indexed and added to new values at the same index:
tot = []
while True:
a = input('Input : ')
n = range (1,a+1,1)
x, y = len(tot), len(n)
if y > x:
tot[x:y] = [0]*(y-x) # pad the tot list with zeros
tot[:y] = [tot[i]+v for i, v in enumerate(n)]
print tot
Output:
Input : 3
[1, 2, 3]
Input : 2
[2, 4, 3]
Input : 6
[3, 6, 6, 4, 5, 6]
Input : 1
[4, 6, 6, 4, 5, 6]
Input : 0
[4, 6, 6, 4, 5, 6]