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 22 hours ago.
This post was edited and submitted for review 22 hours ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
The task is for the value k, make that many rotations (moving the last value in the list to the front) in place. First the list nums is reversed, and then the list until the given k value is reversed again, however this does not pass the test cases. However, when I test this code on an online compiler with an arbitrary list, it does what it is supposed to.
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
Do not return anything, modify nums in-place instead.
k = k % len(nums)
nums = nums[::-1]
nums[k::] = nums[k::][::-1]
Related
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 7 months ago.
Improve this question
I am breaking down the following problem:
def removeElement(nums, val):
for i in range(nums.count(val)):
nums.remove(val)
return len(nums)
I cannot understand what for i in range(nums.count(val)): does
Thx
nums.count(val) counts how many instances of val are in nums.
for i in range(nums.count(val)):... is a loop that iterates nums.count(val) times, or, in other words, it iterates as many times as the number of val's in nums.
i does not actually do anything here, so the more "Pythonic" way of writing that would be to replace i with an underscore (_) to indicate that the variable is not used.
Finally, nums.remove(val) removes the first occurence of val from nums. This is repeated num.count(val) times, so this loop is actually removing all instances of val from num.
return len(nums) simply returns the number of elements in nums after all the val's are removed, though it is wrongly indented (should be outside of the for loop).
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 11 months ago.
Improve this question
I have a code which compares two lists
a=[10,20,30,40,50,60,40,45]
x=[10,50,5,100,15,60]
z=([i > j for i, j in zip(a[-len(x):], x)])
print(z)
I need to rewrite it in the following format but current wording
returns error. How to correct it?
a=[10,20,30,40,50,60,40,45]
x=[10,50,5,100,15,60]
for i in range(a[-len(x):],x):
if a[i]>x[i]:
print('yes')
else:
print('no')
The issue is you're using range incorrectly. range takes integer arguments while you're passing the array
....
start_range = -len(x)
for i in range(len(x)):
if a[start_range]>x[i]:
print('yes')
else:
print('no')
start_range+=1
...
Now I'm not sure what is it you expect from the output since the first snippet returns boolean in an array while the second snippet would print out yes and no in separate lines.
However, you can build the logic around the above since the only problem was the loop range.
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 3 years ago.
Improve this question
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num] from everyone.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list because it is reset multiple times as everyone in list everyone takes a turn.
I tried this question, but it only worked from removing entire lists from eachother. Is there a way I could change it to work for this case?
Thanks for your time!
everyone.remove(enemy_list[num])
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
Hi I am coding in Python and I have to Write a program that lets a user choose a letter. The program will then find all the words beginning with that letter in a list and print them out. It should also say how many words it found. Can anyone help I don't have a clue and an internet search turned up nothing.
Long version:
def num_words(word_list, letter):
total = []
for word in word_listlist:
if ( word.startswith(letter) ):
total.append(word)
return total
new_list = ["hello","bring","http","bin"]
print(num_words(new_list,"h"))
print(len(num_words(new_list,"h")))
Or even easier using list comprehension:
[val for val in new_list if val.startswith("h")]
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 7 years ago.
Improve this question
I am trying to recursively append to a list while iterating over a nested list. For example, if I have a list [['a',['b',['c','d']]]], I would like a function to return [['a','b','c'],['a','b','d']]. My attempt so far is the following:
def recurse_append(data):
r_list=[]
for elem in data:
if isinstance(elem, list):
r_list.extend(recurse_append(elem))
else:
r_list.append(elem)
return r_list
which for this example returns ['a','b','c','d']. Unlike the example, the nested list that I am writing this for does not contain the same number of elements at each level, so I thought that a recursive function would be the best way to take this into account. Any help towards a solution would be greatly appreciated.