beginner to coding so posting question here [closed] - python-3.x

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
the question is Given an array A of length n. we need to create a new array B of length n-2 such that B[i] = max(A[i-1], A[i], A[i+1]) except from array A and n (1-based indexing) and return the new array, first and last element of array A has to excluded
def newArray(A):
B = []
for i in range(1, len(A)-2):
B[i] = (max(A[i-1], A[i], A[i+1]))
return B
this is my approach but landed with no output
sample testcase:
input
A = [1,2,3]
output 3
explanation: max(1,2,3) is 3 so answer is 3

Consider utilizing list.append and recognizing that you need to use len(A) - 1 instead of len(A) - 2 since the stop parameter for range is exclusive:
def new_array(A: list[int]) -> list[int]:
B = []
for i in range(1, len(A) - 1):
B.append(max(A[i - 1], A[i], A[i + 1]))
return B
print(f'{new_array([1, 2]) = }')
print(f'{new_array([1, 2, 3]) = }')
print(f'{new_array([1, 2, 3, 4]) = }')
print(f'{new_array([1, 2, 3, 2]) = }')
Output:
new_array([1, 2]) = []
new_array([1, 2, 3]) = [3]
new_array([1, 2, 3, 4]) = [3, 4]
new_array([1, 2, 3, 2]) = [3, 3]

Related

Count list elements without duplicates [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Python and finding my way.
I have this code here:
L = ['l','u','k','e','l','i']
k = [L.count(c) for c in L]
print(k)
And it gives this result:
[2, 1, 1, 1, 2, 1]
which is great but I want to get to this result
[2, 1, 1, 1, 1]
That is, a result in which the 'l' is not counted twice.
How do I do that?
NB: the list L here is from user input, I convert user input to list L so I can't tell beforehand what the user will input but once its typed in, I want to convert the string into a list and count all the elements without counting an element twice.
you can do something like that
L = ['l','u','k','e','l','i']
import collections
s = collections.Counter(L)
s
Counter({'l': 2, 'u': 1, 'k': 1, 'e': 1, 'i': 1})
s.values()
dict_values([2, 1, 1, 1, 1])
Use a dictionary:
L = ['l','u','k','e','l','i']
counts = {}
for l in L:
counts[l] = counts.get(l, 0) + 1
print(counts)
# {'k': 1, 'l': 2, 'e': 1, 'u': 1, 'i': 1}

I am trying to append square of a list to the list [duplicate]

This question already has answers here:
Can I append items to a list that I am looping through in Python? [duplicate]
(2 answers)
Closed 3 years ago.
list1=[-4,-1,0,3,10]
for k in list1:
list1.append(k**2)
I am trying to find the square of the numbers in list1 and then I am trying to add the square to the same list1. But it is going to infinity loop. Can some1 help me here
Try this
>>> list1 = [-4, -1, 0, 3, 10]
>>> list1.extend([x**2 for x in list1])
>>> list1
[-4, -1, 0, 3, 10, 16, 1, 0, 9, 100]
may be something like this
list1=[-4,-1,0,3,10]
list2 = []
for k in list1:
list2.append(k**2)
list1 = list1 + list2
print(list1)
Or This
list1=[-4,-1,0,3,10]
list1 = list1 + [k**2 for k in list1]
print(list1)
http://ideone.com/vgZlXl

Can this numpy.where() approach be adapted for two conditions instead of one? [duplicate]

This question already has answers here:
Numpy where function multiple conditions
(9 answers)
Closed 5 years ago.
I can get numpy.where() to work for one condition but not two conditions.
For one condition:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1, 1, 1, 2, 4, 5])
i, = np.where(a < 2)
print(i)
>> [ 0 5 8 10 11 12] ## indices where a[i] = 1
For two conditions:
# condition = (a > 1 and a < 3)
# i, = np.where(condition)
i, = np.where(a > 1 and a < 3)
print(i)
>> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I read up on a.any() and a.all() in this other SO post, but this doesn't work for my purpose since I want all indices that fit the condition rather than a single boolean value.
Is there a way to adapt this for two conditions?
Use np.where((a > 1) & (a < 3))

List containing functions is returned as None [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 5 years ago.
def foo(a):
a.append(1)
if len(a) > 10:
print a
return a
else:
foo(a)
Why this recursive function returns None (see transcript below)? I can't quite understand what I am doing wrong.
In [263]: x = []
In [264]: y = foo(x)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
In [265]: print y
None
You don't return anything in the else clause:
else:
return foo(a)

Check a tuple for at least 5 consecutive numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a tuple A and I would like to check if it contains at least 5 consecutive numbers somewhere. What is the most time efficient way to do this?
A= (1, 4, 5, 6, 7, 8, 12) -->TRUE
This is used in a montecarlo siulation to check if a 7 card poker hand contain a straight.
Assuming a sorted tuple of 7 different integers. If there are 5 integers in series, i.e. (n, n+1, n+2, n+3, n+4), they must start on position 0, 1 or 2 and end on position 4, 5, or 6 respectively.
straight = any(a[4+i] - a[i] == 4 for i in (0,1,2))
Update: If the sequence length is not fixed:
(suggested by tobias_k in comments)
straight = any(a[4+i] - a[i] == 4 for i in range(len(a)-4))
I don't think there's a simple one-liner for this, but you basically just have to loop the elements in the list and check whether it's one more than the last one, and return True if the running count reaches 5. You also have to consider the case that there are two cards with the same value.
def has_straight(values, req=5):
last = count = None
for x in values:
if x - 1 == last:
count += 1 # one more on the straight
elif x == last:
pass # same value as before
else:
count = 1 # start a new straight
if count >= req:
return True
last = x
return False
Some examples:
has_straight((1, 4, 5, 6, 7, 10, 12)) # no straight -> False
has_straight((1, 4, 5, 6, 7, 8, 12)) # straight in middle -> True
has_straight((1, 2, 3, 4, 5, 10, 12)) # at beginning -> True
has_straight((1, 2, 8, 9, 10, 11, 12)) # at very end -> True
has_straight((1, 2, 2, 3, 4, 4, 5)) # straight with dupes -> True
Complexity would be O(n), which is as good as it could possibly get, as you have to check each number.

Resources